Several fixes and optimizations all over the library (#28)
This PR contains the work done to cleans up the library after the Swift 6.2 migration: drops the legacy conditional-compilation paths, hardens the OpenAPI specification, removes boilerplate from the live client, and makes the live test suite resilient to changes in the upstream service data.
* Swift 6.2 baseline
* Adopted swift-tools-version: 6.2, which allowed removing every #if swift(>=6.0) / #if swift(>=6.2) branch and natural-language test names are now the only code paths.
* `AmiiboClient` client now refines `Sendable`, and both `AmiiboLiveClient` and `AmiiboService` conform to it, so instances can be shared across concurrency domains.
* Live client
* Extracted a generic `perform(_:)` helper that wraps every generated API call and funnels transport errors through the existing error mapper — replaces six repeated do/catch blocks and lets the private fetch* methods use typed throws end-to-end.
* Added the internal `KeyNamePayload` protocol and a generic makeModels(from:), collapsing the four near-identical mapping/sorting blocks for amiibo series, types, game characters and game series into one.
* The server URL is now resolved once into a static let constant instead of on every initialization.
* Improved error mapping: URLError.cancelled → .cancelled, .cannotParseResponse → .decoding, and .dataNotAllowed, .internationalRoamingOff, .secureConnectionFailed now map to .notAvailable.
* Simplified the Amiibo.Platform initializer ([Amiibo+Platform.swift](vscode-webview://1m6rk4uhpaukb4hrbp3j54p77iq693fq2ql7o9oup2pca1omf7f6/Sources/AmiiboService/Public/Models/Amiibo/Amiibo+Platform.swift)) using optional chaining and ?? [] instead of nested guards and immediately-invoked closures.
* Set the en_US_POSIX locale on both fixed-format date formatters so the user's locale or 12/24-hour setting can no longer break parsing.
* OpenAPI specification
* Replaced the shared Tuple schema with explicit per-type schemas (AmiiboSeries, AmiiboType, GameCharacter, GameSeries), so the generated code exposes real properties instead of allOf wrappers with .value1 accessors.
* Extracted named list schemas (AmiiboList, AmiiboSeriesList, …) for the wrapper payloads, which turns the generated .case2 enum cases into readable .AmiiboSeriesList cases.
* Added validation patterns and length bounds to the id, head, tail and key query parameters, and collapsed the redundant pattern/minLength/maxLength triplets on head/tail into ^[0-9a-fA-F]{8}$.
* Extracted the shared InternalServerError response, replacing the five inline 500 descriptions.
* Documented the two non-standard date/time decoding behaviours (date-only strings for release dates, offset-less timestamps for lastUpdated) and why the optional amiibo wrapper property must stay optional.
* Corrected the info.version value from v1.0.0 to 1.0.0.
* ⚠️ Breaking changes
* Minimum Swift version raised from 5.10 to 6.2.
* `AmiiboServiceError.unknown` now carries a String description of the underlying error.
* `AmiiboClient` requires Sendable conformance, so existing custom mock clients must be Sendable.
Reviewed-on: #28
Co-authored-by: Javier Cicchelli <javier@rock-n-code.com>
Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
This commit was merged in pull request #28.
This commit is contained in:
@@ -17,8 +17,23 @@ import OpenAPIRuntime
|
||||
import OpenAPIURLSession
|
||||
|
||||
/// A type that implements a live client to the [Amiibo API](https://www.amiiboapi.org) online service.
|
||||
///
|
||||
/// This client maps any transport error or unsuccessful HTTP response to an ``AmiiboServiceError`` error, and sorts the items of the list responses in ascending order by identifier or key.
|
||||
public struct AmiiboLiveClient: Sendable {
|
||||
|
||||
// MARK: Constants
|
||||
|
||||
/// The base URL of the live service, resolved once from the server defined in the OpenAPI specification.
|
||||
///
|
||||
/// The validity of the URL is guaranteed by the bundled `openapi.yaml` specification and enforced by a unit test, so resolution is not expected to fail at runtime.
|
||||
static let serverURL: URL = {
|
||||
guard let url = try? Servers.Server1.url() else {
|
||||
fatalError("The server URL defined in the OpenAPI specification could not be resolved. Verify that the 'openapi.yaml' server definition is valid.")
|
||||
}
|
||||
|
||||
return url
|
||||
}()
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
/// A client generated by the OpenAPI Runtime library to perform API calls.
|
||||
@@ -29,12 +44,8 @@ public struct AmiiboLiveClient: Sendable {
|
||||
/// Initializes this client with a transport for performing HTTP operations.
|
||||
/// - Parameter transport: A transport that performs HTTP operations. Defaults to a `URLSessionTransport` using the shared session.
|
||||
public init(transport: any ClientTransport = URLSessionTransport()) {
|
||||
guard let serverURL = try? Servers.Server1.url() else {
|
||||
fatalError("The server URL defined in the OpenAPI specification could not be resolved. Verify that the 'openapi.yaml' server definition is valid.")
|
||||
}
|
||||
|
||||
self.client = .init(
|
||||
serverURL: serverURL,
|
||||
serverURL: Self.serverURL,
|
||||
configuration: .init(dateTranscoder: ISODateTimeTranscoder()),
|
||||
transport: transport
|
||||
)
|
||||
@@ -49,7 +60,6 @@ extension AmiiboLiveClient: AmiiboClient {
|
||||
|
||||
// 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.
|
||||
@@ -101,69 +111,11 @@ extension AmiiboLiveClient: AmiiboClient {
|
||||
}
|
||||
|
||||
/// Gets the date when the data was last updated.
|
||||
/// - Returns: A last updated date.
|
||||
/// - Returns: A last updated date, decoded as UTC.
|
||||
/// - Throws: An ``AmiiboServiceError`` error in case some issue is encountered while generating the result.
|
||||
public func getLastUpdated() async throws(AmiiboServiceError) -> Date {
|
||||
try await fetchLastUpdated()
|
||||
}
|
||||
#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 await fetchAmiibos(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(
|
||||
by filter: AmiiboSeriesFilter
|
||||
) async throws -> [AmiiboSeries] {
|
||||
try await fetchAmiiboSeries(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(
|
||||
by filter: AmiiboTypeFilter
|
||||
) async throws -> [AmiiboType] {
|
||||
try await fetchAmiiboTypes(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(
|
||||
by filter: GameCharacterFilter
|
||||
) async throws -> [GameCharacter] {
|
||||
try await fetchGameCharacters(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(
|
||||
by filter: GameSeriesFilter
|
||||
) async throws -> [GameSeries] {
|
||||
try await fetchGameSeries(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 fetchLastUpdated()
|
||||
}
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
@@ -179,11 +131,9 @@ private extension AmiiboLiveClient {
|
||||
/// - Throws: An ``AmiiboServiceError`` error in case some issue is encountered while generating the result.
|
||||
func fetchAmiibos(
|
||||
_ filter: AmiiboFilter
|
||||
) async throws -> [Amiibo] {
|
||||
let response: Operations.getAmiibos.Output
|
||||
|
||||
do {
|
||||
response = try await client.getAmiibos(.init(query: .init(
|
||||
) async throws(AmiiboServiceError) -> [Amiibo] {
|
||||
let response = try await perform {
|
||||
try await client.getAmiibos(.init(query: .init(
|
||||
id: filter.identifier,
|
||||
head: filter.head,
|
||||
tail: filter.tail,
|
||||
@@ -195,8 +145,6 @@ private extension AmiiboLiveClient {
|
||||
showgames: filter.showGames,
|
||||
showusage: filter.showUsage
|
||||
)))
|
||||
} catch {
|
||||
try handle(error: error)
|
||||
}
|
||||
|
||||
switch response {
|
||||
@@ -206,11 +154,12 @@ private extension AmiiboLiveClient {
|
||||
switch output.amiibo {
|
||||
case let .Amiibo(object):
|
||||
return [Amiibo(object)]
|
||||
case let .case2(list):
|
||||
case let .AmiiboList(list):
|
||||
return list
|
||||
.map { Amiibo($0) }
|
||||
.sorted { $0.identifier < $1.identifier }
|
||||
case .none:
|
||||
// The service returns `"amiibo": null` when an `id` filter matches nothing.
|
||||
return []
|
||||
}
|
||||
}
|
||||
@@ -231,16 +180,12 @@ private extension AmiiboLiveClient {
|
||||
/// - Throws: An ``AmiiboServiceError`` error in case some issue is encountered while generating the result.
|
||||
func fetchAmiiboSeries(
|
||||
_ filter: AmiiboSeriesFilter
|
||||
) async throws -> [AmiiboSeries] {
|
||||
let response: Operations.getAmiiboSeries.Output
|
||||
|
||||
do {
|
||||
response = try await client.getAmiiboSeries(.init(query: .init(
|
||||
) async throws(AmiiboServiceError) -> [AmiiboSeries] {
|
||||
let response = try await perform {
|
||||
try await client.getAmiiboSeries(.init(query: .init(
|
||||
key: filter.key,
|
||||
name: filter.name
|
||||
)))
|
||||
} catch {
|
||||
try handle(error: error)
|
||||
}
|
||||
|
||||
switch response {
|
||||
@@ -249,11 +194,9 @@ private extension AmiiboLiveClient {
|
||||
case let .json(output):
|
||||
switch output.amiibo {
|
||||
case let .AmiiboSeries(payload):
|
||||
return [AmiiboSeries(payload.value1)]
|
||||
case let .case2(list):
|
||||
return list
|
||||
.map { AmiiboSeries($0.value1) }
|
||||
.sorted { $0.key < $1.key }
|
||||
return makeModels(from: [payload])
|
||||
case let .AmiiboSeriesList(list):
|
||||
return makeModels(from: list)
|
||||
}
|
||||
}
|
||||
case .badRequest:
|
||||
@@ -273,16 +216,12 @@ private extension AmiiboLiveClient {
|
||||
/// - Throws: An ``AmiiboServiceError`` error in case some issue is encountered while generating the result.
|
||||
func fetchAmiiboTypes(
|
||||
_ filter: AmiiboTypeFilter
|
||||
) async throws -> [AmiiboType] {
|
||||
let response: Operations.getAmiiboTypes.Output
|
||||
|
||||
do {
|
||||
response = try await client.getAmiiboTypes(.init(query: .init(
|
||||
) async throws(AmiiboServiceError) -> [AmiiboType] {
|
||||
let response = try await perform {
|
||||
try await client.getAmiiboTypes(.init(query: .init(
|
||||
key: filter.key,
|
||||
name: filter.name
|
||||
)))
|
||||
} catch {
|
||||
try handle(error: error)
|
||||
}
|
||||
|
||||
switch response {
|
||||
@@ -291,11 +230,9 @@ private extension AmiiboLiveClient {
|
||||
case let .json(output):
|
||||
switch output.amiibo {
|
||||
case let .AmiiboType(payload):
|
||||
return [AmiiboType(payload.value1)]
|
||||
case let .case2(list):
|
||||
return list
|
||||
.map { AmiiboType($0.value1) }
|
||||
.sorted { $0.key < $1.key }
|
||||
return makeModels(from: [payload])
|
||||
case let .AmiiboTypeList(list):
|
||||
return makeModels(from: list)
|
||||
}
|
||||
}
|
||||
case .badRequest:
|
||||
@@ -315,16 +252,12 @@ private extension AmiiboLiveClient {
|
||||
/// - Throws: An ``AmiiboServiceError`` error in case some issue is encountered while generating the result.
|
||||
func fetchGameCharacters(
|
||||
_ filter: GameCharacterFilter
|
||||
) async throws -> [GameCharacter] {
|
||||
let response: Operations.getGameCharacters.Output
|
||||
|
||||
do {
|
||||
response = try await client.getGameCharacters(.init(query: .init(
|
||||
) async throws(AmiiboServiceError) -> [GameCharacter] {
|
||||
let response = try await perform {
|
||||
try await client.getGameCharacters(.init(query: .init(
|
||||
key: filter.key,
|
||||
name: filter.name
|
||||
)))
|
||||
} catch {
|
||||
try handle(error: error)
|
||||
}
|
||||
|
||||
switch response {
|
||||
@@ -333,11 +266,9 @@ private extension AmiiboLiveClient {
|
||||
case let .json(output):
|
||||
switch output.amiibo {
|
||||
case let .GameCharacter(payload):
|
||||
return [GameCharacter(payload.value1)]
|
||||
case let .case2(list):
|
||||
return list
|
||||
.map { GameCharacter($0.value1) }
|
||||
.sorted { $0.key < $1.key }
|
||||
return makeModels(from: [payload])
|
||||
case let .GameCharacterList(list):
|
||||
return makeModels(from: list)
|
||||
}
|
||||
}
|
||||
case .badRequest:
|
||||
@@ -357,16 +288,12 @@ private extension AmiiboLiveClient {
|
||||
/// - Throws: An ``AmiiboServiceError`` error in case some issue is encountered while generating the result.
|
||||
func fetchGameSeries(
|
||||
_ filter: GameSeriesFilter
|
||||
) async throws -> [GameSeries] {
|
||||
let response: Operations.getGameSeries.Output
|
||||
|
||||
do {
|
||||
response = try await client.getGameSeries(.init(query: .init(
|
||||
) async throws(AmiiboServiceError) -> [GameSeries] {
|
||||
let response = try await perform {
|
||||
try await client.getGameSeries(.init(query: .init(
|
||||
key: filter.key,
|
||||
name: filter.name
|
||||
)))
|
||||
} catch {
|
||||
try handle(error: error)
|
||||
}
|
||||
|
||||
switch response {
|
||||
@@ -375,11 +302,9 @@ private extension AmiiboLiveClient {
|
||||
case let .json(output):
|
||||
switch output.amiibo {
|
||||
case let .GameSeries(payload):
|
||||
return [GameSeries(payload.value1)]
|
||||
case let .case2(list):
|
||||
return list
|
||||
.map { GameSeries($0.value1) }
|
||||
.sorted { $0.key < $1.key }
|
||||
return makeModels(from: [payload])
|
||||
case let .GameSeriesList(list):
|
||||
return makeModels(from: list)
|
||||
}
|
||||
}
|
||||
case .badRequest:
|
||||
@@ -394,15 +319,11 @@ private extension AmiiboLiveClient {
|
||||
}
|
||||
|
||||
/// Fetches the date when the data was last updated.
|
||||
/// - Returns: A fetched last updated date.
|
||||
/// - Returns: A fetched last updated date, decoded as UTC.
|
||||
/// - Throws: An ``AmiiboServiceError`` error in case some issue is encountered while generating the result.
|
||||
func fetchLastUpdated() async throws -> Date {
|
||||
let response: Operations.getLastUpdated.Output
|
||||
|
||||
do {
|
||||
response = try await client.getLastUpdated()
|
||||
} catch {
|
||||
try handle(error: error)
|
||||
func fetchLastUpdated() async throws(AmiiboServiceError) -> Date {
|
||||
let response = try await perform {
|
||||
try await client.getLastUpdated()
|
||||
}
|
||||
|
||||
switch response {
|
||||
@@ -418,10 +339,33 @@ private extension AmiiboLiveClient {
|
||||
}
|
||||
}
|
||||
|
||||
/// Performs an API call, mapping any error thrown by the underlying client to an ``AmiiboServiceError`` error.
|
||||
/// - Parameter operation: A closure that performs the API call.
|
||||
/// - Returns: The output of the performed API call.
|
||||
/// - Throws: An ``AmiiboServiceError`` error in case the API call failed.
|
||||
func perform<Output>(
|
||||
_ operation: () async throws -> Output
|
||||
) async throws(AmiiboServiceError) -> Output {
|
||||
do {
|
||||
return try await operation()
|
||||
} catch {
|
||||
try handle(error: error)
|
||||
}
|
||||
}
|
||||
|
||||
/// Maps a list of key-name payloads into a sorted list of models.
|
||||
/// - Parameter payloads: A list of payloads to map into models.
|
||||
/// - Returns: A list of models sorted by their keys in ascending order.
|
||||
func makeModels<Model: KeyNameModel>(from payloads: [some KeyNamePayload]) -> [Model] {
|
||||
payloads
|
||||
.map { Model($0) }
|
||||
.sorted { $0.key < $1.key }
|
||||
}
|
||||
|
||||
/// Maps a given error to an ``AmiiboServiceError`` error.
|
||||
/// - Parameter error: An error to map.
|
||||
/// - Throws: An ``AmiiboServiceError`` error that corresponds to the given error.
|
||||
func handle(error: any Error) throws -> Never {
|
||||
func handle(error: any Error) throws(AmiiboServiceError) -> Never {
|
||||
switch error {
|
||||
case is CancellationError:
|
||||
throw AmiiboServiceError.cancelled
|
||||
@@ -431,21 +375,28 @@ private extension AmiiboLiveClient {
|
||||
throw AmiiboServiceError.decoding
|
||||
case let urlError as URLError:
|
||||
switch urlError.code {
|
||||
case .cancelled:
|
||||
throw AmiiboServiceError.cancelled
|
||||
case .cannotParseResponse:
|
||||
throw AmiiboServiceError.decoding
|
||||
case .cannotFindHost,
|
||||
.cannotConnectToHost,
|
||||
.dataNotAllowed,
|
||||
.dnsLookupFailed,
|
||||
.internationalRoamingOff,
|
||||
.networkConnectionLost,
|
||||
.notConnectedToInternet,
|
||||
.secureConnectionFailed,
|
||||
.timedOut:
|
||||
throw AmiiboServiceError.notAvailable
|
||||
default:
|
||||
throw AmiiboServiceError.unknown
|
||||
throw AmiiboServiceError.unknown(String(describing: urlError))
|
||||
}
|
||||
default:
|
||||
throw AmiiboServiceError.unknown
|
||||
throw AmiiboServiceError.unknown(String(describing: clientError.underlyingError))
|
||||
}
|
||||
default:
|
||||
throw AmiiboServiceError.unknown
|
||||
throw AmiiboServiceError.unknown(String(describing: error))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -28,8 +28,8 @@ public enum AmiiboServiceError: Error {
|
||||
case notFound
|
||||
/// The server returned an undocumented HTTP status code.
|
||||
case undocumented(_ statusCode: Int)
|
||||
/// An unexpected error that does not fall into any other category.
|
||||
case unknown
|
||||
/// An unexpected error that does not fall into any other category, with a description of the underlying error.
|
||||
case unknown(_ description: String)
|
||||
}
|
||||
|
||||
// MARK: - Equatable
|
||||
@@ -50,7 +50,7 @@ extension AmiiboServiceError: LocalizedError {
|
||||
case .notAvailable: "The backend service is currently unreachable due to a network or server issue."
|
||||
case .notFound: "No results were found matching the given filter criteria."
|
||||
case .undocumented(let statusCode): "The server returned an undocumented HTTP status code: \(statusCode)."
|
||||
case .unknown: "An unexpected error occurred."
|
||||
case .unknown(let description): "An unexpected error occurred: \(description)"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -47,30 +47,18 @@ extension Amiibo {
|
||||
_ threeDS: [Components.Schemas.AmiiboGame]?,
|
||||
_ wiiU: [Components.Schemas.AmiiboGame]?
|
||||
) {
|
||||
guard (`switch` != nil && `switch`?.isEmpty == false)
|
||||
|| (switch2 != nil && switch2?.isEmpty == false)
|
||||
|| (threeDS != nil && threeDS?.isEmpty == false)
|
||||
|| (wiiU != nil && wiiU?.isEmpty == false)
|
||||
guard `switch`?.isEmpty == false
|
||||
|| switch2?.isEmpty == false
|
||||
|| threeDS?.isEmpty == false
|
||||
|| wiiU?.isEmpty == false
|
||||
else {
|
||||
return nil
|
||||
}
|
||||
|
||||
self.switch = {
|
||||
guard let `switch` else { return [] }
|
||||
return `switch`.map { .init($0) }
|
||||
}()
|
||||
self.switch2 = {
|
||||
guard let switch2 else { return [] }
|
||||
return switch2.map { .init($0) }
|
||||
}()
|
||||
self.threeDS = {
|
||||
guard let threeDS else { return [] }
|
||||
return threeDS.map { .init($0) }
|
||||
}()
|
||||
self.wiiU = {
|
||||
guard let wiiU else { return [] }
|
||||
return wiiU.map { .init($0) }
|
||||
}()
|
||||
self.switch = `switch`?.map { .init($0) } ?? []
|
||||
self.switch2 = switch2?.map { .init($0) } ?? []
|
||||
self.threeDS = threeDS?.map { .init($0) } ?? []
|
||||
self.wiiU = wiiU?.map { .init($0) } ?? []
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,6 +16,8 @@ import Foundation
|
||||
|
||||
extension Amiibo {
|
||||
/// A model that represents the regional release dates of an amiibo.
|
||||
///
|
||||
/// The service provides these dates as date-only values, which are decoded as midnight UTC.
|
||||
public struct Release: Sendable, Hashable {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
@@ -27,7 +27,7 @@ public struct AmiiboSeries: KeyNameModel {
|
||||
|
||||
/// Initializes this model from a given payload.
|
||||
/// - Parameter payload: A payload that contains the values for the model.
|
||||
init(_ payload: Components.Schemas.Tuple) {
|
||||
init(_ payload: some KeyNamePayload) {
|
||||
self.key = payload.key
|
||||
self.name = payload.name
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ public struct AmiiboType: KeyNameModel {
|
||||
|
||||
/// Initializes this model from a given payload.
|
||||
/// - Parameter payload: A payload that contains the values for the model.
|
||||
init(_ payload: Components.Schemas.Tuple) {
|
||||
init(_ payload: some KeyNamePayload) {
|
||||
self.key = payload.key
|
||||
self.name = payload.name
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ public struct GameCharacter: KeyNameModel {
|
||||
|
||||
/// Initializes this model from a given payload.
|
||||
/// - Parameter payload: A payload that contains the values for the model.
|
||||
init(_ payload: Components.Schemas.Tuple) {
|
||||
init(_ payload: some KeyNamePayload) {
|
||||
self.key = payload.key
|
||||
self.name = payload.name
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ public struct GameSeries: KeyNameModel {
|
||||
|
||||
/// Initializes this model from a given payload.
|
||||
/// - Parameter payload: A payload that contains the values for the model.
|
||||
init(_ payload: Components.Schemas.Tuple) {
|
||||
init(_ payload: some KeyNamePayload) {
|
||||
self.key = payload.key
|
||||
self.name = payload.name
|
||||
}
|
||||
|
||||
@@ -15,11 +15,12 @@
|
||||
import Foundation
|
||||
|
||||
/// A protocol that defines API clients containing all available endpoints to interact with.
|
||||
public protocol AmiiboClient {
|
||||
///
|
||||
/// Conforming types must be `Sendable`, as clients are expected to be used safely across concurrency domains.
|
||||
public protocol AmiiboClient: Sendable {
|
||||
|
||||
// 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,44 +52,8 @@ public protocol AmiiboClient {
|
||||
func getGameSeries(by filter: GameSeriesFilter) async throws(AmiiboServiceError) -> [GameSeries]
|
||||
|
||||
/// Gets the date when the data was last updated.
|
||||
/// - Returns: A last updated date.
|
||||
/// - Returns: A last updated date, decoded as UTC.
|
||||
/// - 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
|
||||
|
||||
}
|
||||
|
||||
@@ -15,7 +15,9 @@
|
||||
import Foundation
|
||||
|
||||
/// A type that implements the service that uses a client to make calls.
|
||||
public struct AmiiboService {
|
||||
///
|
||||
/// This service forwards every call to the ``AmiiboClient`` client injected during initialization, which defaults to an ``AmiiboLiveClient`` instance. This type is `Sendable`, so an instance can be safely shared across concurrency domains.
|
||||
public struct AmiiboService: Sendable {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
@@ -32,7 +34,6 @@ 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.
|
||||
@@ -84,68 +85,10 @@ public struct AmiiboService {
|
||||
}
|
||||
|
||||
/// Gets the date when the data was last updated.
|
||||
/// - Returns: A last updated date.
|
||||
/// - Returns: A last updated date, decoded as UTC.
|
||||
/// - Throws: An ``AmiiboServiceError`` error in case some issue is encountered while generating the result.
|
||||
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
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user