Skip to content

Commit

Permalink
Impove optional send
Browse files Browse the repository at this point in the history
  • Loading branch information
kean committed Dec 29, 2021
1 parent e6e5f77 commit afe1abf
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 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.1

*Dec 29, 2021*

- The new optional `send()` variant now also supports `String`, and `Data`.

## Get 0.3.0

*Dec 29, 2021*
Expand Down
22 changes: 12 additions & 10 deletions Sources/Get/APIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,24 @@ public actor APIClient {
if data.isEmpty {
return nil
} else {
return try await self.serializer.decode(data)
return try await self.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
if T.self == Data.self {
return data as! T
} else if T.self == String.self {
guard let string = String(data: data, encoding: .utf8) else { throw URLError(.badServerResponse) }
return string as! T
} else {
return try await self.serializer.decode(data)
}
try await send(request, decode)
}

private func decode<T: Decodable>(_ data: Data) async throws -> T {
if T.self == Data.self {
return data as! T
} else if T.self == String.self {
guard let string = String(data: data, encoding: .utf8) else { throw URLError(.badServerResponse) }
return string as! T
} else {
return try await self.serializer.decode(data)
}
}

Expand Down

0 comments on commit afe1abf

Please sign in to comment.