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 Foundation
import OpenAPIRuntime
import OpenAPIURLSession import OpenAPIURLSession
public struct AmiiboLiveClient { public struct AmiiboLiveClient {
@ -38,19 +39,31 @@ extension AmiiboLiveClient: APIClient {
// MARK: Functions // MARK: Functions
public func getAmiibos(by filter: AmiiboFilter) async throws -> [Amiibo] { public func getAmiibos(by filter: AmiiboFilter) async throws -> [Amiibo] {
let response = try await client.getAmiibos( let response = try await {
.init(query: .init( do {
amiiboSeries: filter.series, return try await client.getAmiibos(
character: filter.gameCharacter, .init(query: .init(
gameseries: filter.gameSeries, amiiboSeries: filter.series,
id: filter.identifier, character: filter.gameCharacter,
name: filter.name, gameseries: filter.gameSeries,
showgames: filter.showGames, id: filter.identifier,
showusage: filter.showUsage, name: filter.name,
_type: filter.type showgames: filter.showGames,
)) showusage: filter.showUsage,
) _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 { switch response {
case let .ok(ok): case let .ok(ok):
switch ok.body { switch ok.body {

View File

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

View File

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