Skip to content

Commit

Permalink
Merge pull request #14 from zlata/validate
Browse files Browse the repository at this point in the history
response validation added
  • Loading branch information
Alexander-Ignition authored Nov 6, 2020
2 parents 0fac5b9 + 1887ed6 commit f868ac8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
29 changes: 16 additions & 13 deletions Sources/Apexy/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Data, AFError>) in

let result = APIResult<T.Content>(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<Data, AFError>) in

let result = APIResult<T.Content>(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)
Expand Down
13 changes: 13 additions & 0 deletions Sources/Apexy/Endpoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?) { }
}

0 comments on commit f868ac8

Please sign in to comment.