37c0f3e322
This PR contains the work done to: * Documented all the `private`, `internal`, and `public` interfaces on the existing codebase; * Set the DocC documentation catalog in the project; * Written the main `Library` article for the DocC documentation catalog; * Added the documentation tasks in the `Makefile` file. Reviewed-on: #4 Co-authored-by: Javier Cicchelli <javier@rock-n-code.com> Co-committed-by: Javier Cicchelli <javier@rock-n-code.com>
303 lines
8.9 KiB
Swift
303 lines
8.9 KiB
Swift
//===----------------------------------------------------------------------===
|
|
//
|
|
// This source file is part of the AmiiboService open source project
|
|
//
|
|
// Copyright (c) 2024-2025 Röck+Cöde VoF. and the AmiiboAPI project authors
|
|
// Licensed under the EUPL 1.2 or later.
|
|
//
|
|
// See LICENSE for license information
|
|
// See CONTRIBUTORS for the list of AmiiboAPI project authors
|
|
//
|
|
//===----------------------------------------------------------------------===
|
|
|
|
import Foundation
|
|
import OpenAPIRuntime
|
|
import OpenAPIURLSession
|
|
|
|
/// A type that implements a live client to the online service.
|
|
public struct AmiiboLiveClient {
|
|
|
|
// MARK: Properties
|
|
|
|
/// A client generated by the `OpenAPIRuntime` library.
|
|
private let client: Client
|
|
|
|
// MARK: Initializers
|
|
|
|
/// Initializes this client.
|
|
public init() {
|
|
self.client = .init(
|
|
// The force unwrapping implemented below assumes that the server definition from the OpenAPI specification is correct.
|
|
serverURL: try! Servers.Server1.url(),
|
|
configuration: .init(dateTranscoder: ISOTimestampTranscoder()),
|
|
transport: URLSessionTransport()
|
|
)
|
|
}
|
|
|
|
}
|
|
|
|
// MARK: - APIClient
|
|
|
|
extension AmiiboLiveClient: APIClient {
|
|
|
|
// MARK: Functions
|
|
|
|
public func getAmiibos(
|
|
by filter: AmiiboFilter
|
|
) async throws(AmiiboServiceError) -> [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)
|
|
}
|
|
}
|
|
|
|
public func getAmiiboSeries(
|
|
by filter: AmiiboSeriesFilter
|
|
) async throws(AmiiboServiceError) -> [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)
|
|
}
|
|
}
|
|
|
|
public func getAmiiboTypes(
|
|
by filter: AmiiboTypeFilter
|
|
) async throws(AmiiboServiceError) -> [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)
|
|
}
|
|
}
|
|
|
|
public func getGameCharacters(
|
|
by filter: GameCharacterFilter
|
|
) async throws(AmiiboServiceError) -> [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)
|
|
}
|
|
}
|
|
|
|
public func getGameSeries(
|
|
by filter: GameSeriesFilter
|
|
) async throws(AmiiboServiceError) -> [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)
|
|
}
|
|
}
|
|
|
|
public func getLastUpdated() async throws(AmiiboServiceError) -> 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)
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
// MARK: - Helpers
|
|
|
|
private extension AmiiboLiveClient {
|
|
|
|
// MARK: Functions
|
|
|
|
/// 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 [.init(object)]
|
|
|
|
case let .AmiiboList(list):
|
|
return list
|
|
.map { .init($0) }
|
|
.sorted { $0.identifier < $1.identifier }
|
|
}
|
|
}
|
|
|
|
/// Retrieves a list of items that conforms to the `KeyNameModel` protocol from a wrapper container.
|
|
/// - Parameters:
|
|
/// - wrapper: A wrapper container that either has an object or a list of items.
|
|
/// - as: a model type that conforms to the `KeyNameModel` protocol.
|
|
/// - Returns: A list of items that conforms to the `KeyNameModel` protocol, sorted by keys.
|
|
func map<Model: KeyNameModel>(
|
|
_ wrapper: Components.Schemas.TupleWrapper,
|
|
as: Model.Type
|
|
) -> [Model] {
|
|
switch wrapper.amiibo {
|
|
case let .Tuple(payload):
|
|
return [.init(payload)]
|
|
|
|
case let .TupleList(list):
|
|
return list
|
|
.map { .init($0) }
|
|
.sorted { $0.key < $1.key }
|
|
}
|
|
}
|
|
|
|
}
|