@@ -25,7 +25,7 @@ public struct RequestDetails: URLRequestConvertible {
2525 }
2626
2727 public func asURLRequest( ) throws -> URLRequest {
28- var request = try URLRequest ( url: self . url, method: self . method, headers: self . headers)
28+ let request = try URLRequest ( url: self . url, method: self . method, headers: self . headers)
2929
3030 return try URLEncoding . methodDependent. encode ( request, with: self . parameters)
3131 }
@@ -44,6 +44,21 @@ public protocol NetworkRequest: URLRequestConvertible {
4444 var parameters : [ String : Any ] ? { get }
4545 /// Any headers (or nil) for the request
4646 var headers : HTTPHeaders ? { get }
47+
48+ /// A callback function which is called after the request succeeds,
49+ /// immediately before the callback
50+ ///
51+ /// The default implementation of this funciton does nothing.
52+ ///
53+ /// - parameter response: The decoded response in the format requested
54+ func onSuccess< T> ( _ response: T ) -> Void
55+ /// A callback function which is called after the request fails, immediately
56+ /// before the callback
57+ ///
58+ /// The default implementation of this funciton does nothing.
59+ ///
60+ /// - parameter error: The error that caused the failure
61+ func onError( _ error: Error ) -> Void
4762}
4863public extension NetworkRequest {
4964 /// Creates a `RequestDetails` object based on the attributes of the
@@ -71,6 +86,8 @@ public extension NetworkRequest {
7186 /// - parameter response: The response from the Alamofire
7287 /// - parameter completionHandler: The completion handler to run
7388 public func complete< R, T> ( error: Error , response: DataResponse < R > ? , completionHandler: ( DataResponse < T > ) -> Void ) {
89+ self . onError ( error)
90+
7491 let result = Result< T> . failure( error)
7592 let errorResponse = DataResponse ( request: response? . request, response: response? . response, data: response? . data, result: result)
7693 completionHandler ( errorResponse)
@@ -84,11 +101,16 @@ public extension NetworkRequest {
84101 /// - parameter response: The response from Alamofire
85102 /// - parameter completionHandler: The completion handler to run
86103 public func complete< R, T> ( object: T , response: DataResponse < R > , completionHandler: ( DataResponse < T > ) -> Void ) {
104+ self . onSuccess ( object)
105+
87106 let result = Result< T> . success( object)
88107 let successResponse = DataResponse ( request: response. request, response: response. response, data: response. data, result: result)
89108 completionHandler ( successResponse)
90109 }
91-
110+
111+ public func onSuccess< T> ( _ response: T ) { }
112+ public func onError( _ error: Error ) { }
113+
92114 // MARK: Data
93115
94116 /// Performs a network request based on the attributes of this instance, and
0 commit comments