-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Major changes of Protocols and Async/Await in Apexy (#40)
* Разделил протоколы. Переписал методы нового concurrency. * Поправил запросы в ULRSession * APIResult вынес в основной таргет * Поправил заголовки файлов * поправил образ и версию xcode * Поправил распаковку результата * Переработал методы для URLSession * Вынес обработку результатов * Вынес ResponseObserver * Обновил README * обновил доку * Apply suggestions from code review Co-authored-by: Ivan Vavilov <[email protected]> * Async cancelation and thread switching fixed * Thread safety fix + test fix + new tests for helper * Specified type for continuation * Fix cancelation check. Fix test. Fix using async/await in sync method * Fix spacing in closure Co-authored-by: Ivan Vavilov <[email protected]>
- Loading branch information
Showing
19 changed files
with
390 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// | ||
// APIResult.swift | ||
// | ||
// | ||
// Created by Aleksei Tiurnin on 17.08.2022. | ||
// | ||
|
||
import Foundation | ||
|
||
public typealias APIResult<Value> = Swift.Result<Value, Error> | ||
|
||
public extension APIResult { | ||
var error: Error? { | ||
switch self { | ||
case .failure(let error): | ||
return error | ||
default: | ||
return nil | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// | ||
// ConcurrencyClient.swift | ||
// | ||
// | ||
// Created by Aleksei Tiurnin on 16.08.2022. | ||
// | ||
|
||
import Foundation | ||
|
||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *) | ||
public protocol ConcurrencyClient: AnyObject { | ||
/// Send request to specified endpoint. | ||
/// - Parameters: | ||
/// - endpoint: endpoint of remote content. | ||
/// - Returns: response data from the server for the request. | ||
func request<T>(_ endpoint: T) async throws -> T.Content where T: Endpoint | ||
|
||
/// Upload data to specified endpoint. | ||
/// - Parameters: | ||
/// - endpoint: endpoint of remote content. | ||
/// - Returns: response data from the server for the upload. | ||
func upload<T>(_ endpoint: T) async throws -> T.Content where T: UploadEndpoint | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// | ||
// ResponseObserver.swift | ||
// | ||
// | ||
// Created by Aleksei Tiurnin on 31.08.2022. | ||
// | ||
|
||
import Foundation | ||
|
||
public typealias ResponseObserver = (URLRequest?, HTTPURLResponse?, Data?, Error?) -> Void |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// | ||
// AlamofireClient+Concurrency.swift | ||
// | ||
// | ||
// Created by Aleksei Tiurnin on 15.08.2022. | ||
// | ||
|
||
import Alamofire | ||
import Apexy | ||
import Foundation | ||
|
||
@available(macOS 10.15, iOS 13, watchOS 6, tvOS 13, *) | ||
extension AlamofireClient: ConcurrencyClient { | ||
|
||
func observeResponse( | ||
dataResponse: DataResponse<Data, AFError>, | ||
error: Error?) { | ||
self.responseObserver?( | ||
dataResponse.request, | ||
dataResponse.response, | ||
dataResponse.data, | ||
error) | ||
} | ||
|
||
open func request<T>(_ endpoint: T) async throws -> T.Content where T : Endpoint { | ||
|
||
let anyRequest = AnyRequest(create: endpoint.makeRequest) | ||
let request = sessionManager.request(anyRequest) | ||
.validate { request, response, data in | ||
Result(catching: { try endpoint.validate(request, response: response, data: data) }) | ||
} | ||
|
||
let dataResponse = await request.serializingData().response | ||
let result = APIResult<T.Content>(catching: { () throws -> T.Content in | ||
do { | ||
let data = try dataResponse.result.get() | ||
return try endpoint.content(from: dataResponse.response, with: data) | ||
} catch { | ||
throw error.unwrapAlamofireValidationError() | ||
} | ||
}) | ||
|
||
Task.detached { [weak self, dataResponse, result] in | ||
self?.observeResponse(dataResponse: dataResponse, error: result.error) | ||
} | ||
|
||
return try result.get() | ||
} | ||
|
||
open func upload<T>(_ endpoint: T) async throws -> T.Content where T : UploadEndpoint { | ||
|
||
let urlRequest: URLRequest | ||
let body: UploadEndpointBody | ||
(urlRequest, body) = try endpoint.makeRequest() | ||
|
||
let request: UploadRequest | ||
switch body { | ||
case .data(let data): | ||
request = sessionManager.upload(data, with: urlRequest) | ||
case .file(let url): | ||
request = sessionManager.upload(url, with: urlRequest) | ||
case .stream(let stream): | ||
request = sessionManager.upload(stream, with: urlRequest) | ||
} | ||
|
||
let dataResponse = await request.serializingData().response | ||
let result = APIResult<T.Content>(catching: { () throws -> T.Content in | ||
do { | ||
let data = try dataResponse.result.get() | ||
return try endpoint.content(from: dataResponse.response, with: data) | ||
} catch { | ||
throw error.unwrapAlamofireValidationError() | ||
} | ||
}) | ||
|
||
Task.detached { [weak self, dataResponse, result] in | ||
self?.observeResponse(dataResponse: dataResponse, error: result.error) | ||
} | ||
|
||
return try result.get() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.