diff --git a/Sources/AmiiboService/Internal/Protocols/APIClient.swift b/Sources/AmiiboService/Internal/Protocols/APIClient.swift index c018758..49e40da 100644 --- a/Sources/AmiiboService/Internal/Protocols/APIClient.swift +++ b/Sources/AmiiboService/Internal/Protocols/APIClient.swift @@ -17,6 +17,7 @@ protocol APIClient { // MARK: Functions +#if swift(>=6.0) /// Gets a list of amiibo items based on a given filter. /// - Parameter filter: A filter to remove unwanted items from the result. /// - Returns: A list of filtered amiibo items. @@ -51,5 +52,41 @@ protocol APIClient { /// - Returns: A last updated date. /// - Throws: An ``AmiiboServiceError`` error in case some issue is encountered while generating the result. func getLastUpdated() async throws(AmiiboServiceError) -> Date +#else + /// Gets a list of amiibo items based on a given filter. + /// - Parameter filter: A filter to remove unwanted items from the result. + /// - Returns: A list of filtered amiibo items. + /// - Throws: An ``AmiiboServiceError`` error in case some issue is encountered while generating the result. + func getAmiibos(by filter: AmiiboFilter) async throws -> [Amiibo] + /// Gets a list of amiibo series based on a given filter. + /// - Parameter filter: A filter to remove unwanted items from the result. + /// - Returns: A list of filtered amiibo series. + /// - Throws: An ``AmiiboServiceError`` error in case some issue is encountered while generating the result. + func getAmiiboSeries(by filter: AmiiboSeriesFilter) async throws -> [AmiiboSeries] + + /// Gets a list of amiibo types based on a given filter. + /// - Parameter filter: A filter to remove unwanted items from the result. + /// - Returns: A list of filtered amiibo types. + /// - Throws: An ``AmiiboServiceError`` error in case some issue is encountered while generating the result. + func getAmiiboTypes(by filter: AmiiboTypeFilter) async throws -> [AmiiboType] + + /// Gets a list of game characters based on a given filter. + /// - Parameter filter: A filter to remove unwanted items from the result. + /// - Returns: A list of filtered game characters. + /// - Throws: An ``AmiiboServiceError`` error in case some issue is encountered while generating the result. + func getGameCharacters(by filter: GameCharacterFilter) async throws -> [GameCharacter] + + /// Gets a list of game series based on a given filter. + /// - Parameter filter: A filter to remove unwanted items from the result. + /// - Returns: A list of filtered game series. + /// - Throws: An ``AmiiboServiceError`` error in case some issue is encountered while generating the result. + func getGameSeries(by filter: GameSeriesFilter) async throws -> [GameSeries] + + /// Gets the date when the data was last updated. + /// - Returns: A last updated date. + /// - Throws: An ``AmiiboServiceError`` error in case some issue is encountered while generating the result. + func getLastUpdated() async throws -> Date +#endif + } diff --git a/Sources/AmiiboService/Public/Clients/AmiiboLiveClient.swift b/Sources/AmiiboService/Public/Clients/AmiiboLiveClient.swift index 7994a7d..1cc938e 100644 --- a/Sources/AmiiboService/Public/Clients/AmiiboLiveClient.swift +++ b/Sources/AmiiboService/Public/Clients/AmiiboLiveClient.swift @@ -43,6 +43,7 @@ extension AmiiboLiveClient: APIClient { // MARK: Functions +#if swift(>=6.0) /// Gets a list of amiibo items based on a given filter. /// - Parameter filter: A filter to remove unwanted items from the result. /// - Returns: A list of filtered amiibo items. @@ -277,6 +278,242 @@ extension AmiiboLiveClient: APIClient { throw AmiiboServiceError.undocumented(statusCode) } } +#else + /// Gets a list of amiibo items based on a given filter. + /// - Parameter filter: A filter to remove unwanted items from the result. + /// - Returns: A list of filtered amiibo items. + /// - Throws: An ``AmiiboServiceError`` error in case some issue is encountered while generating the result. + public func getAmiibos( + by filter: AmiiboFilter + ) async throws -> [Amiibo] { + let response: Operations.getAmiibos.Output + + do { + response = try await client.getAmiibos( + .init(query: .init( + amiiboSeries: filter.series, + character: filter.gameCharacter, + gameseries: filter.gameSeries, + id: filter.identifier, + name: filter.name, + showgames: filter.showGames, + showusage: filter.showUsage, + _type: filter.type + )) + ) + } catch let error as ClientError { + if error.underlyingError is DecodingError { + throw AmiiboServiceError.decoding + } else { + throw AmiiboServiceError.unknown + } + } catch { + throw AmiiboServiceError.unknown + } + + switch response { + case let .ok(ok): + switch ok.body { + case let .json(output): + return map(output) + } + + case .badRequest: + throw AmiiboServiceError.badRequest + + case let .undocumented(statusCode, _): + throw AmiiboServiceError.undocumented(statusCode) + } + } + + /// Gets a list of amiibo series based on a given filter. + /// - Parameter filter: A filter to remove unwanted items from the result. + /// - Returns: A list of filtered amiibo series. + /// - Throws: An ``AmiiboServiceError`` error in case some issue is encountered while generating the result. + public func getAmiiboSeries( + by filter: AmiiboSeriesFilter + ) async throws -> [AmiiboSeries] { + let response: Operations.getAmiiboSeries.Output + + do { + response = try await client.getAmiiboSeries( + .init(query: .init( + key: filter.key, + name: filter.name + )) + ) + } catch { + throw AmiiboServiceError.unknown + } + + switch response { + case let .ok(ok): + switch ok.body { + case let .json(output): + return map(output, as: AmiiboSeries.self) + } + + case .badRequest: + throw AmiiboServiceError.badRequest + + case .internalServerError: + throw AmiiboServiceError.notAvailable + + case .notFound: + throw AmiiboServiceError.notFound + + case let .undocumented(statusCode, _): + throw AmiiboServiceError.undocumented(statusCode) + } + } + + /// Gets a list of amiibo types based on a given filter. + /// - Parameter filter: A filter to remove unwanted items from the result. + /// - Returns: A list of filtered amiibo types. + /// - Throws: An ``AmiiboServiceError`` error in case some issue is encountered while generating the result. + public func getAmiiboTypes( + by filter: AmiiboTypeFilter + ) async throws -> [AmiiboType] { + let response: Operations.getAmiiboTypes.Output + + do { + response = try await client.getAmiiboTypes( + .init(query: .init( + key: filter.key, + name: filter.name + )) + ) + } catch { + throw AmiiboServiceError.unknown + } + + switch response { + case let .ok(ok): + switch ok.body { + case let .json(output): + return map(output, as: AmiiboType.self) + } + + case .badRequest: + throw AmiiboServiceError.badRequest + + case .internalServerError: + throw AmiiboServiceError.notAvailable + + case .notFound: + throw AmiiboServiceError.notFound + + case let .undocumented(statusCode, _): + throw AmiiboServiceError.undocumented(statusCode) + } + } + + /// Gets a list of game characters based on a given filter. + /// - Parameter filter: A filter to remove unwanted items from the result. + /// - Returns: A list of filtered game characters. + /// - Throws: An ``AmiiboServiceError`` error in case some issue is encountered while generating the result. + public func getGameCharacters( + by filter: GameCharacterFilter + ) async throws -> [GameCharacter] { + let response: Operations.getGameCharacters.Output + + do { + response = try await client.getGameCharacters( + .init(query: .init( + key: filter.key, + name: filter.name + )) + ) + } catch { + throw AmiiboServiceError.unknown + } + + switch response { + case let .ok(ok): + switch ok.body { + case let .json(output): + return map(output, as: GameCharacter.self) + } + + case .badRequest: + throw AmiiboServiceError.badRequest + + case .internalServerError: + throw AmiiboServiceError.notAvailable + + case .notFound: + throw AmiiboServiceError.notFound + + case let .undocumented(statusCode, _): + throw AmiiboServiceError.undocumented(statusCode) + } + } + + /// Gets a list of game series based on a given filter. + /// - Parameter filter: A filter to remove unwanted items from the result. + /// - Returns: A list of filtered game series. + /// - Throws: An ``AmiiboServiceError`` error in case some issue is encountered while generating the result. + public func getGameSeries( + by filter: GameSeriesFilter + ) async throws -> [GameSeries] { + let response: Operations.getGameSeries.Output + + do { + response = try await client.getGameSeries( + .init(query: .init( + key: filter.key, + name: filter.name + )) + ) + } catch { + throw AmiiboServiceError.unknown + } + + switch response { + case let .ok(ok): + switch ok.body { + case let .json(output): + return map(output, as: GameSeries.self) + } + + case .badRequest: + throw AmiiboServiceError.badRequest + + case .internalServerError: + throw AmiiboServiceError.notAvailable + + case .notFound: + throw AmiiboServiceError.notFound + + case let .undocumented(statusCode, _): + throw AmiiboServiceError.undocumented(statusCode) + } + } + + /// Gets the date when the data was last updated. + /// - Returns: A last updated date. + /// - Throws: An ``AmiiboServiceError`` error in case some issue is encountered while generating the result. + public func getLastUpdated() async throws -> Date { + let response: Operations.getLastUpdated.Output + + do { + response = try await client.getLastUpdated() + } catch { + throw AmiiboServiceError.unknown + } + + switch response { + case let .ok(ok): + switch ok.body { + case let .json(output): + return output.lastUpdated + } + + case let .undocumented(statusCode, _): + throw AmiiboServiceError.undocumented(statusCode) + } + } +#endif } diff --git a/Sources/AmiiboService/Public/Clients/AmiiboMockClient.swift b/Sources/AmiiboService/Public/Clients/AmiiboMockClient.swift index e3c5c17..789e77f 100644 --- a/Sources/AmiiboService/Public/Clients/AmiiboMockClient.swift +++ b/Sources/AmiiboService/Public/Clients/AmiiboMockClient.swift @@ -76,6 +76,7 @@ extension AmiiboMockClient: APIClient { // MARK: Functions +#if swift(>=6.0) /// Gets a list of amiibo items based on a given filter. /// - Parameter filter: A filter to remove unwanted items from the result. /// - Returns: A list of filtered amiibo items. @@ -158,7 +159,90 @@ extension AmiiboMockClient: APIClient { return lastUpdated } +#else + /// Gets a list of amiibo items based on a given filter. + /// - Parameter filter: A filter to remove unwanted items from the result. + /// - Returns: A list of filtered amiibo items. + /// - Throws: An ``AmiiboServiceError`` error in case some issue is encountered while generating the result. + public func getAmiibos(by filter: AmiiboFilter) async throws -> [Amiibo] { + try throwErrorIfExists() + + guard let amiibos else { + throw AmiiboServiceError.notFound + } + + return amiibos + } + /// Gets a list of amiibo series based on a given filter. + /// - Parameter filter: A filter to remove unwanted items from the result. + /// - Returns: A list of filtered amiibo series. + /// - Throws: An ``AmiiboServiceError`` error in case some issue is encountered while generating the result. + public func getAmiiboSeries(by filter: AmiiboSeriesFilter) async throws -> [AmiiboSeries] { + try throwErrorIfExists() + + guard let amiiboSeries else { + throw AmiiboServiceError.notFound + } + + return amiiboSeries + } + + /// Gets a list of amiibo types based on a given filter. + /// - Parameter filter: A filter to remove unwanted items from the result. + /// - Returns: A list of filtered amiibo types. + /// - Throws: An ``AmiiboServiceError`` error in case some issue is encountered while generating the result. + public func getAmiiboTypes(by filter: AmiiboTypeFilter) async throws -> [AmiiboType] { + try throwErrorIfExists() + + guard let amiiboTypes else { + throw AmiiboServiceError.notFound + } + + return amiiboTypes + } + + /// Gets a list of game characters based on a given filter. + /// - Parameter filter: A filter to remove unwanted items from the result. + /// - Returns: A list of filtered game characters. + /// - Throws: An ``AmiiboServiceError`` error in case some issue is encountered while generating the result. + public func getGameCharacters(by filter: GameCharacterFilter) async throws -> [GameCharacter] { + try throwErrorIfExists() + + guard let gameCharacters else { + throw AmiiboServiceError.notFound + } + + return gameCharacters + } + + /// Gets a list of game series based on a given filter. + /// - Parameter filter: A filter to remove unwanted items from the result. + /// - Returns: A list of filtered game series. + /// - Throws: An ``AmiiboServiceError`` error in case some issue is encountered while generating the result. + public func getGameSeries(by filter: GameSeriesFilter) async throws -> [GameSeries] { + try throwErrorIfExists() + + guard let gameSeries else { + throw AmiiboServiceError.notFound + } + + return gameSeries + } + + /// Gets the date when the data was last updated. + /// - Returns: A last updated date. + /// - Throws: An ``AmiiboServiceError`` error in case some issue is encountered while generating the result. + public func getLastUpdated() async throws -> Date { + try throwErrorIfExists() + + guard let lastUpdated else { + throw AmiiboServiceError.notFound + } + + return lastUpdated + } +#endif } diff --git a/Sources/AmiiboService/Public/Services/AmiiboService.swift b/Sources/AmiiboService/Public/Services/AmiiboService.swift index 8fea32e..fdb947f 100644 --- a/Sources/AmiiboService/Public/Services/AmiiboService.swift +++ b/Sources/AmiiboService/Public/Services/AmiiboService.swift @@ -32,7 +32,8 @@ public struct AmiiboService { } // MARK: Functions - + +#if swift(>=6.0) /// Gets a list of amiibo items based on a given filter. /// - Parameter filter: A filter to remove unwanted items from the result. /// - Returns: A list of filtered amiibo items. @@ -89,5 +90,63 @@ public struct AmiiboService { public func getLastUpdated() async throws(AmiiboServiceError) -> Date { try await client.getLastUpdated() } +#else + /// Gets a list of amiibo items based on a given filter. + /// - Parameter filter: A filter to remove unwanted items from the result. + /// - Returns: A list of filtered amiibo items. + /// - Throws: An ``AmiiboServiceError`` error in case some issue is encountered while generating the result. + public func getAmiibos( + _ filter: AmiiboFilter = .init() + ) async throws -> [Amiibo] { + try await client.getAmiibos(by: filter) + } + + /// Gets a list of amiibo series based on a given filter. + /// - Parameter filter: A filter to remove unwanted items from the result. + /// - Returns: A list of filtered amiibo series. + /// - Throws: An ``AmiiboServiceError`` error in case some issue is encountered while generating the result. + public func getAmiiboSeries( + _ filter: AmiiboSeriesFilter = .init() + ) async throws -> [AmiiboSeries] { + try await client.getAmiiboSeries(by: filter) + } + + /// Gets a list of amiibo types based on a given filter. + /// - Parameter filter: A filter to remove unwanted items from the result. + /// - Returns: A list of filtered amiibo types. + /// - Throws: An ``AmiiboServiceError`` error in case some issue is encountered while generating the result. + public func getAmiiboTypes( + _ filter: AmiiboTypeFilter = .init() + ) async throws -> [AmiiboType] { + try await client.getAmiiboTypes(by: filter) + } + + /// Gets a list of game characters based on a given filter. + /// - Parameter filter: A filter to remove unwanted items from the result. + /// - Returns: A list of filtered game characters. + /// - Throws: An ``AmiiboServiceError`` error in case some issue is encountered while generating the result. + public func getGameCharacters( + _ filter: GameCharacterFilter = .init() + ) async throws -> [GameCharacter] { + try await client.getGameCharacters(by: filter) + } + + /// Gets a list of game series based on a given filter. + /// - Parameter filter: A filter to remove unwanted items from the result. + /// - Returns: A list of filtered game series. + /// - Throws: An ``AmiiboServiceError`` error in case some issue is encountered while generating the result. + public func getGameSeries( + _ filter: GameSeriesFilter = .init() + ) async throws -> [GameSeries] { + try await client.getGameSeries(by: filter) + } + + /// Gets the date when the data was last updated. + /// - Returns: A last updated date. + /// - Throws: An ``AmiiboServiceError`` error in case some issue is encountered while generating the result. + public func getLastUpdated() async throws -> Date { + try await client.getLastUpdated() + } +#endif } diff --git a/Tests/AmiiboService/Tests/Public/Services/AmiiboServiceLiveTests.swift b/Tests/AmiiboService/Tests/Public/Services/AmiiboServiceLiveTests.swift index 25d4774..908c379 100644 --- a/Tests/AmiiboService/Tests/Public/Services/AmiiboServiceLiveTests.swift +++ b/Tests/AmiiboService/Tests/Public/Services/AmiiboServiceLiveTests.swift @@ -21,7 +21,7 @@ struct AmiiboServiceLiveTests { private let service: AmiiboService - // MARK: Initialisers + // MARK: Initializers init() { self.service = .init(.live())