Alamofire源码分析(2)——DataRequest
2020-12-08 本文已影响0人
无悔zero
上次分析了SessionManager层,这次往下走分析request层。先看例子:
SessionManager.default //直接跳过Alamofire层调用也可以
.request(url, method: .get, parameters: parameters)
.response { (result) in
print(result)
}
- 直接找到源码位置,可以看到这一层返回
DataRequest
,这个不是请求的URLRequest
,但请求的URLRequest
在里面创建了:
open class SessionManager {
...
@discardableResult
open func request(
_ url: URLConvertible,
method: HTTPMethod = .get, /*默认为get方法*/
parameters: Parameters? = nil,
encoding: ParameterEncoding = URLEncoding.default,
headers: HTTPHeaders? = nil)
-> DataRequest
{
var originalRequest: URLRequest?
//创建URLRequest
do {
originalRequest = try URLRequest(url: url, method: method, headers: headers)
let encodedURLRequest = try encoding.encode(originalRequest!, with: parameters)
return request(encodedURLRequest)
} catch {
return request(originalRequest, failedWith: error)
}
}
...
}
- 下一步则是
encoding.encode(originalRequest!, with: parameters)
对参数进行处理,还会判别请求方法分开处理:
public struct URLEncoding: ParameterEncoding {
...
public func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest {
var urlRequest = try urlRequest.asURLRequest()//跟asURL一个意思
guard let parameters = parameters else { return urlRequest }
//判别请求方法分开处理
if let method = HTTPMethod(rawValue: urlRequest.httpMethod ?? "GET"), encodesParametersInURL(with: method) {
...//get
} else {
...//post
}
return urlRequest
}
...
}
- 先看对
get
请求的处理:
public struct URLEncoding: ParameterEncoding {
...
public func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest {
var urlRequest = try urlRequest.asURLRequest()
guard let parameters = parameters else { return urlRequest }
//判别请求方法分开处理
if let method = HTTPMethod(rawValue: urlRequest.httpMethod ?? "GET"), encodesParametersInURL(with: method) {
guard let url = urlRequest.url else {
throw AFError.parameterEncodingFailed(reason: .missingURL)
}
//拼接url后面的参数
if var urlComponents = URLComponents(url: url, resolvingAgainstBaseURL: false), !parameters.isEmpty {
let percentEncodedQuery = (urlComponents.percentEncodedQuery.map { $0 + "&" } ?? "") + query(parameters)
urlComponents.percentEncodedQuery = percentEncodedQuery
urlRequest.url = urlComponents.url
}
} else {
...//post
}
return urlRequest
}
...
}
从不显眼的query
函数发现,对请求参数进行了排序和符号拼接:
public struct URLEncoding: ParameterEncoding {
...
private func query(_ parameters: [String: Any]) -> String {
var components: [(String, String)] = []
//排序
for key in parameters.keys.sorted(by: <) {
let value = parameters[key]!
components += queryComponents(fromKey: key, value: value)
}
return components.map { "\($0)=\($1)" }.joined(separator: "&")//符号拼接
}
...
}
- 这里便是对
get
请求参数的主要处理。
- 回到第2步,另一种处理方式便是对
post
请求的处理,先对请求头做了处理,后面同样用query
进行参数排序和符号拼接:
public struct URLEncoding: ParameterEncoding {
...
public func encode(_ urlRequest: URLRequestConvertible, with parameters: Parameters?) throws -> URLRequest {
var urlRequest = try urlRequest.asURLRequest()
guard let parameters = parameters else { return urlRequest }
//判别请求方法分开处理
if let method = HTTPMethod(rawValue: urlRequest.httpMethod ?? "GET"), encodesParametersInURL(with: method) {
...//get
} else {
//请求头
if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
urlRequest.setValue("application/x-www-form-urlencoded; charset=utf-8", forHTTPHeaderField: "Content-Type")
}
//请求体
urlRequest.httpBody = query(parameters).data(using: .utf8, allowLossyConversion: false)
}
return urlRequest
}
...
}
- 之后便回到第1步,创建
DataRequest
返回,到这里DataRequest
这一层就完成了。