From afe1abfb9954ba67554d71633bce9c21714f9c4e Mon Sep 17 00:00:00 2001 From: kean Date: Wed, 29 Dec 2021 18:24:20 -0500 Subject: [PATCH] Impove optional send --- CHANGELOG.md | 6 ++++++ Sources/Get/APIClient.swift | 22 ++++++++++++---------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c4353b3..f79fc2c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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* diff --git a/Sources/Get/APIClient.swift b/Sources/Get/APIClient.swift index 1f7c751..5add496 100644 --- a/Sources/Get/APIClient.swift +++ b/Sources/Get/APIClient.swift @@ -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(_ request: Request) async throws -> Response { - 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(_ 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) } }