Skip to content

Commit

Permalink
Add send optional variant
Browse files Browse the repository at this point in the history
  • Loading branch information
kean committed Dec 29, 2021
1 parent e840251 commit e6e5f77
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Get 0.x

## Get 0.3.0

*Dec 29, 2021*

- Add `send` variant that works with optional types. If the response is empty – return `nil`.

## Get 0.2.1

*Dec 24, 2021*
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public struct Response<T> {
}
```

The response can be any `Decodable` type. And if the response type is `Data`, the client simply returns raw response data. If it's a `String`, it returns the response as plain text.
The response can be any `Decodable` type. The response can also be optional. And if the response type is `Data`, the client simply returns raw response data. If it's a `String`, it returns the response as plain text.

> If you just want to retrieve the response data, you can also call `data(for:)`.

Expand Down
11 changes: 11 additions & 0 deletions Sources/Get/APIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ public actor APIClient {
self.serializer = Serializer(decoder: configuration.decoder, encoder: configuration.encoder)
}

/// Sends the given request and returns a response with a decoded response value.
public func send<T: Decodable>(_ request: Request<T?>) async throws -> Response<T?> {
try await send(request) { data in
if data.isEmpty {
return nil
} else {
return try await self.serializer.decode(data)
}
}
}

/// Sends the given request and returns a response with a decoded response value.
public func send<T: Decodable>(_ request: Request<T>) async throws -> Response<T> {
try await send(request) { data in
Expand Down
4 changes: 2 additions & 2 deletions Sources/Get/Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ final class DataLoader: NSObject, URLSessionDataDelegate {
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
guard let handler = handlers[task] else { return }
handlers[task] = nil
if let data = handler.data, let response = task.response, error == nil {
handler.completion(.success((data, response, handler.metrics)))
if let response = task.response, error == nil {
handler.completion(.success((handler.data ?? Data(), response, handler.metrics)))
} else {
handler.completion(.failure(error ?? URLError(.unknown)))
}
Expand Down
27 changes: 27 additions & 0 deletions Tests/GetTests/ClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,33 @@ final class APIClientTests: XCTestCase {
// THEN returns decoded JSON
XCTAssertEqual(user.login, "kean")
}

// func value(for:) -> Decodable
func testResponseDecodableOptional() async throws {
// GIVEN
let url = URL(string: "https://api.github.com/user")!
Mock(url: url, dataType: .html, statusCode: 200, data: [
.get: Data()
]).register()

// WHEN
let user: User? = try await client.send(.get("/user")).value

// THEN returns decoded JSON
XCTAssertNil(user)
}

// func value(for:) -> Decodable
func testResponseEmpty() async throws {
// GIVEN
let url = URL(string: "https://api.github.com/user")!
Mock(url: url, dataType: .html, statusCode: 200, data: [
.get: Data()
]).register()

// WHEN
try await client.send(.get("/user")).value
}

// func value(for:) -> Data
func testResponseData() async throws {
Expand Down

0 comments on commit e6e5f77

Please sign in to comment.