Few fixes and improvements to the library, plus Github Actions setup (#31)
CI / Build and test (push) Canceled after 0s
Documentation / Build documentation (push) Canceled after 0s
Documentation / Deploy to GitHub Pages (push) Canceled after 0s
CI / Build and test (push) Canceled after 0s
Documentation / Build documentation (push) Canceled after 0s
Documentation / Deploy to GitHub Pages (push) Canceled after 0s
This PR contains the work done to fix a cancellation-mapping bug in `AmiiboLiveClient`, bring the client under offline test coverage, add the conformances SwiftUI and persistence need, and moves CI and documentation publishing to GitHub Actions. All public API changes are additive.
### Fix
- `AmiiboLiveClient` reported an early task cancellation as `.unknown("CancellationError()")`. The OpenAPI runtime wraps every error in `ClientError`, so the `CancellationError` check never matched; it now also checks the wrapped error and throws `.cancelled`.
### Tests
- `ThrowingTransport` and `StubTransport` test doubles let the live client run offline. New tests cover error mapping, HTTP status → error mapping, `oneOf` object-vs-list decoding, `"amiibo": null`, sorting, filter → query encoding, `Platform`/`Release` mapping, and the date transcoder.
- Models gained `package` memberwise initializers so tests can build real fixtures without `@testable`. These are not public API.
- The test target now depends on `OpenAPIRuntime` and `HTTPTypes` (both already in the dependency graph).
### API
- Models are `Codable` and `Identifiable` (`Amiibo.id` is its `identifier`; the key/name models use `key`).
- Filters are `Hashable`.
### Documentation
- Doc comments trimmed library-wide; one repeated `- Throws:` sentence appeared 24 times. `Library.md` and the README tightened to match.
### CI and docs publishing
- `ci.yml`: build and offline tests on PRs and pushes to `main`.
- `live-tests.yml`: weekly and on-demand run against the live API.
- `docs.yml`: builds DocC on PRs, deploys to GitHub Pages on `main`. Pinned to Xcode 26 because swift-docc-plugin 1.5.0 fails to launch its tools under Xcode 27.
- The generated `docs/` folder (327 files) is removed from the repository and ignored.
Reviewed-on: #31
Co-authored-by: Javier Cicchelli <javier@rock-n-code.com>
This commit was merged in pull request #31.
This commit is contained in:
1 parent
44bfaca553
commit
13b9c74a38
361 files changed
+1395
-863
No files matched your search
@@ -6,7 +6,7 @@ A Swift client for the Amiibo API.
|
||||
|
||||
`AmiiboService` fetches data from the [Amiibo API](https://www.amiiboapi.org) and returns it as Swift models.
|
||||
|
||||
The library does not expose a generated client that maps one-to-one to the REST endpoints. Instead, ``AmiiboService`` offers one method per resource, takes a typed filter, returns sorted models, and throws a single ``AmiiboServiceError`` type.
|
||||
``AmiiboService`` offers one method per resource: each takes a typed filter, returns sorted models, and throws ``AmiiboServiceError``.
|
||||
|
||||
## Installation
|
||||
|
||||
@@ -76,9 +76,7 @@ do throws(AmiiboServiceError) {
|
||||
|
||||
## Caching
|
||||
|
||||
The library has no built-in cache. The Amiibo API asks regular consumers to cache responses.
|
||||
|
||||
To cache at the HTTP level, give ``AmiiboLiveClient`` a transport backed by a `URLCache`. The cache follows the server's cache headers:
|
||||
The library has no built-in cache, and the Amiibo API asks regular consumers to cache responses. To cache at the HTTP level, give ``AmiiboLiveClient`` a transport backed by a `URLCache`:
|
||||
|
||||
```swift
|
||||
import OpenAPIURLSession
|
||||
@@ -101,11 +99,11 @@ let service = AmiiboService(
|
||||
)
|
||||
```
|
||||
|
||||
You can also store the returned models in your own application storage.
|
||||
Models are `Codable`, so you can also store them yourself.
|
||||
|
||||
## Testing
|
||||
|
||||
To test without network access, pass a mock ``AmiiboClient`` to ``AmiiboService/init(client:)``. The mock can return stub data or throw ``AmiiboServiceError`` values. ``AmiiboClient`` refines `Sendable`, so the mock must be `Sendable`, too:
|
||||
To test without network access, pass a mock ``AmiiboClient`` to ``AmiiboService/init(client:)``. ``AmiiboClient`` refines `Sendable`, so the mock must be `Sendable`, too:
|
||||
|
||||
```swift
|
||||
import AmiiboService
|
||||
|
||||
@@ -18,13 +18,11 @@ extension DateFormatter {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
/// An ISO date formatter.
|
||||
///
|
||||
/// This formatter implements the `yyyy-MM-dd` date format.
|
||||
/// An ISO date formatter for the `yyyy-MM-dd` format.
|
||||
static let isoDate: DateFormatter = {
|
||||
let formatter = DateFormatter()
|
||||
|
||||
// A fixed-format date requires the POSIX locale, as the user's locale or 12/24-hour setting could otherwise alter the parsing.
|
||||
// The POSIX locale keeps the user's locale and 12/24-hour setting from altering fixed-format parsing.
|
||||
formatter.locale = .init(identifier: "en_US_POSIX")
|
||||
formatter.dateFormat = "yyyy-MM-dd"
|
||||
formatter.timeZone = .init(secondsFromGMT: 0)
|
||||
@@ -32,14 +30,13 @@ extension DateFormatter {
|
||||
return formatter
|
||||
}()
|
||||
|
||||
/// An ISO timestamp formatter.
|
||||
/// An ISO timestamp formatter for the `yyyy-MM-dd'T'HH:mm:ss.SSSSSS` format.
|
||||
///
|
||||
/// This formatter implements the `yyyy-MM-dd'T'HH:mm:ss.SSSSSS` custom date format.
|
||||
/// Within the context of this library, this formatter is solely used to decode a date formatted as a timestamp that is returned by the ``AmiiboService/getLastUpdated()`` function.
|
||||
/// Used only to decode the timestamp returned by ``AmiiboService/getLastUpdated()``.
|
||||
static let isoTimestamp: DateFormatter = {
|
||||
let formatter = DateFormatter()
|
||||
|
||||
// A fixed-format date requires the POSIX locale, as the user's locale or 12/24-hour setting could otherwise alter the parsing.
|
||||
// The POSIX locale keeps the user's locale and 12/24-hour setting from altering fixed-format parsing.
|
||||
formatter.locale = .init(identifier: "en_US_POSIX")
|
||||
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSSSS"
|
||||
formatter.timeZone = .init(secondsFromGMT: 0)
|
||||
|
||||
@@ -12,8 +12,8 @@
|
||||
//
|
||||
// ===----------------------------------------------------------------------===
|
||||
|
||||
/// A protocol that defines filters containing optional `key` and/or `name` values for querying resources.
|
||||
protocol KeyNameFilter {
|
||||
/// A protocol for filters with optional `key` and `name` values.
|
||||
protocol KeyNameFilter: Hashable {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
// ===----------------------------------------------------------------------===
|
||||
|
||||
/// A protocol that defines models containing a `key` and `name` pair.
|
||||
protocol KeyNameModel: Sendable, Hashable {
|
||||
protocol KeyNameModel: Sendable, Hashable, Codable, Identifiable {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
|
||||
@@ -15,15 +15,15 @@
|
||||
import Foundation
|
||||
import OpenAPIRuntime
|
||||
|
||||
/// A type that allows the decoding and encoding of ISO dates, supporting both the `yyyy-MM-dd'T'HH:mm:ss.SSSSSS` timestamp format and the `yyyy-MM-dd` date-only format.
|
||||
/// A transcoder for ISO dates in the `yyyy-MM-dd'T'HH:mm:ss.SSSSSS` timestamp and `yyyy-MM-dd` date-only formats.
|
||||
struct ISODateTimeTranscoder {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
/// A formatter to use to decode and encode ISO timestamp dates.
|
||||
/// The ISO timestamp formatter.
|
||||
private let timestampFormatter: DateFormatter = .isoTimestamp
|
||||
|
||||
/// A formatter to use to decode and encode ISO date-only strings.
|
||||
|
||||
/// The ISO date-only formatter.
|
||||
private let dateFormatter: DateFormatter = .isoDate
|
||||
|
||||
}
|
||||
@@ -34,17 +34,13 @@ extension ISODateTimeTranscoder: DateTranscoder {
|
||||
|
||||
// MARK: Functions
|
||||
|
||||
/// Encodes a date into an ISO timestamp string.
|
||||
/// - Parameter date: A date to encode.
|
||||
/// - Returns: A string representation of the date in `yyyy-MM-dd'T'HH:mm:ss.SSSSSS` format.
|
||||
/// Encodes a date as an ISO timestamp string.
|
||||
func encode(_ date: Date) throws -> String {
|
||||
timestampFormatter.string(from: date)
|
||||
}
|
||||
|
||||
/// Decodes an ISO date string into a date, trying the timestamp format first and falling back to the date-only format.
|
||||
/// - Parameter string: A string to decode.
|
||||
/// - Returns: A date parsed from the string.
|
||||
/// - Throws: A `DecodingError` if the string cannot be parsed into a valid date.
|
||||
/// Decodes an ISO date string, trying the timestamp format before the date-only one.
|
||||
/// - Throws: A `DecodingError` if the string matches neither format.
|
||||
func decode(_ string: String) throws -> Date {
|
||||
if let date = timestampFormatter.date(from: string) {
|
||||
return date
|
||||
|
||||
@@ -16,16 +16,16 @@ import Foundation
|
||||
import OpenAPIRuntime
|
||||
import OpenAPIURLSession
|
||||
|
||||
/// A type that implements a live client to the [Amiibo API](https://www.amiiboapi.org) online service.
|
||||
/// A live client for the [Amiibo API](https://www.amiiboapi.org).
|
||||
///
|
||||
/// 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.
|
||||
/// It maps transport errors and unsuccessful responses to ``AmiiboServiceError``, and sorts list results 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 base URL of the live service, from 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.
|
||||
/// A unit test guards this URL, 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.")
|
||||
@@ -41,8 +41,8 @@ public struct AmiiboLiveClient: Sendable {
|
||||
|
||||
// MARK: Initializers
|
||||
|
||||
/// 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.
|
||||
/// Initializes this client.
|
||||
/// - Parameter transport: A transport for HTTP operations. Defaults to a `URLSessionTransport` on the shared session.
|
||||
public init(transport: any ClientTransport = URLSessionTransport()) {
|
||||
self.client = .init(
|
||||
serverURL: Self.serverURL,
|
||||
@@ -61,9 +61,9 @@ extension AmiiboLiveClient: AmiiboClient {
|
||||
// MARK: Functions
|
||||
|
||||
/// Gets a list of amiibo items based on a given filter.
|
||||
/// - Parameter filter: A filter to remove unwanted items from the result.
|
||||
/// - Parameter filter: A filter for the result.
|
||||
/// - Returns: A list of filtered amiibo items.
|
||||
/// - Throws: An ``AmiiboServiceError`` error in case some issue is encountered while generating the result.
|
||||
/// - Throws: An ``AmiiboServiceError`` if the call fails.
|
||||
public func getAmiibos(
|
||||
by filter: AmiiboFilter
|
||||
) async throws(AmiiboServiceError) -> [Amiibo] {
|
||||
@@ -71,9 +71,9 @@ extension AmiiboLiveClient: AmiiboClient {
|
||||
}
|
||||
|
||||
/// Gets a list of amiibo series based on a given filter.
|
||||
/// - Parameter filter: A filter to remove unwanted items from the result.
|
||||
/// - Parameter filter: A filter for the result.
|
||||
/// - Returns: A list of filtered amiibo series.
|
||||
/// - Throws: An ``AmiiboServiceError`` error in case some issue is encountered while generating the result.
|
||||
/// - Throws: An ``AmiiboServiceError`` if the call fails.
|
||||
public func getAmiiboSeries(
|
||||
by filter: AmiiboSeriesFilter
|
||||
) async throws(AmiiboServiceError) -> [AmiiboSeries] {
|
||||
@@ -81,9 +81,9 @@ extension AmiiboLiveClient: AmiiboClient {
|
||||
}
|
||||
|
||||
/// Gets a list of amiibo types based on a given filter.
|
||||
/// - Parameter filter: A filter to remove unwanted items from the result.
|
||||
/// - Parameter filter: A filter for the result.
|
||||
/// - Returns: A list of filtered amiibo types.
|
||||
/// - Throws: An ``AmiiboServiceError`` error in case some issue is encountered while generating the result.
|
||||
/// - Throws: An ``AmiiboServiceError`` if the call fails.
|
||||
public func getAmiiboTypes(
|
||||
by filter: AmiiboTypeFilter
|
||||
) async throws(AmiiboServiceError) -> [AmiiboType] {
|
||||
@@ -91,9 +91,9 @@ extension AmiiboLiveClient: AmiiboClient {
|
||||
}
|
||||
|
||||
/// Gets a list of game characters based on a given filter.
|
||||
/// - Parameter filter: A filter to remove unwanted items from the result.
|
||||
/// - Parameter filter: A filter for the result.
|
||||
/// - Returns: A list of filtered game characters.
|
||||
/// - Throws: An ``AmiiboServiceError`` error in case some issue is encountered while generating the result.
|
||||
/// - Throws: An ``AmiiboServiceError`` if the call fails.
|
||||
public func getGameCharacters(
|
||||
by filter: GameCharacterFilter
|
||||
) async throws(AmiiboServiceError) -> [GameCharacter] {
|
||||
@@ -101,9 +101,9 @@ extension AmiiboLiveClient: AmiiboClient {
|
||||
}
|
||||
|
||||
/// Gets a list of game series based on a given filter.
|
||||
/// - Parameter filter: A filter to remove unwanted items from the result.
|
||||
/// - Parameter filter: A filter for the result.
|
||||
/// - Returns: A list of filtered game series.
|
||||
/// - Throws: An ``AmiiboServiceError`` error in case some issue is encountered while generating the result.
|
||||
/// - Throws: An ``AmiiboServiceError`` if the call fails.
|
||||
public func getGameSeries(
|
||||
by filter: GameSeriesFilter
|
||||
) async throws(AmiiboServiceError) -> [GameSeries] {
|
||||
@@ -112,7 +112,7 @@ extension AmiiboLiveClient: AmiiboClient {
|
||||
|
||||
/// Gets the date when the data was last updated.
|
||||
/// - Returns: A last updated date, decoded as UTC.
|
||||
/// - Throws: An ``AmiiboServiceError`` error in case some issue is encountered while generating the result.
|
||||
/// - Throws: An ``AmiiboServiceError`` if the call fails.
|
||||
public func getLastUpdated() async throws(AmiiboServiceError) -> Date {
|
||||
try await fetchLastUpdated()
|
||||
}
|
||||
@@ -125,10 +125,7 @@ private extension AmiiboLiveClient {
|
||||
|
||||
// MARK: Functions
|
||||
|
||||
/// Fetches 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 fetched amiibo items filtered, if requested.
|
||||
/// - Throws: An ``AmiiboServiceError`` error in case some issue is encountered while generating the result.
|
||||
/// Fetches the amiibos matching a filter.
|
||||
func fetchAmiibos(
|
||||
_ filter: AmiiboFilter
|
||||
) async throws(AmiiboServiceError) -> [Amiibo] {
|
||||
@@ -174,10 +171,7 @@ private extension AmiiboLiveClient {
|
||||
}
|
||||
}
|
||||
|
||||
/// Fetches 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 fetched amiibo series filtered, if requested.
|
||||
/// - Throws: An ``AmiiboServiceError`` error in case some issue is encountered while generating the result.
|
||||
/// Fetches the amiibo series matching a filter.
|
||||
func fetchAmiiboSeries(
|
||||
_ filter: AmiiboSeriesFilter
|
||||
) async throws(AmiiboServiceError) -> [AmiiboSeries] {
|
||||
@@ -210,10 +204,7 @@ private extension AmiiboLiveClient {
|
||||
}
|
||||
}
|
||||
|
||||
/// Fetches 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 fetched amiibo types filtered, if requested.
|
||||
/// - Throws: An ``AmiiboServiceError`` error in case some issue is encountered while generating the result.
|
||||
/// Fetches the amiibo types matching a filter.
|
||||
func fetchAmiiboTypes(
|
||||
_ filter: AmiiboTypeFilter
|
||||
) async throws(AmiiboServiceError) -> [AmiiboType] {
|
||||
@@ -246,10 +237,7 @@ private extension AmiiboLiveClient {
|
||||
}
|
||||
}
|
||||
|
||||
/// Fetches 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 fetched game characters filtered, if requested.
|
||||
/// - Throws: An ``AmiiboServiceError`` error in case some issue is encountered while generating the result.
|
||||
/// Fetches the game characters matching a filter.
|
||||
func fetchGameCharacters(
|
||||
_ filter: GameCharacterFilter
|
||||
) async throws(AmiiboServiceError) -> [GameCharacter] {
|
||||
@@ -282,10 +270,7 @@ private extension AmiiboLiveClient {
|
||||
}
|
||||
}
|
||||
|
||||
/// Fetches 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 fetched game series filtered, if requested.
|
||||
/// - Throws: An ``AmiiboServiceError`` error in case some issue is encountered while generating the result.
|
||||
/// Fetches the game series matching a filter.
|
||||
func fetchGameSeries(
|
||||
_ filter: GameSeriesFilter
|
||||
) async throws(AmiiboServiceError) -> [GameSeries] {
|
||||
@@ -319,8 +304,6 @@ private extension AmiiboLiveClient {
|
||||
}
|
||||
|
||||
/// Fetches the date when the data was last updated.
|
||||
/// - 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(AmiiboServiceError) -> Date {
|
||||
let response = try await perform {
|
||||
try await client.getLastUpdated()
|
||||
@@ -339,10 +322,7 @@ 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.
|
||||
/// Performs an API call, mapping any error to an ``AmiiboServiceError``.
|
||||
func perform<Output>(
|
||||
_ operation: () async throws -> Output
|
||||
) async throws(AmiiboServiceError) -> Output {
|
||||
@@ -353,24 +333,23 @@ private extension AmiiboLiveClient {
|
||||
}
|
||||
}
|
||||
|
||||
/// 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.
|
||||
/// Maps key-name payloads into models sorted by key.
|
||||
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.
|
||||
/// Maps an error to the corresponding ``AmiiboServiceError``.
|
||||
func handle(error: any Error) throws(AmiiboServiceError) -> Never {
|
||||
switch error {
|
||||
case is CancellationError:
|
||||
throw AmiiboServiceError.cancelled
|
||||
case let clientError as ClientError:
|
||||
// The underlying client wraps every error, including cancellation, in a `ClientError`.
|
||||
switch clientError.underlyingError {
|
||||
case is CancellationError:
|
||||
throw AmiiboServiceError.cancelled
|
||||
case is DecodingError:
|
||||
throw AmiiboServiceError.decoding
|
||||
case let urlError as URLError:
|
||||
|
||||
@@ -22,7 +22,7 @@ public enum AmiiboServiceError: Error {
|
||||
case cancelled
|
||||
/// The response body could not be decoded into the expected model.
|
||||
case decoding
|
||||
/// The backend service is currently unreachable due to a network or server issue.
|
||||
/// The service is unreachable due to a network or server issue.
|
||||
case notAvailable
|
||||
/// No results were found matching the given filter criteria.
|
||||
///
|
||||
@@ -30,7 +30,7 @@ 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, with a description of the underlying error.
|
||||
/// Any other error, with a description of the underlying one.
|
||||
case unknown(_ description: String)
|
||||
}
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
/// A filter for amiibo requests.
|
||||
///
|
||||
/// Values left as `nil` do not restrict the result.
|
||||
public struct AmiiboFilter: Sendable {
|
||||
public struct AmiiboFilter: Sendable, Hashable {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
@@ -37,10 +37,10 @@ public struct AmiiboFilter: Sendable {
|
||||
/// An amiibo series key or name to match, if any.
|
||||
public let series: String?
|
||||
|
||||
/// A flag that indicates whether to include related games in ``Amiibo/platform``. `nil` means `false`.
|
||||
/// Whether to include related games in ``Amiibo/platform``. `nil` means `false`.
|
||||
public let showGames: Bool?
|
||||
|
||||
/// A flag that indicates whether to include related games and their amiibo usages in ``Amiibo/platform``. `nil` means `false`.
|
||||
/// Whether to include related games and their usages in ``Amiibo/platform``. `nil` means `false`.
|
||||
public let showUsage: Bool?
|
||||
|
||||
/// The last 8 hexadecimal characters of an identifier to match, if any. Shorter values match as a prefix.
|
||||
@@ -61,8 +61,8 @@ public struct AmiiboFilter: Sendable {
|
||||
/// - series: An amiibo series key or name to match, if any.
|
||||
/// - gameCharacter: A game character key or name to match, if any.
|
||||
/// - gameSeries: A game series key or name to match, if any.
|
||||
/// - showGames: A flag that indicates whether to include related games in ``Amiibo/platform``. `nil` means `false`.
|
||||
/// - showUsage: A flag that indicates whether to include related games and their amiibo usages in ``Amiibo/platform``. `nil` means `false`.
|
||||
/// - showGames: Whether to include related games in ``Amiibo/platform``. `nil` means `false`.
|
||||
/// - showUsage: Whether to include related games and their usages in ``Amiibo/platform``. `nil` means `false`.
|
||||
public init(
|
||||
head: String? = nil,
|
||||
tail: String? = nil,
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
import Foundation
|
||||
|
||||
/// A model that represents an amiibo.
|
||||
public struct Amiibo: Sendable, Hashable {
|
||||
public struct Amiibo: Sendable, Hashable, Codable, Identifiable {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
@@ -36,7 +36,7 @@ public struct Amiibo: Sendable, Hashable {
|
||||
|
||||
/// The related games of this amiibo, grouped by platform.
|
||||
///
|
||||
/// This value is `nil` unless the request sets ``AmiiboFilter/showGames`` or ``AmiiboFilter/showUsage``, or when the amiibo has no related games.
|
||||
/// `nil` unless the request sets ``AmiiboFilter/showGames`` or ``AmiiboFilter/showUsage``, or when there are no related games.
|
||||
public let platform: Platform?
|
||||
|
||||
/// The release dates of this amiibo across different regions.
|
||||
@@ -52,7 +52,32 @@ public struct Amiibo: Sendable, Hashable {
|
||||
public let type: String
|
||||
|
||||
// MARK: Initializers
|
||||
|
||||
|
||||
/// Initializes this model.
|
||||
package init(
|
||||
head: String,
|
||||
tail: String,
|
||||
name: String,
|
||||
type: String,
|
||||
series: String,
|
||||
gameCharacter: String,
|
||||
gameSeries: String,
|
||||
image: String,
|
||||
release: Release = .init(),
|
||||
platform: Platform? = nil
|
||||
) {
|
||||
self.gameCharacter = gameCharacter
|
||||
self.gameSeries = gameSeries
|
||||
self.head = head
|
||||
self.image = image
|
||||
self.name = name
|
||||
self.platform = platform
|
||||
self.release = release
|
||||
self.series = series
|
||||
self.tail = tail
|
||||
self.type = type
|
||||
}
|
||||
|
||||
/// Initializes this model from a given payload.
|
||||
/// - Parameter payload: A payload that contains the values for the model.
|
||||
init(_ payload: Components.Schemas.Amiibo) {
|
||||
@@ -74,7 +99,12 @@ public struct Amiibo: Sendable, Hashable {
|
||||
}
|
||||
|
||||
// MARK: Computed
|
||||
|
||||
|
||||
/// The ``identifier`` of this amiibo.
|
||||
public var id: String {
|
||||
identifier
|
||||
}
|
||||
|
||||
/// The full 16-character hexadecimal identifier, composed of the ``head`` and ``tail``.
|
||||
public var identifier: String {
|
||||
head + tail
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
|
||||
extension Amiibo {
|
||||
/// A model that represents a game related to an amiibo.
|
||||
public struct Game: Sendable, Hashable {
|
||||
public struct Game: Sendable, Hashable, Codable {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
@@ -29,6 +29,17 @@ extension Amiibo {
|
||||
|
||||
// MARK: Initializers
|
||||
|
||||
/// Initializes this model.
|
||||
package init(
|
||||
identifiers: [String],
|
||||
name: String,
|
||||
usages: [Usage]? = nil
|
||||
) {
|
||||
self.identifiers = identifiers
|
||||
self.name = name
|
||||
self.usages = usages
|
||||
}
|
||||
|
||||
/// Initializes this model from a given payload.
|
||||
/// - Parameter payload: A payload that contains the values for the model.
|
||||
init(_ payload: Components.Schemas.AmiiboGame) {
|
||||
|
||||
@@ -13,8 +13,8 @@
|
||||
// ===----------------------------------------------------------------------===
|
||||
|
||||
extension Amiibo {
|
||||
/// A model that represents a collection of `Switch`, `Switch 2`, `3DS`, and `Wii U` games related to an amiibo.
|
||||
public struct Platform: Sendable, Hashable {
|
||||
/// A model that groups the games related to an amiibo by platform.
|
||||
public struct Platform: Sendable, Hashable, Codable {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
@@ -31,8 +31,21 @@ extension Amiibo {
|
||||
public let wiiU: [Game]
|
||||
|
||||
// MARK: Initializers
|
||||
|
||||
|
||||
/// Initializes this model.
|
||||
package init(
|
||||
`switch`: [Game] = [],
|
||||
switch2: [Game] = [],
|
||||
threeDS: [Game] = [],
|
||||
wiiU: [Game] = []
|
||||
) {
|
||||
self.switch = `switch`
|
||||
self.switch2 = switch2
|
||||
self.threeDS = threeDS
|
||||
self.wiiU = wiiU
|
||||
}
|
||||
|
||||
/// Initializes this model from the given payloads.
|
||||
///
|
||||
/// > important: In case no data is provided, then an instance of this model is not created.
|
||||
///
|
||||
|
||||
@@ -18,7 +18,7 @@ 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 {
|
||||
public struct Release: Sendable, Hashable, Codable {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
@@ -36,6 +36,19 @@ extension Amiibo {
|
||||
|
||||
// MARK: Initializers
|
||||
|
||||
/// Initializes this model.
|
||||
package init(
|
||||
america: Date? = nil,
|
||||
australia: Date? = nil,
|
||||
europe: Date? = nil,
|
||||
japan: Date? = nil
|
||||
) {
|
||||
self.america = america
|
||||
self.australia = australia
|
||||
self.europe = europe
|
||||
self.japan = japan
|
||||
}
|
||||
|
||||
/// Initializes this model from a given payload.
|
||||
/// - Parameter payload: A payload that contains the values for the model.
|
||||
init(_ payload: Components.Schemas.AmiiboRelease) {
|
||||
|
||||
@@ -14,18 +14,24 @@
|
||||
|
||||
extension Amiibo {
|
||||
/// A model that represents the usage of an amiibo within a certain game.
|
||||
public struct Usage: Sendable, Hashable {
|
||||
public struct Usage: Sendable, Hashable, Codable {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
/// A description of how the amiibo is used within the game.
|
||||
public let explanation: String
|
||||
|
||||
/// A flag that indicates whether the amiibo can save game data.
|
||||
/// Whether the amiibo can save game data.
|
||||
public let isWriteable: Bool
|
||||
|
||||
// MARK: Initializers
|
||||
|
||||
/// Initializes this model.
|
||||
package init(explanation: String, isWriteable: Bool) {
|
||||
self.explanation = explanation
|
||||
self.isWriteable = isWriteable
|
||||
}
|
||||
|
||||
/// Initializes this model from a given payload.
|
||||
/// - Parameter payload: A payload that contains the values for the model.
|
||||
init(_ payload: Components.Schemas.AmiiboUsage) {
|
||||
|
||||
@@ -24,12 +24,25 @@ public struct AmiiboSeries: KeyNameModel {
|
||||
public let name: String
|
||||
|
||||
// MARK: Initializers
|
||||
|
||||
|
||||
/// Initializes this model.
|
||||
package init(key: String, name: String) {
|
||||
self.key = key
|
||||
self.name = name
|
||||
}
|
||||
|
||||
/// Initializes this model from a given payload.
|
||||
/// - Parameter payload: A payload that contains the values for the model.
|
||||
init(_ payload: some KeyNamePayload) {
|
||||
self.key = payload.key
|
||||
self.name = payload.name
|
||||
}
|
||||
|
||||
|
||||
// MARK: Computed
|
||||
|
||||
/// The ``key`` of this amiibo series.
|
||||
public var id: String {
|
||||
key
|
||||
}
|
||||
|
||||
}
|
||||
@@ -24,12 +24,25 @@ public struct AmiiboType: KeyNameModel {
|
||||
public let name: String
|
||||
|
||||
// MARK: Initializers
|
||||
|
||||
|
||||
/// Initializes this model.
|
||||
package init(key: String, name: String) {
|
||||
self.key = key
|
||||
self.name = name
|
||||
}
|
||||
|
||||
/// Initializes this model from a given payload.
|
||||
/// - Parameter payload: A payload that contains the values for the model.
|
||||
init(_ payload: some KeyNamePayload) {
|
||||
self.key = payload.key
|
||||
self.name = payload.name
|
||||
}
|
||||
|
||||
|
||||
// MARK: Computed
|
||||
|
||||
/// The ``key`` of this amiibo type.
|
||||
public var id: String {
|
||||
key
|
||||
}
|
||||
|
||||
}
|
||||
@@ -25,11 +25,24 @@ public struct GameCharacter: KeyNameModel {
|
||||
|
||||
// MARK: Initializers
|
||||
|
||||
/// Initializes this model.
|
||||
package init(key: String, name: String) {
|
||||
self.key = key
|
||||
self.name = name
|
||||
}
|
||||
|
||||
/// Initializes this model from a given payload.
|
||||
/// - Parameter payload: A payload that contains the values for the model.
|
||||
init(_ payload: some KeyNamePayload) {
|
||||
self.key = payload.key
|
||||
self.name = payload.name
|
||||
}
|
||||
|
||||
|
||||
// MARK: Computed
|
||||
|
||||
/// The ``key`` of this game character.
|
||||
public var id: String {
|
||||
key
|
||||
}
|
||||
|
||||
}
|
||||
@@ -24,12 +24,25 @@ public struct GameSeries: KeyNameModel {
|
||||
public let name: String
|
||||
|
||||
// MARK: Initializers
|
||||
|
||||
|
||||
/// Initializes this model.
|
||||
package init(key: String, name: String) {
|
||||
self.key = key
|
||||
self.name = name
|
||||
}
|
||||
|
||||
/// Initializes this model from a given payload.
|
||||
/// - Parameter payload: A payload that contains the values for the model.
|
||||
init(_ payload: some KeyNamePayload) {
|
||||
self.key = payload.key
|
||||
self.name = payload.name
|
||||
}
|
||||
|
||||
|
||||
// MARK: Computed
|
||||
|
||||
/// The ``key`` of this game series.
|
||||
public var id: String {
|
||||
key
|
||||
}
|
||||
|
||||
}
|
||||
@@ -16,44 +16,44 @@ import Foundation
|
||||
|
||||
/// A protocol that defines a client that fetches data from the Amiibo API.
|
||||
///
|
||||
/// ``AmiiboLiveClient`` is the default implementation. Conform to this protocol to provide another one, such as a mock for tests. Conforming types must be `Sendable`.
|
||||
/// ``AmiiboLiveClient`` is the default implementation; conform to provide another, such as a mock. Conforming types must be `Sendable`.
|
||||
public protocol AmiiboClient: Sendable {
|
||||
|
||||
// MARK: Functions
|
||||
|
||||
/// Gets a list of amiibo items based on a given filter.
|
||||
/// - Parameter filter: A filter to remove unwanted items from the result.
|
||||
/// - Parameter filter: A filter for the result.
|
||||
/// - Returns: A list of filtered amiibo items.
|
||||
/// - Throws: An ``AmiiboServiceError`` error in case some issue is encountered while generating the result.
|
||||
/// - Throws: An ``AmiiboServiceError`` if the call fails.
|
||||
func getAmiibos(by filter: AmiiboFilter) async throws(AmiiboServiceError) -> [Amiibo]
|
||||
|
||||
/// Gets a list of amiibo series based on a given filter.
|
||||
/// - Parameter filter: A filter to remove unwanted items from the result.
|
||||
/// - Parameter filter: A filter for the result.
|
||||
/// - Returns: A list of filtered amiibo series.
|
||||
/// - Throws: An ``AmiiboServiceError`` error in case some issue is encountered while generating the result.
|
||||
/// - Throws: An ``AmiiboServiceError`` if the call fails.
|
||||
func getAmiiboSeries(by filter: AmiiboSeriesFilter) async throws(AmiiboServiceError) -> [AmiiboSeries]
|
||||
|
||||
/// Gets a list of amiibo types based on a given filter.
|
||||
/// - Parameter filter: A filter to remove unwanted items from the result.
|
||||
/// - Parameter filter: A filter for the result.
|
||||
/// - Returns: A list of filtered amiibo types.
|
||||
/// - Throws: An ``AmiiboServiceError`` error in case some issue is encountered while generating the result.
|
||||
/// - Throws: An ``AmiiboServiceError`` if the call fails.
|
||||
func getAmiiboTypes(by filter: AmiiboTypeFilter) async throws(AmiiboServiceError) -> [AmiiboType]
|
||||
|
||||
/// Gets a list of game characters based on a given filter.
|
||||
/// - Parameter filter: A filter to remove unwanted items from the result.
|
||||
/// - Parameter filter: A filter for the result.
|
||||
/// - Returns: A list of filtered game characters.
|
||||
/// - Throws: An ``AmiiboServiceError`` error in case some issue is encountered while generating the result.
|
||||
/// - Throws: An ``AmiiboServiceError`` if the call fails.
|
||||
func getGameCharacters(by filter: GameCharacterFilter) async throws(AmiiboServiceError) -> [GameCharacter]
|
||||
|
||||
/// Gets a list of game series based on a given filter.
|
||||
/// - Parameter filter: A filter to remove unwanted items from the result.
|
||||
/// - Parameter filter: A filter for the result.
|
||||
/// - Returns: A list of filtered game series.
|
||||
/// - Throws: An ``AmiiboServiceError`` error in case some issue is encountered while generating the result.
|
||||
/// - Throws: An ``AmiiboServiceError`` if the call fails.
|
||||
func getGameSeries(by filter: GameSeriesFilter) async throws(AmiiboServiceError) -> [GameSeries]
|
||||
|
||||
/// Gets the date when the data was last updated.
|
||||
/// - Returns: A last updated date, decoded as UTC.
|
||||
/// - Throws: An ``AmiiboServiceError`` error in case some issue is encountered while generating the result.
|
||||
/// - Throws: An ``AmiiboServiceError`` if the call fails.
|
||||
func getLastUpdated() async throws(AmiiboServiceError) -> Date
|
||||
|
||||
}
|
||||
@@ -16,7 +16,7 @@ import Foundation
|
||||
|
||||
/// The entry point for fetching data from the Amiibo API.
|
||||
///
|
||||
/// This service forwards every call to the ``AmiiboClient`` passed at initialization, which defaults to ``AmiiboLiveClient``. It is `Sendable`, so you can share an instance across concurrency domains.
|
||||
/// It forwards every call to the ``AmiiboClient`` given at initialization, which defaults to ``AmiiboLiveClient``. Instances are `Sendable`.
|
||||
public struct AmiiboService: Sendable {
|
||||
|
||||
// MARK: Properties
|
||||
@@ -35,9 +35,9 @@ public struct AmiiboService: Sendable {
|
||||
// MARK: Functions
|
||||
|
||||
/// Gets a list of amiibo items based on a given filter.
|
||||
/// - Parameter filter: A filter to remove unwanted items from the result.
|
||||
/// - Parameter filter: A filter for the result.
|
||||
/// - Returns: A list of filtered amiibo items.
|
||||
/// - Throws: An ``AmiiboServiceError`` error in case some issue is encountered while generating the result.
|
||||
/// - Throws: An ``AmiiboServiceError`` if the call fails.
|
||||
public func getAmiibos(
|
||||
_ filter: AmiiboFilter = .init()
|
||||
) async throws(AmiiboServiceError) -> [Amiibo] {
|
||||
@@ -45,9 +45,9 @@ public struct AmiiboService: Sendable {
|
||||
}
|
||||
|
||||
/// Gets a list of amiibo series based on a given filter.
|
||||
/// - Parameter filter: A filter to remove unwanted items from the result.
|
||||
/// - Parameter filter: A filter for the result.
|
||||
/// - Returns: A list of filtered amiibo series.
|
||||
/// - Throws: An ``AmiiboServiceError`` error in case some issue is encountered while generating the result.
|
||||
/// - Throws: An ``AmiiboServiceError`` if the call fails.
|
||||
public func getAmiiboSeries(
|
||||
_ filter: AmiiboSeriesFilter = .init()
|
||||
) async throws(AmiiboServiceError) -> [AmiiboSeries] {
|
||||
@@ -55,9 +55,9 @@ public struct AmiiboService: Sendable {
|
||||
}
|
||||
|
||||
/// Gets a list of amiibo types based on a given filter.
|
||||
/// - Parameter filter: A filter to remove unwanted items from the result.
|
||||
/// - Parameter filter: A filter for the result.
|
||||
/// - Returns: A list of filtered amiibo types.
|
||||
/// - Throws: An ``AmiiboServiceError`` error in case some issue is encountered while generating the result.
|
||||
/// - Throws: An ``AmiiboServiceError`` if the call fails.
|
||||
public func getAmiiboTypes(
|
||||
_ filter: AmiiboTypeFilter = .init()
|
||||
) async throws(AmiiboServiceError) -> [AmiiboType] {
|
||||
@@ -65,9 +65,9 @@ public struct AmiiboService: Sendable {
|
||||
}
|
||||
|
||||
/// Gets a list of game characters based on a given filter.
|
||||
/// - Parameter filter: A filter to remove unwanted items from the result.
|
||||
/// - Parameter filter: A filter for the result.
|
||||
/// - Returns: A list of filtered game characters.
|
||||
/// - Throws: An ``AmiiboServiceError`` error in case some issue is encountered while generating the result.
|
||||
/// - Throws: An ``AmiiboServiceError`` if the call fails.
|
||||
public func getGameCharacters(
|
||||
_ filter: GameCharacterFilter = .init()
|
||||
) async throws(AmiiboServiceError) -> [GameCharacter] {
|
||||
@@ -75,9 +75,9 @@ public struct AmiiboService: Sendable {
|
||||
}
|
||||
|
||||
/// Gets a list of game series based on a given filter.
|
||||
/// - Parameter filter: A filter to remove unwanted items from the result.
|
||||
/// - Parameter filter: A filter for the result.
|
||||
/// - Returns: A list of filtered game series.
|
||||
/// - Throws: An ``AmiiboServiceError`` error in case some issue is encountered while generating the result.
|
||||
/// - Throws: An ``AmiiboServiceError`` if the call fails.
|
||||
public func getGameSeries(
|
||||
_ filter: GameSeriesFilter = .init()
|
||||
) async throws(AmiiboServiceError) -> [GameSeries] {
|
||||
@@ -86,7 +86,7 @@ public struct AmiiboService: Sendable {
|
||||
|
||||
/// Gets the date when the data was last updated.
|
||||
/// - Returns: A last updated date, decoded as UTC.
|
||||
/// - Throws: An ``AmiiboServiceError`` error in case some issue is encountered while generating the result.
|
||||
/// - Throws: An ``AmiiboServiceError`` if the call fails.
|
||||
public func getLastUpdated() async throws(AmiiboServiceError) -> Date {
|
||||
try await client.getLastUpdated()
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user