Skip to content

Commit 0dcb35e

Browse files
committed
public recoverResult
upd Plugins.Basic with concurrency
1 parent 66c2b1f commit 0dcb35e

File tree

6 files changed

+22
-22
lines changed

6 files changed

+22
-22
lines changed

Source/Extensions/Result+Recover.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Foundation
22

3-
internal extension Result where Failure: Error {
3+
public extension Result where Failure: Error {
44
/// Returns the original result if successful, or a default value if the error is recoverable.
55
///
66
/// - Parameter defaultValue: The value to use when recovering from a recoverable error.

Source/Managers/Request/AnyRequest.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public extension AnyRequest {
5757
/// - decoder: The JSON decoder or decoding strategy.
5858
/// - keyPath: Optional key path for decoding nested values.
5959
/// - Returns: A `TypedRequest` instance that decodes the response.
60-
func decode<T>(_ type: T.Type = T.self, with decoder: JSONDecoder, keyPath: DecodableKeyPath<T> = []) -> TypedRequest<T>
60+
func decode<T>(_: T.Type = T.self, with decoder: JSONDecoder, keyPath: DecodableKeyPath<T> = []) -> TypedRequest<T>
6161
where T: Decodable {
6262
return .init(anyRequest: self, decoder: DecodableContent<T>(decoder: { decoder }, keyPath: keyPath))
6363
}
@@ -69,7 +69,7 @@ public extension AnyRequest {
6969
/// - decoder: The JSON decoder or decoding strategy.
7070
/// - keyPath: Optional key path for decoding nested values.
7171
/// - Returns: A `TypedRequest` instance that decodes the response.
72-
func decode<T>(_ type: T.Type = T.self, with decoder: JSONDecoding? = nil, keyPath: DecodableKeyPath<T> = []) -> TypedRequest<T>
72+
func decode<T>(_: T.Type = T.self, with decoder: JSONDecoding? = nil, keyPath: DecodableKeyPath<T> = []) -> TypedRequest<T>
7373
where T: Decodable {
7474
return .init(anyRequest: self, decoder: DecodableContent<T>(decoder: decoder, keyPath: keyPath))
7575
}
@@ -81,7 +81,7 @@ public extension AnyRequest {
8181
/// - decoder: The JSON decoder or decoding strategy.
8282
/// - keyPath: Optional key path for decoding nested values.
8383
/// - Returns: A `TypedRequest` instance that decodes the response.
84-
func decode<T>(_ type: T.Type = T.self, with decoder: JSONDecoder, keyPath: DecodableKeyPath<T> = []) -> TypedRequest<T>
84+
func decode<T>(_: T.Type = T.self, with decoder: JSONDecoder, keyPath: DecodableKeyPath<T> = []) -> TypedRequest<T>
8585
where T: Decodable & ExpressibleByNilLiteral {
8686
return .init(anyRequest: self, decoder: DecodableContent<T>(decoder: { decoder }, keyPath: keyPath))
8787
}
@@ -93,7 +93,7 @@ public extension AnyRequest {
9393
/// - decoder: The JSON decoder or decoding strategy.
9494
/// - keyPath: Optional key path for decoding nested values.
9595
/// - Returns: A `TypedRequest` instance that decodes the response.
96-
func decode<T>(_ type: T.Type = T.self, with decoder: JSONDecoding? = nil, keyPath: DecodableKeyPath<T> = []) -> TypedRequest<T>
96+
func decode<T>(_: T.Type = T.self, with decoder: JSONDecoding? = nil, keyPath: DecodableKeyPath<T> = []) -> TypedRequest<T>
9797
where T: Decodable & ExpressibleByNilLiteral {
9898
return .init(anyRequest: self, decoder: DecodableContent<T>(decoder: decoder, keyPath: keyPath))
9999
}
@@ -105,7 +105,7 @@ public extension AnyRequest {
105105
/// - decoder: The decoding strategy or JSON decoder.
106106
/// - keyPath: Optional key path for nested decoding.
107107
/// - Returns: A result containing the decoded value or an error.
108-
func decodeAsync<T>(_ type: T.Type = T.self, with decoder: JSONDecoding? = nil, keyPath: DecodableKeyPath<T> = []) async -> Result<T, Error>
108+
func decodeAsync<T>(_: T.Type = T.self, with decoder: JSONDecoding? = nil, keyPath: DecodableKeyPath<T> = []) async -> Result<T, Error>
109109
where T: Decodable {
110110
return await TypedRequest<T>(anyRequest: self, decoder: DecodableContent<T>(decoder: decoder, keyPath: keyPath)).async()
111111
}
@@ -118,7 +118,7 @@ public extension AnyRequest {
118118
/// - keyPath: Optional key path for nested decoding.
119119
/// - Throws: An error if decoding fails.
120120
/// - Returns: The decoded result.
121-
func decodeAsyncWithThrowing<T>(_ type: T.Type = T.self, with decoder: JSONDecoding? = nil, keyPath: DecodableKeyPath<T> = []) async throws -> T
121+
func decodeAsyncWithThrowing<T>(_: T.Type = T.self, with decoder: JSONDecoding? = nil, keyPath: DecodableKeyPath<T> = []) async throws -> T
122122
where T: Decodable {
123123
return try await TypedRequest<T>(anyRequest: self, decoder: DecodableContent<T>(decoder: decoder, keyPath: keyPath)).asyncWithThrowing()
124124
}
@@ -130,7 +130,7 @@ public extension AnyRequest {
130130
/// - decoder: The decoding strategy or JSON decoder.
131131
/// - keyPath: Optional key path for nested decoding.
132132
/// - Returns: A result containing the decoded value or an error.
133-
func decodeAsync<T>(_ type: T.Type = T.self, with decoder: JSONDecoding? = nil, keyPath: DecodableKeyPath<T> = []) async -> Result<T, Error>
133+
func decodeAsync<T>(_: T.Type = T.self, with decoder: JSONDecoding? = nil, keyPath: DecodableKeyPath<T> = []) async -> Result<T, Error>
134134
where T: Decodable & ExpressibleByNilLiteral {
135135
return await TypedRequest<T>(anyRequest: self, decoder: DecodableContent<T>(decoder: decoder, keyPath: keyPath)).async()
136136
}
@@ -143,7 +143,7 @@ public extension AnyRequest {
143143
/// - keyPath: Optional key path for nested decoding.
144144
/// - Throws: An error if decoding fails.
145145
/// - Returns: The decoded result.
146-
func decodeAsyncWithThrowing<T>(_ type: T.Type = T.self, with decoder: JSONDecoding? = nil, keyPath: DecodableKeyPath<T> = []) async throws -> T
146+
func decodeAsyncWithThrowing<T>(_: T.Type = T.self, with decoder: JSONDecoding? = nil, keyPath: DecodableKeyPath<T> = []) async throws -> T
147147
where T: Decodable & ExpressibleByNilLiteral {
148148
return try await TypedRequest<T>(anyRequest: self, decoder: DecodableContent<T>(decoder: decoder, keyPath: keyPath)).asyncWithThrowing()
149149
}

Source/Managers/Request/DecodableRequestManager.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public extension DecodableRequestManager {
2525
/// - completionQueue: The queue for delivering the result.
2626
/// - completion: A closure called with the decoded result or error.
2727
/// - Returns: A `SmartTasking` instance representing the request.
28-
func request<T>(_ type: T.Type = T.self,
28+
func request<T>(_: T.Type = T.self,
2929
keyPath: DecodableKeyPath<T> = [],
3030
address: Address,
3131
parameters: Parameters = .init(),
@@ -57,7 +57,7 @@ public extension DecodableRequestManager {
5757
/// - completion: A closure called with the decoded result or error.
5858
/// - Returns: A `SmartTasking` instance representing the request.
5959
/// If decoding fails, returns `.success(nil)` instead of an error.
60-
func request<T>(_ type: T.Type = T.self,
60+
func request<T>(_: T.Type = T.self,
6161
keyPath: DecodableKeyPath<T> = [],
6262
address: Address,
6363
parameters: Parameters = .init(),

Source/Plugin/Plugins.Basic.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ public extension Plugins {
2020

2121
#if swift(>=6.0)
2222
/// A closure that provides an `AuthBasicToken` instance or `nil` if credentials are unavailable.
23-
typealias AuthBasicTokenProvider = @Sendable () -> AuthBasicToken?
23+
typealias AuthBasicTokenProvider = @Sendable () async -> AuthBasicToken?
2424
#else
2525
/// A closure that provides an `AuthBasicToken` instance or `nil` if credentials are unavailable.
26-
typealias AuthBasicTokenProvider = () -> AuthBasicToken?
26+
typealias AuthBasicTokenProvider = () async -> AuthBasicToken?
2727
#endif
2828

2929
/// Creates a plugin that adds HTTP Basic Authentication to outgoing requests.
@@ -39,7 +39,7 @@ public extension Plugins {
3939
priority: .authBasic,
4040
type: .header(overrideExisting ? .set("Authorization") : .trySet("Authorization")),
4141
tokenProvider: {
42-
return tokenProvider().map { token in
42+
return await tokenProvider().map { token in
4343
let token = Data("\(token.username):\(token.password)".utf8).base64EncodedString()
4444
return "Basic " + token
4545
}

Tests/Managers/RequestCompletionTests.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,38 +35,38 @@ final class RequestCompletionTests: XCTestCase {
3535
}
3636

3737
func test_complete_queue_completion() {
38-
let _ = subject.complete(in: .async(Queue.main), completion: { _ in })
38+
_ = subject.complete(in: .async(Queue.main), completion: { _ in })
3939
XCTAssertHaveReceived(manager, .requestWithAddress_Parameters_Userinfo_Completionqueue_Completion, with: address, parameters, Argument.anything, DelayedQueue.async(Queue.main), Argument.closure, countSpecifier: .atLeast(1))
4040
}
4141

4242
func test_extension_complete_completion() {
43-
let _ = subject.complete(completion: { _ in })
43+
_ = subject.complete(completion: { _ in })
4444
XCTAssertHaveReceived(manager, .requestWithAddress_Parameters_Userinfo_Completionqueue_Completion, with: address, parameters, Argument.anything, DelayedQueue.async(Queue.main), Argument.closure, countSpecifier: .atLeast(1))
4545
}
4646

4747
func test_extension_oneWay() {
4848
task.stub(.detach).andReturn(task)
4949
task.stub(.deferredStart).andReturn(task)
5050

51-
let _ = subject.oneWay()
51+
_ = subject.oneWay()
5252
XCTAssertHaveReceived(manager, .requestWithAddress_Parameters_Userinfo_Completionqueue_Completion, with: address, parameters, Argument.anything, DelayedQueue.async(Queue.main), Argument.closure, countSpecifier: .atLeast(1))
5353

5454
XCTAssertHaveReceived(task, .detach, countSpecifier: .atLeast(1))
5555
XCTAssertHaveReceived(task, .deferredStart, countSpecifier: .atLeast(1))
5656
}
5757

5858
func test_extension_complete_void() {
59-
let _ = subject.complete(completion: {})
59+
_ = subject.complete(completion: {})
6060
XCTAssertHaveReceived(manager, .requestWithAddress_Parameters_Userinfo_Completionqueue_Completion, with: address, parameters, Argument.anything, DelayedQueue.async(Queue.main), Argument.closure, countSpecifier: .atLeast(1))
6161
}
6262

6363
func test_extension_async() async {
64-
let _ = await subject.async()
64+
_ = await subject.async()
6565
XCTAssertHaveReceived(manager, .requestWithAddress_Parameters_Userinfo, with: address, parameters, Argument.anything, countSpecifier: .atLeast(1))
6666
}
6767

6868
func test_extension_asyncWithThrowing() async throws {
69-
let _ = try? await subject.asyncWithThrowing()
69+
_ = try? await subject.asyncWithThrowing()
7070
XCTAssertHaveReceived(manager, .requestWithAddress_Parameters_Userinfo, with: address, parameters, Argument.anything, countSpecifier: .atLeast(1))
7171
}
7272
}

Tests/Stub/HTTPStubProtocolTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ final class HTTPStubProtocolTests: XCTestCase {
5656
.store(in: &observers)
5757

5858
do {
59-
let _ = try await session.data(for: request)
59+
_ = try await session.data(for: request)
6060
XCTFail("Should throw an error")
6161
} catch {
6262
#if os(watchOS)
@@ -83,7 +83,7 @@ final class HTTPStubProtocolTests: XCTestCase {
8383

8484
func test_no_stub() async {
8585
do {
86-
let _ = try await session.data(for: request)
86+
_ = try await session.data(for: request)
8787
XCTFail("Should throw an error")
8888
} catch {
8989
XCTAssertEqual((error as NSError).domain, "NSURLErrorDomain", error.localizedDescription)

0 commit comments

Comments
 (0)