RestFetcher
使用方法
要运行示例项目,请先 克隆仓库,然后从 Example 目录中运行 pod install
要求
安装
RestFetcher 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中:
pod "RestFetcher"
使用方法
RF请求
RFRequest<T: RFDecodable> 这是一个打算用于扩展以支持REST请求家族的基础类。T定义了每个请求应生成的响应类。
您可以通过覆盖以下内容来自定义每个请求或任何子请求:
var logger: RFLogger
default = RFConsoleLogger
Allows child classes to inject their own logging framework with any object that conforms to the RFLogger protocol
var timeout: TimeInterval
default = 30
Allows child classes to modify the amount of time a request will wait for a response.
var keysToScrub: [String]
default = ["password"]
Allows child classes to define what JSON keys are obscured when logged
var restMethod: RFMethod
default = .get
Allows child classes to define the rest method to use (e.g GET, POST, PUT, DELETE, etc.)
var domain: String
Allows child classes to define the root domain of their REST service (e.g. https://google.com)
var rootPath: String
Allows child classes to defile the root api path for their REST service (e.g. /api/v1)
var pathResource: String
Allows child classes to define the restful resource path for each endpoint (e.g. /user/auth)
var queryArguments: [String: String]
Allows child classes to define the list of query arguments to include in the request URL
All keys and values will be URL encoded by the framework
var requestBody: String
Allows child classes to set the body of the request (i.e. JSON string, http url ecoded string)
var requestHeaders: [String: String]
Allows child classes to define the request headers to be sent to the server
func createResponse(responseTime: Double, code: Int, headers: [String: String], data: Data?) -> T.ResponseType?
default = instance of generic type provided from response string
Allows child classes to perform additional processing on response objects before reporting success
responseTime - The amount of time in seconds the response took
code - the response code reported by the service
headers - a key-value mapping of response headers
data - the raw data sent by the service response
调用请求
let request = MyRequest()
request.successCallback = { code, response in // process response }
request.errorCallback = { error in // process error }
request.setCustomRequestVariables() // set any arguments for the request defined by the subclass
request.fetch()
响应包装类型
此库能够处理大多数响应类型,但用户可以通过实现RFDecodable协议来定义额外的行为。
associatedType ResponseType // the generic type that this response will return in callbacks
acceptType: String? // the value to auto-inject into the Accept http header
object: ResponseType // the value of the response
init?(data: Data) // an init method for generating the response object
包含的响应包装器
RFVoidResponse // for services that are expected to return no body
RFDataResponse // for services that are expected to return binary data (e.g. files, images, etc.)
RFStringResponse // for services that are expected to return raw string data
RFDecodableResponse<T: Decodable> // for services that return JSON requres a Decodable object to decode the JSON into
RFRawResponse<T> // for responses where the user wishes to generate the response by overriding createResponse()
作者
查理斯·奥德尔,[email protected]
授权
RestFetcher 在MIT许可证下可用。有关更多信息,请参阅LICENSE文件。