Alamofire源碼解析 - Alamofire

協(xié)議和擴展

查看這段代碼先看到的是兩個protocol和實現(xiàn)這些協(xié)議的extension

23D7248B-5377-4FFC-9529-C128F37208C0.png

  • URLConvertible:構(gòu)造一個URL,實現(xiàn)的類有String,URL,URLComponents,方法具體實現(xiàn)都比較簡單。
 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:構(gòu)造一個URLRequest,實現(xiàn)的類只有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的地方都用對應(yīng)的Protocol表示,不直接使用對應(yīng)類型,這個設(shè)計讓代碼顯得更加靈活。

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,每類方法中又包含若干種不同的請求方法。由于這里的請求方法并沒有真正的執(zhí)行網(wǎng)絡(luò)請求,只是對SessionManager中的請求方法的封裝,所有都是很簡單調(diào)用SessionManager方法而已。

Data Request

平時使用得最多的請求類型,可以創(chuàng)建一個簡單的網(wǎng)絡(luò)請求

///必填參數(shù)是實現(xiàn)了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的方法大同小異,第三個方法參數(shù)需要傳入一個Data,這個Data應(yīng)該是用于斷點下載使用,上次任務(wù)取消后保留的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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容