diff --git a/Sources/AmiiboService/Public/Clients/AmiiboLiveClient.swift b/Sources/AmiiboService/Public/Clients/AmiiboLiveClient.swift index 545019f..1b96582 100644 --- a/Sources/AmiiboService/Public/Clients/AmiiboLiveClient.swift +++ b/Sources/AmiiboService/Public/Clients/AmiiboLiveClient.swift @@ -181,16 +181,16 @@ private extension AmiiboLiveClient { do { response = try await client.getAmiibos(.init(query: .init( + id: filter.identifier, + head: filter.head, + tail: filter.tail, + name: filter.name, + _type: filter.type, amiiboSeries: filter.series, character: filter.gameCharacter, gameseries: filter.gameSeries, - head: filter.head, - id: filter.identifier, - name: filter.name, showgames: filter.showGames, - showusage: filter.showUsage, - tail: filter.tail, - _type: filter.type + showusage: filter.showUsage ))) } catch { try handle(error: error) @@ -200,7 +200,16 @@ private extension AmiiboLiveClient { case let .ok(ok): switch ok.body { case let .json(output): - return map(output) + switch output.amiibo { + case let .Amiibo(object): + return [Amiibo(object)] + case let .AmiiboList(list): + return list + .map { Amiibo($0) } + .sorted { $0.identifier < $1.identifier } + case .none: + return [] + } } case .badRequest: throw AmiiboServiceError.badRequest @@ -235,7 +244,14 @@ private extension AmiiboLiveClient { case let .ok(ok): switch ok.body { case let .json(output): - return map(output) + switch output.amiibo { + case let .AmiiboSeries(payload): + return [AmiiboSeries(payload.value1)] + case let .AmiiboSeriesList(list): + return list + .map { AmiiboSeries($0.value1) } + .sorted { $0.key < $1.key } + } } case .badRequest: throw AmiiboServiceError.badRequest @@ -270,7 +286,14 @@ private extension AmiiboLiveClient { case let .ok(ok): switch ok.body { case let .json(output): - return map(output) + switch output.amiibo { + case let .AmiiboType(payload): + return [AmiiboType(payload.value1)] + case let .AmiiboTypeList(list): + return list + .map { AmiiboType($0.value1) } + .sorted { $0.key < $1.key } + } } case .badRequest: throw AmiiboServiceError.badRequest @@ -305,7 +328,14 @@ private extension AmiiboLiveClient { case let .ok(ok): switch ok.body { case let .json(output): - return map(output) + switch output.amiibo { + case let .GameCharacter(payload): + return [GameCharacter(payload.value1)] + case let .GameCharacterList(list): + return list + .map { GameCharacter($0.value1) } + .sorted { $0.key < $1.key } + } } case .badRequest: throw AmiiboServiceError.badRequest @@ -340,7 +370,14 @@ private extension AmiiboLiveClient { case let .ok(ok): switch ok.body { case let .json(output): - return map(output) + switch output.amiibo { + case let .GameSeries(payload): + return [GameSeries(payload.value1)] + case let .GameSeriesList(list): + return list + .map { GameSeries($0.value1) } + .sorted { $0.key < $1.key } + } } case .badRequest: throw AmiiboServiceError.badRequest @@ -371,10 +408,6 @@ private extension AmiiboLiveClient { case let .json(output): return output.lastUpdated } - case .badRequest: - throw AmiiboServiceError.badRequest - case .notFound: - throw AmiiboServiceError.notFound case .internalServerError: throw AmiiboServiceError.notAvailable case let .undocumented(statusCode, _): @@ -413,36 +446,4 @@ private extension AmiiboLiveClient { } } - /// Retrieves a list of amiibo items from a wrapper container. - /// - Parameter wrapper: A wrapper container that either has an object or a list of items. - /// - Returns: A list of amiibo items, sorted by identifiers. - func map( - _ wrapper: Components.Schemas.AmiiboWrapper - ) -> [Amiibo] { - switch wrapper.amiibo { - case let .Amiibo(object): - return [Amiibo(object)] - case let .AmiiboList(list): - return list - .map { Amiibo($0) } - .sorted { $0.identifier < $1.identifier } - } - } - - /// Retrieves a list of items that conforms to the `KeyNameModel` protocol from a wrapper container. - /// - Parameter wrapper: A wrapper container that either has an object or a list of items. - /// - Returns: A list of items that conforms to the `KeyNameModel` protocol, sorted by keys. - func map( - _ wrapper: Components.Schemas.TupleWrapper - ) -> [Model] { - switch wrapper.amiibo { - case let .Tuple(payload): - return [Model(payload)] - case let .TupleList(list): - return list - .map { Model($0) } - .sorted { $0.key < $1.key } - } - } - } diff --git a/Tests/AmiiboService/Tests/Public/Services/AmiiboServiceLiveTests.swift b/Tests/AmiiboService/Tests/Public/Services/AmiiboServiceLiveTests.swift index e4452ce..040e48e 100644 --- a/Tests/AmiiboService/Tests/Public/Services/AmiiboServiceLiveTests.swift +++ b/Tests/AmiiboService/Tests/Public/Services/AmiiboServiceLiveTests.swift @@ -552,6 +552,8 @@ enum Input { .init(tail: "00000002"), .init(tail: "0002"), .init(identifier: "0000000000000002"), + .init(identifier: "0000000000000000"), + .init(identifier: "0000000"), .init(name: "zelda"), .init(name: "Something"), .init(name: "zel"), @@ -587,8 +589,6 @@ enum Input { static let amiibosThrows: [AmiiboFilter] = [ .init(head: .empty), .init(tail: .empty), - .init(identifier: "0000000000000000"), - .init(identifier: "0000000"), .init(identifier: .empty), .init(type: "0x"), .init(series: "0x"), @@ -659,9 +659,9 @@ enum Input { enum Output { /// A list of number of items that are expected from the `assertAmiibos` assertion. - static let amiibos: [Int] = [.totalAmiibos, 7, 7, 1, 1, 1, 5, .zero, 7, .totalAmiibos, 235, 235, .zero, .zero, .zero, .zero, 96, 26, .zero, .zero, 63, .totalAmiibos, 12, 6, .zero, .zero, .zero, .totalAmiibos, 49, 32, .zero, .zero, 147, .totalAmiibos, .totalAmiibos, .totalAmiibos] + static let amiibos: [Int] = [.totalAmiibos, 7, 7, 1, 1, 1, .zero, .zero, 5, .zero, 7, .totalAmiibos, 235, 235, .zero, .zero, .zero, .zero, 96, 26, .zero, .zero, 63, .totalAmiibos, 12, 6, .zero, .zero, .zero, .totalAmiibos, 49, 32, .zero, .zero, 147, .totalAmiibos, .totalAmiibos, .totalAmiibos] /// A list of errors are expected to be thrown from the `assertAmiibosThrows` assertion. - static let amiibosThrows: [AmiiboServiceError] = [.badRequest, .badRequest, .decoding, .decoding, .badRequest, .badRequest, .badRequest, .badRequest, .badRequest] + static let amiibosThrows: [AmiiboServiceError] = [.badRequest, .badRequest, .badRequest, .badRequest, .badRequest, .badRequest, .badRequest] /// A list of number of items that are expected from the `assertAmiiboSeries` assertion. static let amiiboSeries: [Int] = [.totalAmiiboSeries, 1, 1, 1, .totalAmiiboSeries] /// A list of errors are expected to be thrown from the `assertAmiiboSeriesThrows` assertion.