22import Alamofire
33import Foundation
44
5+ /// Contains basic information which is used to create a `URLRequest`
56public struct RequestDetails : URLRequestConvertible {
7+
8+ /// The HTTP method of the request
69 public let method : HTTPMethod
10+ /// The url of the request
711 public let url : URL
12+ /// Any parameters (or nil) for the request
13+ ///
14+ /// Parameters are encoded and added to the request based on the method of
15+ /// the request
816 public let parameters : [ String : Any ] ?
917
1018 public init ( method: HTTPMethod , url: URL , parameters: [ String : Any ] ? = nil ) {
@@ -21,12 +29,21 @@ public struct RequestDetails: URLRequestConvertible {
2129 }
2230}
2331
32+ /// A basic protocol to define network requests
2433public protocol NetworkRequest : URLRequestConvertible {
34+ /// The HTTP method of the request
2535 var method : HTTPMethod { get }
36+ /// The url of the request
2637 var url : URL { get }
38+ /// Any parameters (or nil) for the request
39+ ///
40+ /// Parameters are encoded and added to the request based on the method of
41+ /// the request
2742 var parameters : [ String : Any ] ? { get }
2843}
2944public extension NetworkRequest {
45+ /// Creates a `RequestDetails` object based on the attributes of the
46+ /// instance
3047 public func getRequestDetails( ) -> RequestDetails {
3148 return RequestDetails (
3249 method: self . method,
@@ -39,11 +56,28 @@ public extension NetworkRequest {
3956 return try self . getRequestDetails ( ) . asURLRequest ( )
4057 }
4158
59+ /// Performs the completionHandler when an error occurs during the response
60+ /// handler
61+ ///
62+ /// This is a convenience function to map the response type to the expected
63+ /// completion type
64+ ///
65+ /// - parameter error: The error that occurred
66+ /// - parameter response: The response from the Alamofire
67+ /// - parameter completionHandler: The completion handler to run
4268 public func complete< R, T> ( error: Error , response: DataResponse < R > ? , completionHandler: ( DataResponse < T > ) -> Void ) {
4369 let result = Result< T> . failure( error)
4470 let errorResponse = DataResponse ( request: response? . request, response: response? . response, data: response? . data, result: result)
4571 completionHandler ( errorResponse)
4672 }
73+ /// Performs the completionHandler after a response is successfuly handled
74+ ///
75+ /// This is a convenience function to map the response type to the expected
76+ /// completion type
77+ ///
78+ /// - parameter object: The decoded response object
79+ /// - parameter response: The response from Alamofire
80+ /// - parameter completionHandler: The completion handler to run
4781 public func complete< R, T> ( object: T , response: DataResponse < R > , completionHandler: ( DataResponse < T > ) -> Void ) {
4882 let result = Result< T> . success( object)
4983 let successResponse = DataResponse ( request: response. request, response: response. response, data: response. data, result: result)
@@ -52,6 +86,13 @@ public extension NetworkRequest {
5286
5387 // MARK: Data
5488
89+ /// Performs a network request based on the attributes of this instance, and
90+ /// retrieves the response data
91+ ///
92+ /// - parameter completionHandler: A callback which is run on completion of
93+ /// the request
94+ ///
95+ /// - returns: The request that was sent
5596 @discardableResult
5697 public func responseData( completionHandler: @escaping ( ( DataResponse < Data > ) -> Void ) ) -> DataRequest {
5798 return Alamofire . request ( self ) . responseData ( completionHandler: { response in
@@ -72,13 +113,28 @@ public extension NetworkRequest {
72113
73114 // MARK: JSON
74115
116+ /// Performs a network request based on the attributes of this instance, and
117+ /// retrieves the response json
118+ ///
119+ /// - parameter completionHandler: A callback which is run on completion of
120+ /// the request
121+ ///
122+ /// - returns: The request that was sent
75123 @discardableResult
76124 public func responseJSON( completionHandler: @escaping ( ( DataResponse < Any > ) -> Void ) ) -> DataRequest {
77125 return Alamofire . request ( self ) . responseJSON { response in
78126 self . processJSONResponse ( response: response, completionHandler: completionHandler)
79127 }
80128 }
81129
130+ /// Processes a JSON response from the server
131+ ///
132+ /// This can be overridden to provide custom processing. The default
133+ /// implementation only checks for request cancellation.
134+ ///
135+ /// - parameter response: The response to process
136+ /// - parameter completionHandler: A callback which is run once processing
137+ /// is complete
82138 public func processJSONResponse( response: DataResponse < Any > , completionHandler: @escaping ( ( DataResponse < Any > ) -> Void ) ) {
83139 switch response. result {
84140 case . failure( let error) :
0 commit comments