Skip to content

Commit 6049b86

Browse files
authored
BUG: Remove TMDb class and refactor configuration (#153)
* BUG: Remove TMDb class and refactor configuration * Fix lint error
1 parent c7f0dd7 commit 6049b86

File tree

7 files changed

+33
-63
lines changed

7 files changed

+33
-63
lines changed

Sources/TMDb/TMDb.docc/GettingStarted/ConfiguringTMDb.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,5 @@ let tmdbConfiguration = TMDbConfiguration(
5050
Once a configuration object has been created, configure TMDb with it.
5151

5252
```swift
53-
TMDb.configure(tmdbConfiguration)
53+
TMDbConfiguration.configure(tmdbConfiguration)
5454
```

Sources/TMDb/TMDb.docc/TMDb.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ For the TMDb API documentation, see
4343

4444
- <doc:/CreatingTMDbAPIKey>
4545
- <doc:/ConfiguringTMDb>
46-
- ``TMDb/TMDb``
4746
- ``TMDbConfiguration``
4847
- ``HTTPClient``
4948

Sources/TMDb/TMDb.swift

Lines changed: 0 additions & 49 deletions
This file was deleted.

Sources/TMDb/TMDbConfiguration.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ import Foundation
2626
///
2727
public struct TMDbConfiguration {
2828

29+
private(set) static var shared = TMDbConfiguration(
30+
apiKey: {
31+
preconditionFailure("Configuration must first be set by calling TMDbConfiguration.configure(_:).")
32+
},
33+
httpClient: {
34+
preconditionFailure("Configuration must first be set by calling TMDbConfiguration.configure(_:).")
35+
}
36+
)
37+
2938
let apiKey: @Sendable () -> String
3039
let httpClient: @Sendable () -> any HTTPClient
3140

@@ -61,4 +70,14 @@ public struct TMDbConfiguration {
6170
self.httpClient = httpClient
6271
}
6372

73+
///
74+
/// Sets the configuration to be used with TMDb services.
75+
///
76+
/// - Parameters:
77+
/// - configuration: A TMDb configuration object.
78+
///
79+
public static func configure(_ configuration: TMDbConfiguration) {
80+
shared = configuration
81+
}
82+
6483
}

Sources/TMDb/TMDbFactory.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,11 @@ extension TMDbFactory {
112112
}
113113

114114
private static var apiKey: String {
115-
TMDb.configuration.apiKey()
115+
TMDbConfiguration.shared.apiKey()
116116
}
117117

118118
private static var httpClient: any HTTPClient {
119-
TMDb.configuration.httpClient()
119+
TMDbConfiguration.shared.httpClient()
120120
}
121121

122122
}

Tests/TMDbIntegrationTests/Utility/XCTestCase+ConfigureTMDb.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ import XCTest
2323
extension XCTestCase {
2424

2525
func configureTMDb() throws {
26-
try TMDb.configure(TMDbConfiguration(apiKey: tmdbAPIKey()))
26+
let configuration = try TMDbConfiguration(apiKey: tmdbAPIKey())
27+
TMDbConfiguration.configure(configuration)
2728
}
2829

2930
private func tmdbAPIKey() throws -> String {

Tests/TMDbTests/TMDbTests.swift renamed to Tests/TMDbTests/TMDbConfigurationTests.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// TMDbTests.swift
2+
// TMDbConfigurationTests.swift
33
// TMDb
44
//
55
// Copyright © 2024 Adam Young.
@@ -20,23 +20,23 @@
2020
@testable import TMDb
2121
import XCTest
2222

23-
final class TMDbTest: XCTestCase {
23+
final class TMDbConfigurationTests: XCTestCase {
2424

2525
func testConfigureSetsAPIKey() {
2626
let expectedAPIKey = "abc123"
2727
let configuration = TMDbConfiguration(apiKey: expectedAPIKey)
2828

29-
TMDb.configure(configuration)
30-
let apiKey = TMDb.configuration.apiKey()
29+
TMDbConfiguration.configure(configuration)
30+
let apiKey = TMDbConfiguration.shared.apiKey()
3131

3232
XCTAssertEqual(apiKey, expectedAPIKey)
3333
}
3434

3535
func testConfigurationWhenHTTPClientNotSetUsesDefaultAdapter() {
3636
let configuration = TMDbConfiguration(apiKey: "")
3737

38-
TMDb.configure(configuration)
39-
let httpClient = TMDb.configuration.httpClient()
38+
TMDbConfiguration.configure(configuration)
39+
let httpClient = TMDbConfiguration.shared.httpClient()
4040

4141
XCTAssertTrue(httpClient is URLSessionHTTPClientAdapter)
4242
}
@@ -45,15 +45,15 @@ final class TMDbTest: XCTestCase {
4545
let expectedHTTPClient = MockHTTPClient()
4646
let configuration = TMDbConfiguration(apiKey: "", httpClient: expectedHTTPClient)
4747

48-
TMDb.configure(configuration)
49-
let httpClient = TMDb.configuration.httpClient()
48+
TMDbConfiguration.configure(configuration)
49+
let httpClient = TMDbConfiguration.shared.httpClient()
5050

5151
XCTAssertIdentical(httpClient as AnyObject, expectedHTTPClient)
5252
}
5353

5454
}
5555

56-
extension TMDbTest {
56+
extension TMDbConfigurationTests {
5757

5858
private final class MockHTTPClient: HTTPClient {
5959

0 commit comments

Comments
 (0)