HTTP 工具:ModestProposal
jopen
10年前
ModestProposal 是 HTTP 工具,特性:
-
URL 绑定
-
请求绑定
-
响应验证
-
JSON 格式化
-
JSON 解析
-
实体翻译
-
异步转换
URL 绑定
let baseURL = NSURL(string: "http://test.com")! let loginURL = baseURL.buildURL(path: "/login") // http://test.com/login let dataURL = baseURL.buildURL(path: "/data", parameters: ["id": "100", "page": "3"]) // http://test.com/data?id=100&page=3
请求绑定
let baseRequest = NSURLRequest(URL: baseURL) let loginRequest = baseRequest.POST("/login", parameters: ["id": "100", "page": "3"]) // HTTPBody will be set to parameters loginRequest["Custom-Header"] = "Custom value"
响应验证
let response = // NSURLResponse from a request let builder = ValidatorBuilder() // Add rules that are executed in order builder.valid(when: response.isHTTP, otherwise: NSError(domain, "MyDomain", code: kErrorCode, userInfo: nil)) builder.valid(when: response.matchesStatuses([HTTPStatusSuccessful], otherwise: NSError(domain, "MyDomain", code: kErrorCode, userInfo: nil)) builder.valid(when: response["Custom-Header"] == "Custom value", otherwise: NSError(domain, "MyDomain", code: kErrorCode, userInfo: nil)) let validator = builder.build() if let error = validator.validate() { println(error) } else { println("Valid!") }