NetService是一个轻量级的纯Swift实现的HTTP库,用于请求/下载/上传网络。该项目受到了流行的Alamofire和AFNetworking的启发。尽管该项目也是基于URL加载系统构建的,但该项目与Alamofire/AFNetworking在根本上是不同的。两个方面都不同:设计概念和用法
功能
- URL / JSON参数编码
- 异步和同步请求
- 上传文件/数据/流/表单数据
- 使用请求或恢复数据下载文件
- 使用
URLCredential
进行身份验证 - 支持三种身份验证头:
Bearer
/Basic
/ 自定义令牌 - 带有进度条的上传和下载进度闭包
- 重试请求
- TLS证书和公开密钥固定
- 请求数中件
- 兼容macOS
- 默认和自定义缓存控制
- 默认和自定义内容类型
- 用法
要求
- iOS 10.0+ / macOS 10.15+
- Xcode 12+
- Swift 5.0+
安装
pod 'NetService', '~> 1.0.5'
用法
异步请求
final class YourAPI: DataNetService, NetServiceProtocol {
var timeout: TimeInterval {
30
}
var authorization: NetServiceBuilder.Authorization {
return .none
}
var encoding: ParameterEncoding {
return URLEncoding.default
}
var credential: URLCredential? {
return nil
}
func httpHeaders() -> [String : String] {
[:]
}
func httpParameters() -> [String : Any] {
[:]
}
func httpBuilderHelper(builder: NetServiceBuilder) -> NetServiceBuilder {
return builder
}
var urlString: String {
return _urlString
}
var httpMethod: NetBuilders.Method {
return _method
}
// above code is conforms to NetServiceProtocol protocol
private var _urlString: String
private var _method: NetBuilders.Method = .GET
init(with url: String) {
_urlString = url
}
func setMethod(method: NetBuilders.Method) -> Self {
_method = method
return self
}
}
let urlString = "https://httpbin.org/get"
let api = YourAPI(with: urlString)
api.async { (request) in
let response = request.response
request
....
}
同步请求
let urlString = "https://httpbin.org/get"
let api = YourAPI(with: urlString)
let response = api.sync().response
...
下载请求
class DownloadAPI: DownloadNetService, NetServiceRequestProtocol {
var urlString: String {
return _urlString
}
func httpHeaders() -> [String : String] {
return _parameters
}
func httpParameters() -> [String : Any] {
return _headers
}
....
// Above Code is conforms to NetServiceRequestProtocol
private var _urlString = ""
private var _parameters: [String: Any] = [:]
private var _headers: [String: String] = [:]
init(with url: String, parameters: [String: Any] = [:]) {
_urlString = url
_parameters = parameters
}
init(with url: String, headers: [String: String]) {
_urlString = url
_headers = headers
}
}
let fielURL = ... // donwload file save url
let destination: DestinationClosure = {_, _ in fielURL } // config download file position
let numberOfLines = 100
let urlString = "https://httpbin.org/stream/\(numberOfLines)"
let downloadProgresssView: UIProgressView = ....
DownloadAPI(with: urlString).download(progress: { (progress) in
downloadProgresssView.progress = Float(progress.fractionCompleted)
}, to: destination) { (request) in
let downloadFileURL = request.response.downloadFileURL
print(downloadFileURL)
}
上传请求
class BaseUploadManager: UploadNetService, NetServiceRequestProtocol {
...
var urlString: String {
return _urlString
}
// Above Code is conforms to NetServiceRequestProtocol
var _urlString: String = ""
init(with url: String) {
_urlString = url
}
}
let urlString = "https://httpbin.org/post"
let bundle = Bundle(for: BaseTestCase.self)
let imageURL = bundle.url(forResource: "rainbow", withExtension: "jpg")!
let uplodProgressView: UIProgressView = ...
UploadAPI(with: urlString).upload(file: imageURL) { (progress: Progress) in
uplodProgressView.progress = Fload(progress.fractionCompleted)
} completion: { (request) in
res = request.response
if let responseString = res?.responseString {
print(responseString)
}
}
更多用法在示例和单元测试用例中
想要贡献?
贡献指南
如果你想要贡献,请从贡献指南开始。如有疑问,请随时提问。
许可证
NetService 采用 MIT 许可证发布。详细信息请见LICENSE。