HTTP 工具:ModestProposal
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!") }
本文由用户 jopen 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
转载本站原创文章,请注明出处,并保留原始链接、图片水印。
本站是一个以用户分享为主的开源技术平台,欢迎各类分享!