协议和扩展
查看这段代码先看到的是两个protocol和实现这些协议的extension
- URLConvertible:构造一个URL,实现的类有String,URL,URLComponents,方法具体实现都比较简单。
public protocol URLConvertible {
/// Returns a URL that conforms to RFC 2396 or throws an `Error`.
///
/// - throws: An `Error` if the type cannot be converted to a `URL`.
///
/// - returns: A URL or throws an `Error`.
func asURL() throws -> URL
}
- URLRequestConvertible:构造一个URLRequest,实现的类只有URLRequest
/// Types adopting the `URLRequestConvertible` protocol can be used to construct URL requests.
public protocol URLRequestConvertible {
/// Returns a URL request or throws if an `Error` was encountered.
///
/// - throws: An `Error` if the underlying `URLRequest` is `nil`.
///
/// - returns: A URL request.
func asURLRequest() throws -> URLRequest
}
extension URLRequestConvertible {
/// The URL request.
public var urlRequest: URLRequest? { return try? asURLRequest() }
}
extension URLRequest: URLRequestConvertible {
/// Returns a URL request or throws if an `Error` was encountered.
public func asURLRequest() throws -> URLRequest { return self }
}
在Alamofire中需要使用URL和URLRequest的地方都用对应的Protocol表示,不直接使用对应类型,这个设计让代码显得更加灵活。
extension对URLRequest添加了一个init和adapt方法。
extension URLRequest {
/// Creates an instance with the specified `method`, `urlString` and `headers`.
///
/// - parameter url: The URL.
/// - parameter method: The HTTP method.
/// - parameter headers: The HTTP headers. `nil` by default.
///
/// - returns: The new `URLRequest` instance.
public init(url: URLConvertible, method: HTTPMethod, headers: HTTPHeaders? = nil) throws {
let url = try url.asURL()
self.init(url: url)
httpMethod = method.rawValue
if let headers = headers {
for (headerField, headerValue) in headers {
setValue(headerValue, forHTTPHeaderField: headerField)
}
}
}
func adapt(using adapter: RequestAdapter?) throws -> URLRequest {
guard let adapter = adapter else { return self }
return try adapter.adapt(self)
}
}
各种请求方法
Alamofire中有四大类的请求方法,Data Request,Download Request,Stream Request,每类方法中又包含若干种不同的请求方法。由于这里的请求方法并没有真正的执行网络请求,只是对SessionManager中的请求方法的封装,所有都是很简单调用SessionManager方法而已。
Data Request
平时使用得最多的请求类型,可以创建一个简单的网络请求
///必填参数是实现了URLConvertible的类型,比如上面介绍的
String, URL,URLComponents 。
public func request(
_ url: URLConvertible,
method: HTTPMethod = .get,
parameters: Parameters? = nil,
encoding: ParameterEncoding = URLEncoding.default,
headers: HTTPHeaders? = nil)
-> DataRequest
///只需要一个URLRequestConvertible
public func request(_ urlRequest: URLRequestConvertible) -> DataRequest
Download Request
前两个方法和上面的DataRequest的方法大同小异,第三个方法参数需要传入一个Data,这个Data应该是用于断点下载使用,上次任务取消后保留的Data。
public func download(
_ url: URLConvertible,
method: HTTPMethod = .get,
parameters: Parameters? = nil,
encoding: ParameterEncoding = URLEncoding.default,
headers: HTTPHeaders? = nil,
to destination: DownloadRequest.DownloadFileDestination? = nil)
-> DownloadRequest
public func download(
_ urlRequest: URLRequestConvertible,
to destination: DownloadRequest.DownloadFileDestination? = nil)
-> DownloadRequest
public func download(
resumingWith resumeData: Data,
to destination: DownloadRequest.DownloadFileDestination? = nil)
-> DownloadRequest