diff --git a/Sources/Apexy/Client.swift b/Sources/Apexy/Client.swift index 6b19909..f426ec0 100755 --- a/Sources/Apexy/Client.swift +++ b/Sources/Apexy/Client.swift @@ -100,19 +100,22 @@ public final class Client { ) -> Progress where T: Endpoint { let anyRequest = AnyRequest(create: endpoint.makeRequest) - let request = sessionManager.request(anyRequest).responseData( - queue: responseQueue, - completionHandler: { (response: DataResponse) in - - let result = APIResult(catching: { () throws -> T.Content in - let data = try response.result.get() - return try endpoint.content(from: response.response, with: data) - }) - - self.completionQueue.async { - self.responseObserver?(response.request, response.response, response.data, result.error) - completionHandler(result) - } + let request = sessionManager.request(anyRequest) + .validate { request, response, data in + Result(catching: { try endpoint.validate(request, response: response, data: data) }) + }.responseData( + queue: responseQueue, + completionHandler: { (response: DataResponse) in + + let result = APIResult(catching: { () throws -> T.Content in + let data = try response.result.get() + return try endpoint.content(from: response.response, with: data) + }) + + self.completionQueue.async { + self.responseObserver?(response.request, response.response, response.data, result.error) + completionHandler(result) + } }) return progress(for: request) diff --git a/Sources/Apexy/Endpoint.swift b/Sources/Apexy/Endpoint.swift index 310bb01..7c300a5 100755 --- a/Sources/Apexy/Endpoint.swift +++ b/Sources/Apexy/Endpoint.swift @@ -29,4 +29,17 @@ public protocol Endpoint { /// - Returns: A new endpoint content. /// - Throws: Any error creating content. func content(from response: URLResponse?, with body: Data) throws -> Content + + /// Validate response. + /// + /// - Parameters: + /// - request: The metadata associated with the request. + /// - response: The metadata associated with the response. + /// - data: The response body data. + /// - Throws: Any response validation error. + func validate(_ request: URLRequest?, response: HTTPURLResponse, data: Data?) throws +} + +public extension Endpoint { + func validate(_ request: URLRequest?, response: HTTPURLResponse, data: Data?) { } }