Skip to content

Commit 318b239

Browse files
authored
URLRoutingClient updates (#21)
* Rename `URLRoutingClient.request(_:...)` to `decodedResponse(for:)` * Add `URLRoutingClient.data(for:)` * fix * wip * wip
1 parent 29b8720 commit 318b239

File tree

5 files changed

+44
-17
lines changed

5 files changed

+44
-17
lines changed

Sources/URLRouting/Client/Client.swift

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import XCTestDynamicOverlay
66
import FoundationNetworking
77
#endif
88

9-
/// A type that can make requests to a server, download the response, and decode the response
10-
/// into a model.
9+
/// A type that can make requests to a server, download the response, and decode the response into a
10+
/// model.
1111
///
1212
/// You do not typically construct this type directly from its initializer, and instead use the
13-
/// ``live(router:session:)`` static method for creating an API client from a parser-printer, or
14-
/// use the ``failing`` static variable for creating an API client that throws an error when a
15-
/// request is made and then use ``override(_:with:)-1ot4o`` to override certain routes with mocked
13+
/// ``live(router:session:)`` static method for creating an API client from a parser-printer, or use
14+
/// the ``failing`` static variable for creating an API client that throws an error when a request
15+
/// is made and then use ``override(_:with:)-1ot4o`` to override certain routes with mocked
1616
/// responses.
1717
public struct URLRoutingClient<Route> {
1818
var request: (Route) async throws -> (Data, URLResponse)
@@ -26,6 +26,15 @@ public struct URLRoutingClient<Route> {
2626
self.decoder = decoder
2727
}
2828

29+
/// Makes a request to a route.
30+
///
31+
/// - Parameter route: The route to request.
32+
/// - Returns: The data and response.
33+
@available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *)
34+
public func data(for route: Route) async throws -> (value: Data, response: URLResponse) {
35+
try await self.request(route)
36+
}
37+
2938
/// Makes a request to a route.
3039
///
3140
/// - Parameters:
@@ -34,12 +43,12 @@ public struct URLRoutingClient<Route> {
3443
/// - decoder: A JSON decoder.
3544
/// - Returns: The decoded value.
3645
@available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *)
37-
public func request<Value: Decodable>(
38-
_ route: Route,
46+
public func decodedResponse<Value: Decodable>(
47+
for route: Route,
3948
as type: Value.Type = Value.self,
4049
decoder: JSONDecoder? = nil
4150
) async throws -> (value: Value, response: URLResponse) {
42-
let (data, response) = try await self.request(route)
51+
let (data, response) = try await self.data(for: route)
4352
do {
4453
return (try (decoder ?? self.decoder).decode(type, from: data), response)
4554
} catch {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import Foundation
2+
3+
#if canImport(FoundationNetworking)
4+
import FoundationNetworking
5+
#endif
6+
7+
// NB: Deprecated after 0.1.0:
8+
9+
extension URLRoutingClient {
10+
@available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *)
11+
@available(*, deprecated, renamed: "responseData(for:as:decoder:)")
12+
public func request<Value: Decodable>(
13+
_ route: Route,
14+
as type: Value.Type = Value.self,
15+
decoder: JSONDecoder? = nil
16+
) async throws -> (value: Value, response: URLResponse) {
17+
try await self.decodedResponse(for: route, as: type, decoder: decoder)
18+
}
19+
}

Sources/URLRouting/Scheme.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
/// ...
99
/// }
1010
/// ```
11+
///
12+
/// > Note: Do not use the `Scheme` parser for the purpose of preferring to print a particular
13+
/// > scheme from your router. Instead, consider using ``BaseURLPrinter`` via the `baseURL` and
14+
/// > `baseRequestData` methods on routers.
1115
public struct Scheme: ParserPrinter {
1216
@usableFromInline
1317
let name: String
@@ -18,11 +22,6 @@ public struct Scheme: ParserPrinter {
1822
/// A parser of the `https` scheme.
1923
public static let https = Self("https")
2024

21-
/// A parser of custom schemes.
22-
public static func custom(_ scheme: String) -> Self {
23-
Self(scheme)
24-
}
25-
2625
/// Initializes a scheme parser with a scheme name.
2726
///
2827
/// - Parameter name: A method name.

Tests/URLRoutingTests/URLRoutingClientTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class URLRoutingClientTests: XCTestCase {
1919
let sut = URLRoutingClient<AppRoute>(request: { _ in
2020
("{\"decodableValue\":\"result\"}".data(using: .utf8)!, URLResponse())
2121
})
22-
let response = try await sut.request(.test, as: Response.self)
22+
let response = try await sut.decodedResponse(for: .test, as: Response.self)
2323
XCTAssertEqual(response.value, .init(decodableValue: "result"))
2424
}
2525
@available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *)
@@ -36,7 +36,7 @@ class URLRoutingClientTests: XCTestCase {
3636
request: { _ in
3737
("{\"decodable_value\":\"result\"}".data(using: .utf8)!, URLResponse())
3838
}, decoder: customDecoder)
39-
let response = try await sut.request(.test, as: Response.self)
39+
let response = try await sut.decodedResponse(for: .test, as: Response.self)
4040
XCTAssertEqual(response.value, .init(decodableValue: "result"))
4141
}
4242
@available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *)
@@ -53,7 +53,7 @@ class URLRoutingClientTests: XCTestCase {
5353
request: { _ in
5454
("{\"decodableValue\":\"result\"}".data(using: .utf8)!, URLResponse())
5555
}, decoder: customDecoder)
56-
let response = try await sut.request(.test, as: Response.self, decoder: .init())
56+
let response = try await sut.decodedResponse(for: .test, as: Response.self, decoder: .init())
5757
XCTAssertEqual(response.value, .init(decodableValue: "result"))
5858
}
5959
#endif

Tests/URLRoutingTests/URLRoutingTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class URLRoutingTests: XCTestCase {
6060

6161
let name = try p.parse(&request)
6262
XCTAssertEqual("Hello", name)
63-
XCTAssertEqual(["X-Haha": ["Blob"]], request.headers)
63+
XCTAssertEqual(["x-haha": ["Blob"]], request.headers)
6464
}
6565

6666
func testQuery() throws {

0 commit comments

Comments
 (0)