From 22b941d31cc35e024e74b1cca41f9c299b3ce64e Mon Sep 17 00:00:00 2001 From: "z.samarskaya" Date: Tue, 3 Nov 2020 19:28:12 +0300 Subject: [PATCH 1/3] response validation added --- Sources/Apexy/Client.swift | 28 +++++++++++++++------------- Sources/Apexy/Endpoint.swift | 15 +++++++++++++++ 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/Sources/Apexy/Client.swift b/Sources/Apexy/Client.swift index 6b19909..14cea44 100755 --- a/Sources/Apexy/Client.swift +++ b/Sources/Apexy/Client.swift @@ -100,19 +100,21 @@ 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(endpoint.validate) + .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..63e4e59 100755 --- a/Sources/Apexy/Endpoint.swift +++ b/Sources/Apexy/Endpoint.swift @@ -29,4 +29,19 @@ 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. + /// - Returns: Validation result. + func validate(_ request: URLRequest?, response: HTTPURLResponse, data: Data?) -> Result +} + +public extension Endpoint { + func validate(_ request: URLRequest?, response: HTTPURLResponse, data: Data?) -> Result { + return .success(()) + } } From d5b87c604ab6b3b58a8cf228398738728b0ce0d1 Mon Sep 17 00:00:00 2001 From: "z.samarskaya" Date: Thu, 5 Nov 2020 19:03:53 +0300 Subject: [PATCH 2/3] [Review] Endpoint validation without Result --- Sources/Apexy/Client.swift | 5 +++-- Sources/Apexy/Endpoint.swift | 6 ++---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/Sources/Apexy/Client.swift b/Sources/Apexy/Client.swift index 14cea44..f426ec0 100755 --- a/Sources/Apexy/Client.swift +++ b/Sources/Apexy/Client.swift @@ -101,8 +101,9 @@ public final class Client { let anyRequest = AnyRequest(create: endpoint.makeRequest) let request = sessionManager.request(anyRequest) - .validate(endpoint.validate) - .responseData( + .validate { request, response, data in + Result(catching: { try endpoint.validate(request, response: response, data: data) }) + }.responseData( queue: responseQueue, completionHandler: { (response: DataResponse) in diff --git a/Sources/Apexy/Endpoint.swift b/Sources/Apexy/Endpoint.swift index 63e4e59..ddedd66 100755 --- a/Sources/Apexy/Endpoint.swift +++ b/Sources/Apexy/Endpoint.swift @@ -37,11 +37,9 @@ public protocol Endpoint { /// - response: The metadata associated with the response. /// - data: The response body data. /// - Returns: Validation result. - func validate(_ request: URLRequest?, response: HTTPURLResponse, data: Data?) -> Result + func validate(_ request: URLRequest?, response: HTTPURLResponse, data: Data?) throws } public extension Endpoint { - func validate(_ request: URLRequest?, response: HTTPURLResponse, data: Data?) -> Result { - return .success(()) - } + func validate(_ request: URLRequest?, response: HTTPURLResponse, data: Data?) { } } From 1887ed62b87c7982bc6ac7b3e01446866121dd81 Mon Sep 17 00:00:00 2001 From: "z.samarskaya" Date: Thu, 5 Nov 2020 19:05:44 +0300 Subject: [PATCH 3/3] [Review] comment fix --- Sources/Apexy/Endpoint.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Apexy/Endpoint.swift b/Sources/Apexy/Endpoint.swift index ddedd66..7c300a5 100755 --- a/Sources/Apexy/Endpoint.swift +++ b/Sources/Apexy/Endpoint.swift @@ -36,7 +36,7 @@ public protocol Endpoint { /// - request: The metadata associated with the request. /// - response: The metadata associated with the response. /// - data: The response body data. - /// - Returns: Validation result. + /// - Throws: Any response validation error. func validate(_ request: URLRequest?, response: HTTPURLResponse, data: Data?) throws }