jkolb/ModestProposal

语言: Swift

git: https://github.com/jkolb/ModestProposal

使构建HTTP URL和请求变得容易。
Makes building HTTP URLs and requests easy.
README.md (中文)

ModestProposal 2.0

使构建HTTP URL和请求变得容易。可以与任何接受NSURLRequest作为参数的网络库一起使用。

特征

  • URL构建助手
  • 请求建立帮助者
  • 基本认证编码
  • 验证HTTP响应,状态代码和内容类型
  • 常用HTTP状态代码,内容类型,方法和标头字段的枚举

URL构建

let baseURL = NSURL(string: "http://test.com")!

// http://test.com/login
let loginURL = baseURL.relativeToPath("/login")

// http://test.com/data?id=100&page=3
let dataURL = baseURL.relativeToPath("/data", parameters: ["id": "100", "page": "3"])

请求建设

let baseRequest = NSMutableURLRequest(URL: baseURL)

// Set custom header for all requests
baseRequest["Custom-Header"] = "Custom value"

// URL will have the parameters added to the end of it
let readRequest = baseRequest.GET(path: "/object", parameters: ["id": "100", "page": "3"])
readRequest[.Accept] = "application/json"

// HTTPBody will be set to parameters, content type will be "application/x-www-form-urlencoded"
// and length will be set to match the size of the data generated by parameters
let createRequest = baseRequest.POST(path: "/create", parameters: ["id": "100", "page": "3"])

// HTTPBody will be set to JSON data, content type will be "application/json"
// and length will be set appropriately
let updateRequest = baseRequest.PUT(path: "/update", JSONObject: ["test": 100])

// HTTPBody will be set to data, content type will be "application/octet-stream"
// and length will be set to the length of the supplied data
let data = NSData(contentsOfFile: "file")
let dataRequest = baseRequest.POST(path: "/data", body: data)

基本认证

let baseRequest = NSURLRequest(URL: baseURL)
let loginRequest = baseRequest.POST(path: "/login")
loginRequest.basicAuthorization(username: "test", password: "test")

响应验证

let response: NSURLResponse = ...

do {
    try response.validateIsSuccessfulJSON()
    // OR
    try response.validateIsSuccessfulImage()
    // OR
    try response.validateIsHTTP(statusCode: .OK, contentType: .ApplicationJSON)
    // OR
    try response.validateIsHTTP(statusCode: 200, contentType: "application/json")
    // OR
    try validate(when: respone.isHTTP, otherwise: HTTPError.UnexpectedResponse(response))
    try validate(when: respone.HTTP.statusCode == 200, otherwise: HTTPError.UnexpectedStatusCode(respone.HTTP.statusCode))
    try validate(when: respone.HTTP.MIMEType == "application/json", otherwise: HTTPError.UnexpectedContentType(respone.HTTP.MIMEType))
}
catch {
    // Handle error
}

本文使用googletrans自动翻译,仅供参考, 原文来自github.com

en_README.md

ModestProposal 2.0

Makes building HTTP URLs and requests easy. Can be used with any networking library that accepts NSURLRequest as a parameter.

Features

  • URL building helpers
  • Request building helpers
  • Basic authentication encoding
  • Validation of HTTP response, status codes, and content types
  • Enums for common HTTP status codes, content types, methods, and header fields

URL Building

let baseURL = NSURL(string: "http://test.com")!

// http://test.com/login
let loginURL = baseURL.relativeToPath("/login")

// http://test.com/data?id=100&page=3
let dataURL = baseURL.relativeToPath("/data", parameters: ["id": "100", "page": "3"])

Request building

let baseRequest = NSMutableURLRequest(URL: baseURL)

// Set custom header for all requests
baseRequest["Custom-Header"] = "Custom value"

// URL will have the parameters added to the end of it
let readRequest = baseRequest.GET(path: "/object", parameters: ["id": "100", "page": "3"])
readRequest[.Accept] = "application/json"

// HTTPBody will be set to parameters, content type will be "application/x-www-form-urlencoded"
// and length will be set to match the size of the data generated by parameters
let createRequest = baseRequest.POST(path: "/create", parameters: ["id": "100", "page": "3"])

// HTTPBody will be set to JSON data, content type will be "application/json"
// and length will be set appropriately
let updateRequest = baseRequest.PUT(path: "/update", JSONObject: ["test": 100])

// HTTPBody will be set to data, content type will be "application/octet-stream"
// and length will be set to the length of the supplied data
let data = NSData(contentsOfFile: "file")
let dataRequest = baseRequest.POST(path: "/data", body: data)

Basic authentication

let baseRequest = NSURLRequest(URL: baseURL)
let loginRequest = baseRequest.POST(path: "/login")
loginRequest.basicAuthorization(username: "test", password: "test")

Response validation

let response: NSURLResponse = ...

do {
    try response.validateIsSuccessfulJSON()
    // OR
    try response.validateIsSuccessfulImage()
    // OR
    try response.validateIsHTTP(statusCode: .OK, contentType: .ApplicationJSON)
    // OR
    try response.validateIsHTTP(statusCode: 200, contentType: "application/json")
    // OR
    try validate(when: respone.isHTTP, otherwise: HTTPError.UnexpectedResponse(response))
    try validate(when: respone.HTTP.statusCode == 200, otherwise: HTTPError.UnexpectedStatusCode(respone.HTTP.statusCode))
    try validate(when: respone.HTTP.MIMEType == "application/json", otherwise: HTTPError.UnexpectedContentType(respone.HTTP.MIMEType))
}
catch {
    // Handle error
}