Skip to content

Commit

Permalink
Update creating sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
adamayoung committed Sep 16, 2024
1 parent 0cc1102 commit c5ee3bc
Show file tree
Hide file tree
Showing 18 changed files with 118 additions and 111 deletions.
14 changes: 10 additions & 4 deletions Tests/TMDbIntegrationTests/AccountIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,27 @@ import Testing
.tags(.account),
.enabled(if: CredentialHelper.shared.hasAPIKey && CredentialHelper.shared.hasCredential)
)
struct AccountIntegrationTests {
final class AccountIntegrationTests {

var accountService: (any AccountService)!
var authenticationService: (any AuthenticationService)!
var session: Session!

init() async throws {
let apiKey = CredentialHelper.shared.tmdbAPIKey()
let apiKey = CredentialHelper.shared.tmdbAPIKey
let tmdbClient = TMDbClient(apiKey: apiKey)
let credential = CredentialHelper.shared.tmdbCredential()

self.authenticationService = tmdbClient.authentication
self.accountService = tmdbClient.account
self.session = try await TMDbSessionHelper.shared.createSession()
}

self.session = try await authenticationService.createSession(withCredential: credential)
deinit {
if let thisSession = session {
Task {
try await TMDbSessionHelper.shared.delete(session: thisSession)
}
}
}

@Test("details")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct AuthenticationIntegrationTests {
var authenticationService: (any AuthenticationService)!

init() {
let apiKey = CredentialHelper.shared.tmdbAPIKey()
let apiKey = CredentialHelper.shared.tmdbAPIKey
self.authenticationService = TMDbClient(apiKey: apiKey).authentication
}

Expand All @@ -55,7 +55,7 @@ struct AuthenticationIntegrationTests {
.enabled(if: CredentialHelper.shared.hasCredential)
)
func createAndDeleteSessionWithCredential() async throws {
let credential = CredentialHelper.shared.tmdbCredential()
let credential = CredentialHelper.shared.tmdbCredential

let session = try await authenticationService.createSession(withCredential: credential)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct CertificationIntegrationTests {
var certificationService: (any CertificationService)!

init() {
let apiKey = CredentialHelper.shared.tmdbAPIKey()
let apiKey = CredentialHelper.shared.tmdbAPIKey
self.certificationService = TMDbClient(apiKey: apiKey).certifications
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/TMDbIntegrationTests/CompanyIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct CompanyIntegrationTests {
var companyService: (any CompanyService)!

init() {
let apiKey = CredentialHelper.shared.tmdbAPIKey()
let apiKey = CredentialHelper.shared.tmdbAPIKey
self.companyService = TMDbClient(apiKey: apiKey).companies
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct ConfigurationIntegrationTests {
var configurationService: (any ConfigurationService)!

init() throws {
let apiKey = CredentialHelper.shared.tmdbAPIKey()
let apiKey = CredentialHelper.shared.tmdbAPIKey
self.configurationService = TMDbClient(apiKey: apiKey).configurations
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/TMDbIntegrationTests/DiscoverIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct DiscoverIntegrationTests {
var discoverService: (any DiscoverService)!

init() {
let apiKey = CredentialHelper.shared.tmdbAPIKey()
let apiKey = CredentialHelper.shared.tmdbAPIKey
self.discoverService = TMDbClient(apiKey: apiKey).discover
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/TMDbIntegrationTests/GenreIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct GenreIntegrationTests {
var genreService: (any GenreService)!

init() {
let apiKey = CredentialHelper.shared.tmdbAPIKey()
let apiKey = CredentialHelper.shared.tmdbAPIKey
self.genreService = TMDbClient(apiKey: apiKey).genres
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/TMDbIntegrationTests/MovieIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct MovieIntegrationTests {
var movieService: (any MovieService)!

init() {
let apiKey = CredentialHelper.shared.tmdbAPIKey()
let apiKey = CredentialHelper.shared.tmdbAPIKey
self.movieService = TMDbClient(apiKey: apiKey).movies
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/TMDbIntegrationTests/PersonIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct PersonIntegrationTests {
var personService: (any PersonService)!

init() {
let apiKey = CredentialHelper.shared.tmdbAPIKey()
let apiKey = CredentialHelper.shared.tmdbAPIKey
self.personService = TMDbClient(apiKey: apiKey).people
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/TMDbIntegrationTests/SearchIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct SearchIntegrationTests {
var searchService: (any SearchService)!

init() {
let apiKey = CredentialHelper.shared.tmdbAPIKey()
let apiKey = CredentialHelper.shared.tmdbAPIKey
self.searchService = TMDbClient(apiKey: apiKey).search
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/TMDbIntegrationTests/TVEpisodeServiceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct TVEpisodeServiceTests {
var tvEpisodeService: (any TVEpisodeService)!

init() {
let apiKey = CredentialHelper.shared.tmdbAPIKey()
let apiKey = CredentialHelper.shared.tmdbAPIKey
self.tvEpisodeService = TMDbClient(apiKey: apiKey).tvEpisodes
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/TMDbIntegrationTests/TVSeasonIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct TVSeasonIntegrationTests {
var tvSeasonService: (any TVSeasonService)!

init() {
let apiKey = CredentialHelper.shared.tmdbAPIKey()
let apiKey = CredentialHelper.shared.tmdbAPIKey
self.tvSeasonService = TMDbClient(apiKey: apiKey).tvSeasons
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/TMDbIntegrationTests/TVSeriesServiceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct TVSeriesServiceTests {
var tvSeriesService: (any TVSeriesService)!

init() {
let apiKey = CredentialHelper.shared.tmdbAPIKey()
let apiKey = CredentialHelper.shared.tmdbAPIKey
self.tvSeriesService = TMDbClient(apiKey: apiKey).tvSeries
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/TMDbIntegrationTests/TrendingIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct TrendingIntegrationTests {
var trendingService: (any TrendingService)!

init() {
let apiKey = CredentialHelper.shared.tmdbAPIKey()
let apiKey = CredentialHelper.shared.tmdbAPIKey
self.trendingService = TMDbClient(apiKey: apiKey).trending
}

Expand Down
28 changes: 14 additions & 14 deletions Tests/TMDbIntegrationTests/Utility/CredentialHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,36 @@
import Foundation
import TMDb

struct CredentialHelper {
final class CredentialHelper {

static let shared = CredentialHelper()

var hasCredential: Bool {
let credential = tmdbCredential()

return !credential.username.isEmpty && !credential.password.isEmpty
}

var hasAPIKey: Bool {
!tmdbAPIKey().isEmpty
}

private let processInfo: ProcessInfo

private init(processInto: ProcessInfo = ProcessInfo.processInfo) {
self.processInfo = processInto
}

func tmdbCredential() -> Credential {
var hasCredential: Bool {
let credential = self.tmdbCredential

Check warning on line 34 in Tests/TMDbIntegrationTests/Utility/CredentialHelper.swift

View workflow job for this annotation

GitHub Actions / SwiftFormat

Insert/remove explicit self where applicable. (redundantSelf)

return !credential.username.isEmpty && !credential.password.isEmpty
}

lazy var tmdbCredential: Credential = {
let username = processInfo.environment["TMDB_USERNAME"] ?? ""
let password = processInfo.environment["TMDB_PASSWORD"] ?? ""
let credential = Credential(username: username, password: password)

return credential
}()

var hasAPIKey: Bool {
!self.tmdbAPIKey.isEmpty

Check warning on line 48 in Tests/TMDbIntegrationTests/Utility/CredentialHelper.swift

View workflow job for this annotation

GitHub Actions / SwiftFormat

Insert/remove explicit self where applicable. (redundantSelf)
}

func tmdbAPIKey() -> String {
lazy var tmdbAPIKey: String = {

Check warning on line 51 in Tests/TMDbIntegrationTests/Utility/CredentialHelper.swift

View workflow job for this annotation

GitHub Actions / SwiftFormat

Removes redundant closures bodies, containing a single statement, which are called immediately. (redundantClosure)
processInfo.environment["TMDB_API_KEY"] ?? ""
}
}()

Check warning on line 53 in Tests/TMDbIntegrationTests/Utility/CredentialHelper.swift

View workflow job for this annotation

GitHub Actions / SwiftFormat

Removes redundant closures bodies, containing a single statement, which are called immediately. (redundantClosure)

}
79 changes: 79 additions & 0 deletions Tests/TMDbIntegrationTests/Utility/TMDbSessionHelper.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
//
// File.swift

Check warning on line 2 in Tests/TMDbIntegrationTests/Utility/TMDbSessionHelper.swift

View workflow job for this annotation

GitHub Actions / SwiftFormat

Use specified source file header template for all files. (fileHeader)
// TMDb
//
// Created by Adam Young on 16/09/2024.

Check warning on line 5 in Tests/TMDbIntegrationTests/Utility/TMDbSessionHelper.swift

View workflow job for this annotation

GitHub Actions / SwiftFormat

Use specified source file header template for all files. (fileHeader)
//

Check warning on line 7 in Tests/TMDbIntegrationTests/Utility/TMDbSessionHelper.swift

View workflow job for this annotation

GitHub Actions / SwiftFormat

Use specified source file header template for all files. (fileHeader)
import Foundation
import TMDb

actor TMDbSessionHelper {

static let shared = TMDbSessionHelper()

private let credentialHelper: CredentialHelper

private var sessionCache: CacheEntry?

private init(credentialHelper: CredentialHelper = .shared) {
self.credentialHelper = credentialHelper
}

// func session() async throws -> Session {
// if let sessionCache {
// switch sessionCache {
// case .ready(let session):
// return session
//
// case .inProgress(let task):
// return try await task.value
// }
// }
//
// let apiKey = CredentialHelper.shared.tmdbAPIKey
// let tmdbClient = TMDbClient(apiKey: apiKey)
// let credential = credentialHelper.tmdbCredential
//
// let task = Task {
// try await tmdbClient.authentication.createSession(withCredential: credential)
// }
//
// sessionCache = .inProgress(task)
//
// do {
// let session = try await task.value
// sessionCache = .ready(session)
// return session
// } catch {
// sessionCache = nil
// throw error
// }
// }

func createSession() async throws -> Session {
let apiKey = CredentialHelper.shared.tmdbAPIKey
let tmdbClient = TMDbClient(apiKey: apiKey)
let credential = credentialHelper.tmdbCredential

return try await tmdbClient.authentication.createSession(withCredential: credential)

Check warning on line 59 in Tests/TMDbIntegrationTests/Utility/TMDbSessionHelper.swift

View workflow job for this annotation

GitHub Actions / SwiftFormat

Remove trailing blank line at the end of a scope. (blankLinesAtEndOfScope)

}

func delete(session: Session) async throws {
let apiKey = CredentialHelper.shared.tmdbAPIKey
let tmdbClient = TMDbClient(apiKey: apiKey)

try await tmdbClient.authentication.deleteSession(session)
}

}

extension TMDbSessionHelper {

private enum CacheEntry {
case inProgress(Task<Session, Error>)
case ready(Session)
}

}
78 changes: 0 additions & 78 deletions Tests/TMDbIntegrationTests/Utility/XCTestCase+ConfigureTMDb.swift

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct WatchProviderIntegrationTests {
var watchProviderService: (any WatchProviderService)!

init() {
let apiKey = CredentialHelper.shared.tmdbAPIKey()
let apiKey = CredentialHelper.shared.tmdbAPIKey
self.watchProviderService = TMDbClient(apiKey: apiKey).watchProviders
}

Expand Down

0 comments on commit c5ee3bc

Please sign in to comment.