From 6049b86f2d28abd35a114e962db5a41b5c2fb027 Mon Sep 17 00:00:00 2001 From: Adam Young Date: Tue, 6 Feb 2024 22:38:08 +0000 Subject: [PATCH] BUG: Remove TMDb class and refactor configuration (#153) * BUG: Remove TMDb class and refactor configuration * Fix lint error --- .../GettingStarted/ConfiguringTMDb.md | 2 +- Sources/TMDb/TMDb.docc/TMDb.md | 1 - Sources/TMDb/TMDb.swift | 49 ------------------- Sources/TMDb/TMDbConfiguration.swift | 19 +++++++ Sources/TMDb/TMDbFactory.swift | 4 +- .../Utility/XCTestCase+ConfigureTMDb.swift | 3 +- ...sts.swift => TMDbConfigurationTests.swift} | 18 +++---- 7 files changed, 33 insertions(+), 63 deletions(-) delete mode 100644 Sources/TMDb/TMDb.swift rename Tests/TMDbTests/{TMDbTests.swift => TMDbConfigurationTests.swift} (77%) diff --git a/Sources/TMDb/TMDb.docc/GettingStarted/ConfiguringTMDb.md b/Sources/TMDb/TMDb.docc/GettingStarted/ConfiguringTMDb.md index b7969483..8d5f56aa 100644 --- a/Sources/TMDb/TMDb.docc/GettingStarted/ConfiguringTMDb.md +++ b/Sources/TMDb/TMDb.docc/GettingStarted/ConfiguringTMDb.md @@ -50,5 +50,5 @@ let tmdbConfiguration = TMDbConfiguration( Once a configuration object has been created, configure TMDb with it. ```swift -TMDb.configure(tmdbConfiguration) +TMDbConfiguration.configure(tmdbConfiguration) ``` diff --git a/Sources/TMDb/TMDb.docc/TMDb.md b/Sources/TMDb/TMDb.docc/TMDb.md index 79d1fa5e..422b1457 100644 --- a/Sources/TMDb/TMDb.docc/TMDb.md +++ b/Sources/TMDb/TMDb.docc/TMDb.md @@ -43,7 +43,6 @@ For the TMDb API documentation, see - - -- ``TMDb/TMDb`` - ``TMDbConfiguration`` - ``HTTPClient`` diff --git a/Sources/TMDb/TMDb.swift b/Sources/TMDb/TMDb.swift deleted file mode 100644 index b3c2e70a..00000000 --- a/Sources/TMDb/TMDb.swift +++ /dev/null @@ -1,49 +0,0 @@ -// -// TMDb.swift -// TMDb -// -// Copyright © 2024 Adam Young. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an AS IS BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// - -import Foundation - -/// -/// Provides an interface to set up TMDb. -/// -@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) -public final class TMDb { - - private(set) static var configuration = TMDbConfiguration( - apiKey: { - preconditionFailure("Configuration must first be set by calling TMDb.configure(_:).") - }, - httpClient: { - preconditionFailure("Configuration must first be set by calling TMDb.configure(_:).") - } - ) - - private init() {} - - /// - /// Sets the configuration to be used with TMDb services. - /// - /// - Parameters: - /// - configuration: A TMDb configuration object. - /// - public static func configure(_ configuration: TMDbConfiguration) { - Self.configuration = configuration - } - -} diff --git a/Sources/TMDb/TMDbConfiguration.swift b/Sources/TMDb/TMDbConfiguration.swift index 19d524d4..d79db627 100644 --- a/Sources/TMDb/TMDbConfiguration.swift +++ b/Sources/TMDb/TMDbConfiguration.swift @@ -26,6 +26,15 @@ import Foundation /// public struct TMDbConfiguration { + private(set) static var shared = TMDbConfiguration( + apiKey: { + preconditionFailure("Configuration must first be set by calling TMDbConfiguration.configure(_:).") + }, + httpClient: { + preconditionFailure("Configuration must first be set by calling TMDbConfiguration.configure(_:).") + } + ) + let apiKey: @Sendable () -> String let httpClient: @Sendable () -> any HTTPClient @@ -61,4 +70,14 @@ public struct TMDbConfiguration { self.httpClient = httpClient } + /// + /// Sets the configuration to be used with TMDb services. + /// + /// - Parameters: + /// - configuration: A TMDb configuration object. + /// + public static func configure(_ configuration: TMDbConfiguration) { + shared = configuration + } + } diff --git a/Sources/TMDb/TMDbFactory.swift b/Sources/TMDb/TMDbFactory.swift index c8abe2c5..300d9648 100644 --- a/Sources/TMDb/TMDbFactory.swift +++ b/Sources/TMDb/TMDbFactory.swift @@ -112,11 +112,11 @@ extension TMDbFactory { } private static var apiKey: String { - TMDb.configuration.apiKey() + TMDbConfiguration.shared.apiKey() } private static var httpClient: any HTTPClient { - TMDb.configuration.httpClient() + TMDbConfiguration.shared.httpClient() } } diff --git a/Tests/TMDbIntegrationTests/Utility/XCTestCase+ConfigureTMDb.swift b/Tests/TMDbIntegrationTests/Utility/XCTestCase+ConfigureTMDb.swift index 73112826..8edacf11 100644 --- a/Tests/TMDbIntegrationTests/Utility/XCTestCase+ConfigureTMDb.swift +++ b/Tests/TMDbIntegrationTests/Utility/XCTestCase+ConfigureTMDb.swift @@ -23,7 +23,8 @@ import XCTest extension XCTestCase { func configureTMDb() throws { - try TMDb.configure(TMDbConfiguration(apiKey: tmdbAPIKey())) + let configuration = try TMDbConfiguration(apiKey: tmdbAPIKey()) + TMDbConfiguration.configure(configuration) } private func tmdbAPIKey() throws -> String { diff --git a/Tests/TMDbTests/TMDbTests.swift b/Tests/TMDbTests/TMDbConfigurationTests.swift similarity index 77% rename from Tests/TMDbTests/TMDbTests.swift rename to Tests/TMDbTests/TMDbConfigurationTests.swift index 6d850e64..0ee64b1f 100644 --- a/Tests/TMDbTests/TMDbTests.swift +++ b/Tests/TMDbTests/TMDbConfigurationTests.swift @@ -1,5 +1,5 @@ // -// TMDbTests.swift +// TMDbConfigurationTests.swift // TMDb // // Copyright © 2024 Adam Young. @@ -20,14 +20,14 @@ @testable import TMDb import XCTest -final class TMDbTest: XCTestCase { +final class TMDbConfigurationTests: XCTestCase { func testConfigureSetsAPIKey() { let expectedAPIKey = "abc123" let configuration = TMDbConfiguration(apiKey: expectedAPIKey) - TMDb.configure(configuration) - let apiKey = TMDb.configuration.apiKey() + TMDbConfiguration.configure(configuration) + let apiKey = TMDbConfiguration.shared.apiKey() XCTAssertEqual(apiKey, expectedAPIKey) } @@ -35,8 +35,8 @@ final class TMDbTest: XCTestCase { func testConfigurationWhenHTTPClientNotSetUsesDefaultAdapter() { let configuration = TMDbConfiguration(apiKey: "") - TMDb.configure(configuration) - let httpClient = TMDb.configuration.httpClient() + TMDbConfiguration.configure(configuration) + let httpClient = TMDbConfiguration.shared.httpClient() XCTAssertTrue(httpClient is URLSessionHTTPClientAdapter) } @@ -45,15 +45,15 @@ final class TMDbTest: XCTestCase { let expectedHTTPClient = MockHTTPClient() let configuration = TMDbConfiguration(apiKey: "", httpClient: expectedHTTPClient) - TMDb.configure(configuration) - let httpClient = TMDb.configuration.httpClient() + TMDbConfiguration.configure(configuration) + let httpClient = TMDbConfiguration.shared.httpClient() XCTAssertIdentical(httpClient as AnyObject, expectedHTTPClient) } } -extension TMDbTest { +extension TMDbConfigurationTests { private final class MockHTTPClient: HTTPClient {