Several fixes and optimizations all over the library #28
@@ -19,6 +19,19 @@ import OpenAPIURLSession
|
|||||||
/// A type that implements a live client to the [Amiibo API](https://www.amiiboapi.org) online service.
|
/// A type that implements a live client to the [Amiibo API](https://www.amiiboapi.org) online service.
|
||||||
public struct AmiiboLiveClient: Sendable {
|
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
|
// MARK: Properties
|
||||||
|
|
||||||
/// A client generated by the OpenAPI Runtime library to perform API calls.
|
/// A client generated by the OpenAPI Runtime library to perform API calls.
|
||||||
@@ -29,12 +42,8 @@ public struct AmiiboLiveClient: Sendable {
|
|||||||
/// Initializes this client with a transport for performing HTTP operations.
|
/// 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.
|
/// - Parameter transport: A transport that performs HTTP operations. Defaults to a `URLSessionTransport` using the shared session.
|
||||||
public init(transport: any ClientTransport = URLSessionTransport()) {
|
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(
|
self.client = .init(
|
||||||
serverURL: serverURL,
|
serverURL: Self.serverURL,
|
||||||
configuration: .init(dateTranscoder: ISODateTimeTranscoder()),
|
configuration: .init(dateTranscoder: ISODateTimeTranscoder()),
|
||||||
transport: transport
|
transport: transport
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -0,0 +1,46 @@
|
|||||||
|
// ===----------------------------------------------------------------------===
|
||||||
|
//
|
||||||
|
// This source file is part of the Amiibo Service open source project
|
||||||
|
//
|
||||||
|
// Copyright (c) 2026 Röck+Cöde VoF. and the Amiibo Service project authors
|
||||||
|
// Licensed under Apache license v2.0
|
||||||
|
//
|
||||||
|
// See LICENSE for license information
|
||||||
|
// See CONTRIBUTORS for the list of Amiibo Service project authors
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
//
|
||||||
|
// ===----------------------------------------------------------------------===
|
||||||
|
|
||||||
|
@testable import AmiiboService
|
||||||
|
import Foundation
|
||||||
|
import Testing
|
||||||
|
|
||||||
|
@Suite("Amiibo Live Client")
|
||||||
|
struct AmiiboLiveClientTests {
|
||||||
|
|
||||||
|
// MARK: Server URL tests
|
||||||
|
|
||||||
|
@Test("resolves the server URL defined in the OpenAPI specification")
|
||||||
|
func serverURLResolves() throws {
|
||||||
|
// GIVEN
|
||||||
|
// WHEN
|
||||||
|
let url = try Servers.Server1.url()
|
||||||
|
|
||||||
|
// THEN
|
||||||
|
#expect(url.absoluteString == "https://www.amiiboapi.org/api")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test("uses the server URL from the OpenAPI specification")
|
||||||
|
func serverURLMatchesSpecification() throws {
|
||||||
|
// GIVEN
|
||||||
|
let expected = try Servers.Server1.url()
|
||||||
|
|
||||||
|
// WHEN
|
||||||
|
let url = AmiiboLiveClient.serverURL
|
||||||
|
|
||||||
|
// THEN
|
||||||
|
#expect(url == expected)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user