Improved the "getAmiibos(by: )" function for the AmiiboLiveClient client in the package target.

This commit is contained in:
Javier Cicchelli 2024-09-15 00:15:06 +02:00
parent 4a8f402036
commit 420ed8f7a3
3 changed files with 30 additions and 15 deletions

View File

@ -11,6 +11,7 @@
//===----------------------------------------------------------------------===
import Foundation
import OpenAPIRuntime
import OpenAPIURLSession
public struct AmiiboLiveClient {
@ -38,7 +39,9 @@ extension AmiiboLiveClient: APIClient {
// MARK: Functions
public func getAmiibos(by filter: AmiiboFilter) async throws -> [Amiibo] {
let response = try await client.getAmiibos(
let response = try await {
do {
return try await client.getAmiibos(
.init(query: .init(
amiiboSeries: filter.series,
character: filter.gameCharacter,
@ -50,6 +53,16 @@ extension AmiiboLiveClient: APIClient {
_type: filter.type
))
)
} catch let error as ClientError {
guard let _ = error.underlyingError as? DecodingError else {
throw AmiiboServiceError.unknown
}
throw AmiiboServiceError.decoding
} catch {
throw AmiiboServiceError.unknown
}
}()
switch response {
case let .ok(ok):

View File

@ -12,9 +12,11 @@
public enum AmiiboServiceError: Error {
case badRequest
case decoding
case notAvailable
case notFound
case undocumented(_ statusCode: Int)
case unknown
}
// MARK: - Equatable

View File

@ -67,7 +67,7 @@ struct AmiiboServiceLiveTests {
// WHEN
// THEN
await #expect(throws: AmiiboServiceError.undocumented(404)) {
await #expect(throws: AmiiboServiceError.decoding) {
try await service.getAmiibos(.init(identifier: identifier))
}
}
@ -79,7 +79,7 @@ struct AmiiboServiceLiveTests {
// WHEN
// THEN
await #expect(throws: AmiiboServiceError.undocumented(404)) {
await #expect(throws: AmiiboServiceError.decoding) {
try await service.getAmiibos(.init(identifier: identifier))
}
}