Skip to content

Commit

Permalink
URLRoutingClient updates (#21)
Browse files Browse the repository at this point in the history
* Rename `URLRoutingClient.request(_:...)` to `decodedResponse(for:)`

* Add `URLRoutingClient.data(for:)`

* fix

* wip

* wip
  • Loading branch information
stephencelis authored May 20, 2022
1 parent 29b8720 commit 318b239
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 17 deletions.
25 changes: 17 additions & 8 deletions Sources/URLRouting/Client/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import XCTestDynamicOverlay
import FoundationNetworking
#endif

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

/// Makes a request to a route.
///
/// - Parameter route: The route to request.
/// - Returns: The data and response.
@available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *)
public func data(for route: Route) async throws -> (value: Data, response: URLResponse) {
try await self.request(route)
}

/// Makes a request to a route.
///
/// - Parameters:
Expand All @@ -34,12 +43,12 @@ public struct URLRoutingClient<Route> {
/// - decoder: A JSON decoder.
/// - Returns: The decoded value.
@available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *)
public func request<Value: Decodable>(
_ route: Route,
public func decodedResponse<Value: Decodable>(
for route: Route,
as type: Value.Type = Value.self,
decoder: JSONDecoder? = nil
) async throws -> (value: Value, response: URLResponse) {
let (data, response) = try await self.request(route)
let (data, response) = try await self.data(for: route)
do {
return (try (decoder ?? self.decoder).decode(type, from: data), response)
} catch {
Expand Down
19 changes: 19 additions & 0 deletions Sources/URLRouting/Internal/Deprecations.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import Foundation

#if canImport(FoundationNetworking)
import FoundationNetworking
#endif

// NB: Deprecated after 0.1.0:

extension URLRoutingClient {
@available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *)
@available(*, deprecated, renamed: "responseData(for:as:decoder:)")
public func request<Value: Decodable>(
_ route: Route,
as type: Value.Type = Value.self,
decoder: JSONDecoder? = nil
) async throws -> (value: Value, response: URLResponse) {
try await self.decodedResponse(for: route, as: type, decoder: decoder)
}
}
9 changes: 4 additions & 5 deletions Sources/URLRouting/Scheme.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
/// ...
/// }
/// ```
///
/// > Note: Do not use the `Scheme` parser for the purpose of preferring to print a particular
/// > scheme from your router. Instead, consider using ``BaseURLPrinter`` via the `baseURL` and
/// > `baseRequestData` methods on routers.
public struct Scheme: ParserPrinter {
@usableFromInline
let name: String
Expand All @@ -18,11 +22,6 @@ public struct Scheme: ParserPrinter {
/// A parser of the `https` scheme.
public static let https = Self("https")

/// A parser of custom schemes.
public static func custom(_ scheme: String) -> Self {
Self(scheme)
}

/// Initializes a scheme parser with a scheme name.
///
/// - Parameter name: A method name.
Expand Down
6 changes: 3 additions & 3 deletions Tests/URLRoutingTests/URLRoutingClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class URLRoutingClientTests: XCTestCase {
let sut = URLRoutingClient<AppRoute>(request: { _ in
("{\"decodableValue\":\"result\"}".data(using: .utf8)!, URLResponse())
})
let response = try await sut.request(.test, as: Response.self)
let response = try await sut.decodedResponse(for: .test, as: Response.self)
XCTAssertEqual(response.value, .init(decodableValue: "result"))
}
@available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *)
Expand All @@ -36,7 +36,7 @@ class URLRoutingClientTests: XCTestCase {
request: { _ in
("{\"decodable_value\":\"result\"}".data(using: .utf8)!, URLResponse())
}, decoder: customDecoder)
let response = try await sut.request(.test, as: Response.self)
let response = try await sut.decodedResponse(for: .test, as: Response.self)
XCTAssertEqual(response.value, .init(decodableValue: "result"))
}
@available(iOS 13, macOS 10.15, tvOS 13, watchOS 6, *)
Expand All @@ -53,7 +53,7 @@ class URLRoutingClientTests: XCTestCase {
request: { _ in
("{\"decodableValue\":\"result\"}".data(using: .utf8)!, URLResponse())
}, decoder: customDecoder)
let response = try await sut.request(.test, as: Response.self, decoder: .init())
let response = try await sut.decodedResponse(for: .test, as: Response.self, decoder: .init())
XCTAssertEqual(response.value, .init(decodableValue: "result"))
}
#endif
Expand Down
2 changes: 1 addition & 1 deletion Tests/URLRoutingTests/URLRoutingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class URLRoutingTests: XCTestCase {

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

func testQuery() throws {
Expand Down

0 comments on commit 318b239

Please sign in to comment.