-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FEATURE: Create Guest Session (#145)
- Loading branch information
1 parent
896bd0b
commit 83b26dd
Showing
22 changed files
with
509 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ clean: | |
rm -rf docs | ||
|
||
.PHONY: format | ||
format: | ||
swiftlint --fix | ||
swiftformat . | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// | ||
// AuthenticationService.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 for authenticating with TMDb. | ||
/// | ||
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) | ||
public final class AuthenticationService { | ||
|
||
private let apiClient: any APIClient | ||
|
||
/// | ||
/// Creates an authentication service object. | ||
/// | ||
public convenience init() { | ||
self.init( | ||
apiClient: TMDbFactory.authAPIClient | ||
) | ||
} | ||
|
||
init(apiClient: some APIClient) { | ||
self.apiClient = apiClient | ||
} | ||
|
||
/// | ||
/// Creates a guest session with TMDb. | ||
/// | ||
/// Guest sessions are a special kind of session that give you some of the | ||
/// functionality of an account, but not all. For example, some of the | ||
/// things you can do with a guest session are; maintain a rated list, a | ||
/// watchlist and a favourite list. | ||
/// | ||
/// Guest sessions will automatically be deleted if they are not used | ||
/// within 60 minutes of it being issued. | ||
/// | ||
/// [TMDb API - Authentication: Create Guest Session](https://developer.themoviedb.org/reference/certifications-tv-list) | ||
/// | ||
/// - Throws: TMDb error ``TMDbError``. | ||
/// | ||
/// - Returns: A guest session. | ||
/// | ||
public func createGuestSession() async throws -> GuestSession { | ||
let session: GuestSession | ||
do { | ||
session = try await apiClient.get(endpoint: AuthenticationEndpoint.createGuestSession) | ||
} catch let error { | ||
throw TMDbError(error: error) | ||
} | ||
|
||
return session | ||
} | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
Sources/TMDb/Authentication/Endpoints/AuthenticationEndpoint.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// | ||
// AuthenticationEndpoint.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 | ||
|
||
enum AuthenticationEndpoint { | ||
|
||
case createGuestSession | ||
|
||
} | ||
|
||
extension AuthenticationEndpoint: Endpoint { | ||
|
||
private static let basePath = URL(string: "/authentication")! | ||
|
||
var path: URL { | ||
switch self { | ||
case .createGuestSession: | ||
Self.basePath | ||
.appendingPathComponent("guest_session") | ||
.appendingPathComponent("new") | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// | ||
// GuestSession.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 | ||
|
||
/// | ||
/// A model representing a guest session. | ||
/// | ||
public struct GuestSession: Codable, Equatable, Hashable { | ||
|
||
/// | ||
/// Was session creation successful. | ||
/// | ||
public let success: Bool | ||
|
||
/// | ||
/// The identifier of this session. | ||
/// | ||
public let guestSessionID: String | ||
|
||
/// | ||
/// Date of session expiry. | ||
/// | ||
public let expiresAt: Date | ||
|
||
} | ||
|
||
extension GuestSession { | ||
|
||
private enum CodingKeys: String, CodingKey { | ||
|
||
case success | ||
case guestSessionID = "guestSessionId" | ||
case expiresAt | ||
|
||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
Sources/TMDb/TMDb.docc/Extensions/AuthenticationService.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# ``TMDb/AuthenticationService`` | ||
|
||
## Topics | ||
|
||
### Creating am Authentication Service | ||
|
||
- ``init()`` | ||
|
||
### Creating Sessions | ||
|
||
- ``createGuestSession()`` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
Tests/TMDbIntegrationTests/AuthenticationIntegrationTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// | ||
// AuthenticationIntegrationTests.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 TMDb | ||
import XCTest | ||
|
||
final class AuthenticationIntegrationTests: XCTestCase { | ||
|
||
var authenticationService: AuthenticationService! | ||
|
||
override func setUpWithError() throws { | ||
try super.setUpWithError() | ||
try configureTMDb() | ||
authenticationService = AuthenticationService() | ||
} | ||
|
||
override func tearDown() { | ||
authenticationService = nil | ||
super.tearDown() | ||
} | ||
|
||
func testCreateGuestSession() async throws { | ||
let session = try await authenticationService.createGuestSession() | ||
|
||
XCTAssertTrue(session.success) | ||
XCTAssertNotEqual(session.guestSessionID, "") | ||
} | ||
|
||
} |
66 changes: 66 additions & 0 deletions
66
Tests/TMDbTests/Authentication/AuthenticationServiceTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// | ||
// AuthenticationServiceTests.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. | ||
// | ||
|
||
@testable import TMDb | ||
import XCTest | ||
|
||
final class AuthenticationServiceTests: XCTestCase { | ||
|
||
var service: AuthenticationService! | ||
var apiClient: MockAPIClient! | ||
|
||
override func setUp() { | ||
super.setUp() | ||
apiClient = MockAPIClient() | ||
service = AuthenticationService(apiClient: apiClient) | ||
} | ||
|
||
override func tearDown() { | ||
apiClient = nil | ||
service = nil | ||
super.tearDown() | ||
} | ||
|
||
func testCreateGuestSessionReturnsGuestSession() async throws { | ||
let expectedResult = GuestSession.mock(expiresAt: Date(timeIntervalSince1970: 1_705_956_596)) | ||
|
||
apiClient.result = .success(expectedResult) | ||
|
||
let result = try await service.createGuestSession() | ||
|
||
XCTAssertEqual(result, expectedResult) | ||
XCTAssertEqual(apiClient.lastPath, AuthenticationEndpoint.createGuestSession.path) | ||
} | ||
|
||
func testCreateGuestSessionWhenErrorsThrowsError() async throws { | ||
apiClient.result = .failure(.unknown) | ||
|
||
var error: Error? | ||
do { | ||
_ = try await service.createGuestSession() | ||
} catch let err { | ||
error = err | ||
} | ||
|
||
let tmdbAPIError = try XCTUnwrap(error as? TMDbError) | ||
|
||
XCTAssertEqual(tmdbAPIError, .unknown) | ||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
Tests/TMDbTests/Authentication/Endpoints/AuthenticationEndpointTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// | ||
// AuthenticationEndpointTests.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. | ||
// | ||
|
||
@testable import TMDb | ||
import XCTest | ||
|
||
final class AuthenticationEndpointTests: XCTestCase { | ||
|
||
func testCreateGuestSessionEndpointReturnsURL() throws { | ||
let expectedURL = try XCTUnwrap(URL(string: "/authentication/guest_session/new")) | ||
|
||
let url = AuthenticationEndpoint.createGuestSession.path | ||
|
||
XCTAssertEqual(url, expectedURL) | ||
} | ||
|
||
} |
Oops, something went wrong.