Skip to content

Commit

Permalink
BUG: Remove TMDb class and refactor configuration (#153)
Browse files Browse the repository at this point in the history
* BUG: Remove TMDb class and refactor configuration

* Fix lint error
  • Loading branch information
adamayoung authored Feb 6, 2024
1 parent c7f0dd7 commit 6049b86
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 63 deletions.
2 changes: 1 addition & 1 deletion Sources/TMDb/TMDb.docc/GettingStarted/ConfiguringTMDb.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
```
1 change: 0 additions & 1 deletion Sources/TMDb/TMDb.docc/TMDb.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ For the TMDb API documentation, see

- <doc:/CreatingTMDbAPIKey>
- <doc:/ConfiguringTMDb>
- ``TMDb/TMDb``
- ``TMDbConfiguration``
- ``HTTPClient``

Expand Down
49 changes: 0 additions & 49 deletions Sources/TMDb/TMDb.swift

This file was deleted.

19 changes: 19 additions & 0 deletions Sources/TMDb/TMDbConfiguration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
}

}
4 changes: 2 additions & 2 deletions Sources/TMDb/TMDbFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// TMDbTests.swift
// TMDbConfigurationTests.swift
// TMDb
//
// Copyright © 2024 Adam Young.
Expand All @@ -20,23 +20,23 @@
@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)
}

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)
}
Expand All @@ -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 {

Expand Down

0 comments on commit 6049b86

Please sign in to comment.