HTTPIDL
HTTPIDL 是一组用于类型安全 HTTP 网络和从特定 IDL 生成代码的工具,它支持 Swift 3/4。HTTPIDL旨在帮助开发者关注业务逻辑,尽可能少地关心HTTP细节(你只有在需要扩展HTTPIDL时才需要知道HTTP的细节)
状态
功能
- 类型安全
- Swift 3/4 代码生成器,也支持手写代码
- URL / JSON / URLEncodedForm 请求编码器(甚至支持编码器的组合)
- 上传文件 / 数据 / MultipartFormData
- 自动将 JSON 响应体转换为模型对象(您可以扩展响应解码器以支持其他响应体类型)
- 可扩展的请求编码器
- 可扩展的响应解码器
- 可选 HTTP 客户端(默认为 NSURLSession)
- 请求 & 响应观察者
- 请求 & 响应重写器
要求
- iOS 8.0 +
- Xcode 8.1 +
- Swift 3 +
- python 2.7.x
*注意:您可以在 Swift 3 中使用版本 1.1.9
安装
CocoaPods
CocoaPods是Cocoa项目的依赖管理工具。您可以使用以下命令安装:
$ gem install cocoapods
要使用CocoaPods将HTTPIDL集成到您的Xcode项目中,请在Podfile中指定它:
use_frameworks!
target '<Your Target Name>' do
pod 'HTTPIDL'
end
然后,运行以下命令:
$ pod install
开始使用
生成请求和响应
创建一个名为example.http的文本文件,放在/your/httpidl目录下,并编写以下代码:
MESSAGE /my/example {
GET REQUEST {
INT32 t1 = t;
STRING t2 = tt;
}
GET RESPONSE {
INT64 x1 = x;
DOUBLE x2 = xx;
}
}
然后,运行以下命令:python Pods/HTTPIDL/Sources/Compiler/HTTPIDL.py -d /your/httpidl/directory -o /your/httpidl/idl_output
然后,请求和响应代码文件将放在/your/httpidl/idl_output中,生成以下代码:
import Foundation
import HTTPIDL
class GetMyExampleRequest: Request {
var method: String = "GET"
private var _configuration: RequestConfiguration?
var configuration: RequestConfiguration {
get {
guard let config = _configuration else {
return BaseRequestConfiguration.create(from: manager.configuration, request: self)
}
return config
}
set {
_configuration = newValue
}
}
var manager: RequestManager = BaseRequestManager.shared
var uri: String {
return "/my/example"
}
var t1: Int32?
var t2: String?
let keyOfT1 = "t"
let keyOfT2 = "tt"
var content: RequestContent? {
var result = [String: RequestContent]()
if let tmp = t1 {
result["t"] = tmp.asRequestContent()
}
if let tmp = t2 {
result["tt"] = tmp.asRequestContent()
}
return .dictionary(value: result)
}
@discardableResult
func send(completion: @escaping (GetMyExampleResponse) -> Void, errorHandler: @escaping (HIError) -> Void) -> RequestFuture<GetMyExampleResponse> {
let future: RequestFuture<GetMyExampleResponse> = manager.send(self, responseHandler: completion, errorHandler: errorHandler, progressHandler: nil)
return future
}
@discardableResult
func send(rawResponseHandler: @escaping (HTTPResponse) -> Void, errorHandler: @escaping (HIError) -> Void) -> RequestFuture<HTTPResponse> {
let future = manager.send(self, responseHandler: completion, errorHandler: errorHandler, progressHandler: nil)
return future
}
}
struct GetMyExampleResponse: Response {
let x1: Int64?
let x2: Double?
let rawResponse: HTTPResponse
init(content: ResponseContent?, rawResponse: HTTPResponse) throws {
self.rawResponse = rawResponse
guard let content = content, case .dictionary(let value) = content else {
self.x1 = nil
self.x2 = nil
return
}
self.x1 = Int64(content: value["x"])
self.x2 = Double(content: value["xx"])
}
}
您可以使用以下代码发送HTTP请求:
import HTTPIDL
let request = GetMyExampleRequest()
request.t1 = 123
request.t2 = "while my guitar gently weeps"
request.send(completion: { (response) in
//handle GetMyExampleResponse
}) { (error) in
//handle error
}
手写请求
在HTTPIDL中,无论是生成的代码还是手写的代码,都必须遵守'Request'协议。
public protocol Request {
var method: String {get} //HTTP method
var configuration: RequestConfiguration {get set} //configuration contains baseURLString, headers and so on
var uri: String {get} //uri reference without scheme and host
var content: RequestContent? {get} //http request body
}
遵守'Request'协议的类可以通过以下代码发送:
let request = //your handwritten request
request.configuration.encoder = URLEncodedQueryEncoder.shared
BaseRequestManager.shared.send(request)
或者您可以使用以下简单方法:
HTTPIDL.get
HTTPIDL.getJSON
HTTPIDL.post
HTTPIDL.postJSON
HTTPIDL.delete
HTTPIDL.deleteJSON
HTTPIDL.put
HTTPIDL.putJSON
HTTPIDL.patch
HTTPIDL.patchJSON
HTTPIDL.send
String.download
URL.download
路线图
- 优化错误处理(版本 1.2.0)
- 支持 Objective-C
- 增加对 HTTPS 的支持
- 文档