Simple-Networking
忘记所有的序列化逻辑、映射器以及旧的联网工具,Simple-Networking是基于 URLSession 和 Codable 协议构建的,以使您的生活更加轻松。
示例
要运行示例项目,克隆仓库,打开SimpleNetworking.xcworkspace
并打开示例目标。
安装
SimpleBinding 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的Podfile中。
pod 'Simple-Networking'
使用
import Simple_Networking
使用Codable结果执行GET请求。
给定下一个模型
- 成功案例模型。
struct User: Codable {
let title: String
let userId: Int
}
执行您的请求,预期得到的模型为结果
// 1. Prepare your endpoint.
let endpoint = "https://jsonplaceholder.typicode.com/todos/1"
// 2. Put your Codable type in the result block, in this example, your type is the *User* model.
SN.get(endpoint: endpoint) { [weak self] (response: SNResult<User>) in
switch response {
case .error(let error):
// 3. Hanlde the possible error.
print(error.localizedDescription)
case .success(let response):
// 4. Enjoy your codable result.
self?.nameLabel.text = response.title // <- Response is your User model.
}
}
使用可编码结果的GET请求和可编码错误。
给定以下两个模型
- 成功案例模型。
struct User: Codable {
let title: String
let userId: Int
}
- 错误案例模型
struct ServerError: Codable {
let error: String
let serverError: String
}
执行您的请求以获取成功情况下模型编号1或错误情况下模型编号2
// 1. Prepare your endpoint (this particular one has a 404 response).
let endpoint = "http://www.mocky.io/v2/5de68cd33700005a000924a4"
// 2. Put your Codable type in the result block.
SN.get(endpoint: endpoint) { [weak self] (response: SNResultWithEntity<User, ServerError>) in
switch response {
// Regular error
case .error(let error):
print(error.localizedDescription)
// Error parsed to your error entity
case .errorResult(let entity):
print(entity.error) // <- Entity is ServerError
print(entity.serverError)
// Regular success
case .success(let response):
print(response.title) // <- response is User
}
}
使用可编码请求和结果的POST请求。
给定下一个模型
- 成功案例和请求模型。
struct User: Codable {
let title: String
let userId: Int
}
执行您的请求,预期得到的模型为结果,并发送您的模型作为请求体
// 1. Prepare your endpoint.
let endpoint = "https://jsonplaceholder.typicode.com/posts"
// 2. Prepare your request (Codable model)
let request = User(title: "test title", userId: 99999)
// 2. Make the request
SN.post(endpoint: endpoint, model: request) { [weak self] (response: SNResult<User>) in
switch response {
case .error(let error):
// 3. Hanlde the possible error.
print(error.localizedDescription)
case .success(let response):
// 4. Enjoy
self?.humanNameLabel.text = response.title
}
}
请求头
默认情况下,SimpleNetworking将每个请求的Content-Type设置为application/json。您可以将此更改或使用自己的头信息
SimpleNetworking.defaultHeaders = [
"Content-Type": "application/json",
// Your headers
]
身份验证
我们增加了一个辅助方法来帮助您添加身份验证头(例如JWT令牌)
SimpleNetworking.setAuthenticationHeader(prefix: "Bearer", token: "TOKEN")
调用这个方法将产生
身份验证:Bearer TOKEN
调试
我们提供了调试模式,以便通过Xcode控制台详细查看所有通过Pod的请求,以下是它的操作方式
- 仅检查API响应
SimpleNetworking.debugMode = .onlyResponses
- 仅检查API请求
SimpleNetworking.debugMode = .onlyRequests
- 检查API的所有请求和响应
SimpleNetworking.debugMode = .all
默认情况下,调试模式是禁用的。
SSL-锁定
我们支持证书锁定功能(自v0.3.5版起可用),我们还正在努力添加公钥锁定支持(欢迎提交PR)。为了设置您的证书,您必须调用setupSSLPinning
方法
if let certPath = Bundle.main.path(forResource: "my_cert", ofType: ".der") {
SimpleNetworking.setupSSLPinnig(certificateFullPath: certPath)
}
此方法将远程证书与您的本地证书进行比较。远程证书是从您请求的目标主机获取的。
文档
SNResult
public enum SNResult<T: Codable> {
case success(response: T)
case error(error: SNErrors)
}
SNResultWithEntity
public enum SNResultWithEntity<T: Codable, Y: Codable> {
case success(response: T)
case error(error: SNErrors)
case errorResult(entity: Y)
}
SNResultBlock
public typealias SNResultBlock<T: Codable> = ((_ response: SNResult<T>) -> Void)?
SNResultBlockWithError
public typealias SNResultBlockWithError<T: Codable, Y: Codable> = ((_ response: SNResultWithEntity<T, Y>) -> Void)?
SNErrors
public enum SNErrors: Error {
case endpoint
case badResponse
case custom(error: Error?)
case emptyContent
case unknown(error: String?)
}
作者
Carlos Mejía, [email protected]
https://twitter.com/carlosmejia083
许可
SimpleNetworking在MIT许可下可用。有关更多信息,请参阅LICENSE文件。