-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tech(session): use async as main implementation (#7)
- Loading branch information
Showing
13 changed files
with
269 additions
and
222 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 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 |
---|---|---|
@@ -1,10 +1,27 @@ | ||
import Foundation | ||
|
||
extension URLSession { | ||
/// Return a dataTaskPublisher as a `DataPublisher` | ||
public func dataPublisher(for request: URLRequest) -> Session.RequestDataPublisher { | ||
dataTaskPublisher(for: request) | ||
.mapError { $0 as Error } | ||
.eraseToAnyPublisher() | ||
@available(iOS, deprecated: 15.0, message: "Use built-in API instead") | ||
public func data(for urlRequest: URLRequest) async throws -> (Data, URLResponse) { | ||
try await withCheckedThrowingContinuation { promise in | ||
self.dataTask(with: urlRequest) { data, response, error in | ||
if let error = error { | ||
promise.resume(throwing: error) | ||
} | ||
|
||
guard let data = data, let response = response else { | ||
return promise.resume(throwing: URLError(.badServerResponse)) | ||
} | ||
|
||
promise.resume(returning: (data, response)) | ||
} | ||
.resume() | ||
} | ||
} | ||
|
||
public func data(for urlRequest: URLRequest) async throws -> URLDataResponse { | ||
let (data, response) = try await data(for: urlRequest) | ||
|
||
return URLDataResponse(data: data, response: response as! HTTPURLResponse) | ||
} | ||
} |
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 @@ | ||
import Foundation | ||
|
||
public struct URLDataResponse { | ||
public let data: Data | ||
public let response: HTTPURLResponse | ||
} | ||
|
||
extension URLDataResponse { | ||
public func validate(errorDecoder: DataErrorDecoder? = nil) throws { | ||
do { | ||
try response.validateStatusCode() | ||
} | ||
catch let error as HTTPError { | ||
guard let decoder = errorDecoder, !data.isEmpty else { | ||
throw error | ||
} | ||
|
||
throw try decoder(data) | ||
} | ||
} | ||
} |
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,44 @@ | ||
import Foundation | ||
|
||
#if canImport(Combine) | ||
import Combine | ||
|
||
extension Session { | ||
/// Return a publisher performing request and returning `Output` data | ||
/// | ||
/// The request is validated and decoded appropriately on success. | ||
/// - Returns: a Publisher emitting Output on success, an error otherwise | ||
public func publisher<Output: Decodable>(for request: Request<Output>) -> AnyPublisher<Output, Error> { | ||
let subject = PassthroughSubject<Output, Error>() | ||
|
||
Task { | ||
do { | ||
subject.send(try await response(for: request)) | ||
subject.send(completion: .finished) | ||
} | ||
catch { | ||
subject.send(completion: .failure(error)) | ||
} | ||
} | ||
|
||
return subject.eraseToAnyPublisher() | ||
} | ||
|
||
public func publisher(for request: Request<Void>) -> AnyPublisher<Void, Error> { | ||
let subject = PassthroughSubject<Void, Error>() | ||
|
||
Task { | ||
do { | ||
subject.send(try await response(for: request)) | ||
subject.send(completion: .finished) | ||
} | ||
catch { | ||
subject.send(completion: .failure(error)) | ||
} | ||
} | ||
|
||
return subject.eraseToAnyPublisher() | ||
} | ||
} | ||
|
||
#endif |
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
Oops, something went wrong.