-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PIA-1842: Add sign up native endpoints
- Loading branch information
1 parent
94f1ab8
commit 80598b1
Showing
16 changed files
with
349 additions
and
23 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
19 changes: 19 additions & 0 deletions
19
Sources/PIALibrary/Account/Data/Networking/SignupRequestConfiguration.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,19 @@ | ||
|
||
import Foundation | ||
import NWHttpConnection | ||
|
||
struct SignupRequestConfiguration: NetworkRequestConfigurationType { | ||
let networkRequestModule: NetworkRequestModule = .account | ||
let path: RequestAPI.Path = .signup | ||
let httpMethod: NWHttpConnection.NWConnectionHTTPMethod = .post | ||
let inlcudeAuthHeaders: Bool = false | ||
|
||
// Refreshing the auth tokens is not needed before executing the refresh API token request | ||
let refreshAuthTokensIfNeeded: Bool = false | ||
var contentType: NetworkRequestContentType = .json | ||
let urlQueryParameters: [String : String]? = nil | ||
let responseDataType: NWDataResponseType = .jsonData | ||
var body: Data? = nil | ||
let timeout: TimeInterval = 10 | ||
let requestQueue: DispatchQueue? = DispatchQueue(label: "signup.queue") | ||
} |
36 changes: 36 additions & 0 deletions
36
Sources/PIALibrary/Account/Data/SignupInformationDataCoverter.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,36 @@ | ||
|
||
import Foundation | ||
|
||
class SignupInformationDataCoverter: SignupInformationDataCoverterType { | ||
func callAsFunction(signup: Signup) -> Data? { | ||
let signupInformation = SignupInformation(store: "apple_app_store", | ||
receipt: signup.receipt.base64EncodedString(), | ||
email: signup.email, | ||
marketing: stringify(json: signup.marketing), | ||
debug: stringify(json: signup.debug)) | ||
|
||
return signupInformation.toData() | ||
} | ||
|
||
private func stringify(json: [String: Any]?, prettyPrinted: Bool = false) -> String? { | ||
guard let json else { | ||
return nil | ||
} | ||
|
||
var options: JSONSerialization.WritingOptions = [] | ||
if prettyPrinted { | ||
options = JSONSerialization.WritingOptions.prettyPrinted | ||
} | ||
|
||
do { | ||
let data = try JSONSerialization.data(withJSONObject: json, options: options) | ||
if let string = String(data: data, encoding: String.Encoding.utf8) { | ||
return string | ||
} | ||
} catch { | ||
print(error) | ||
} | ||
|
||
return nil | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
Sources/PIALibrary/Account/Domain/Entities/SignUpAccountnformation.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,18 @@ | ||
|
||
import Foundation | ||
|
||
struct SignUpAccountnformation: Codable { | ||
let status: String | ||
let username: String | ||
let password: String | ||
} | ||
|
||
extension SignUpAccountnformation { | ||
func toDomainModel() -> Credentials { | ||
Credentials(username: username, password: password) | ||
} | ||
|
||
static func makeWith(data: Data) -> SignUpAccountnformation? { | ||
try? JSONDecoder().decode(SignUpAccountnformation.self, from: data) | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
Sources/PIALibrary/Account/Domain/Entities/SignupInformation.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,16 @@ | ||
|
||
import Foundation | ||
|
||
struct SignupInformation: Encodable { | ||
let store: String | ||
let receipt: String | ||
let email: String | ||
let marketing: String? | ||
let debug: String? | ||
} | ||
|
||
extension SignupInformation { | ||
func toData() -> Data? { | ||
try? JSONEncoder().encode(self) | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
Sources/PIALibrary/Account/Domain/Interfaces/SignupInformationDataCoverterType.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,6 @@ | ||
|
||
import Foundation | ||
|
||
protocol SignupInformationDataCoverterType { | ||
func callAsFunction(signup: Signup) -> Data? | ||
} |
51 changes: 51 additions & 0 deletions
51
Sources/PIALibrary/Account/Domain/UseCases/SignupUseCase.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,51 @@ | ||
|
||
import Foundation | ||
|
||
public protocol SignupUseCaseType { | ||
typealias Completion = ((Result<Credentials, NetworkRequestError>) -> Void) | ||
func callAsFunction(signup: Signup, completion: @escaping SignupUseCaseType.Completion) | ||
} | ||
|
||
class SignupUseCase: SignupUseCaseType { | ||
private let networkClient: NetworkRequestClientType | ||
private let signupInformationDataCoverter: SignupInformationDataCoverterType | ||
|
||
init(networkClient: NetworkRequestClientType, signupInformationDataCoverter: SignupInformationDataCoverterType) { | ||
self.networkClient = networkClient | ||
self.signupInformationDataCoverter = signupInformationDataCoverter | ||
} | ||
|
||
func callAsFunction(signup: Signup, completion: @escaping SignupUseCaseType.Completion) { | ||
var configuration = SignupRequestConfiguration() | ||
configuration.body = signupInformationDataCoverter(signup: signup) | ||
|
||
networkClient.executeRequest(with: configuration) { [weak self] error, dataResponse in | ||
guard let self else { return } | ||
if let error { | ||
completion(.failure(error)) | ||
} else if let dataResponse { | ||
self.handleDataResponse(dataResponse, completion: completion) | ||
} else { | ||
completion(.failure(NetworkRequestError.allConnectionAttemptsFailed())) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private extension SignupUseCase { | ||
private func handleDataResponse(_ dataResponse: NetworkRequestResponseType, completion: @escaping SignupUseCaseType.Completion) { | ||
guard let dataResponseContent = dataResponse.data else { | ||
completion(.failure(NetworkRequestError.noDataContent)) | ||
return | ||
} | ||
|
||
guard let dto = SignUpAccountnformation.makeWith(data: dataResponseContent) else { | ||
completion(.failure(NetworkRequestError.unableToDecodeDataContent)) | ||
return | ||
} | ||
|
||
completion(.success(dto.toDomainModel())) | ||
} | ||
} | ||
|
||
|
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
12 changes: 12 additions & 0 deletions
12
Tests/PIALibraryTests/Accounts/Mocks/NetworkRequestResponseStub.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,12 @@ | ||
|
||
import Foundation | ||
@testable import PIALibrary | ||
|
||
struct NetworkRequestResponseStub: NetworkRequestResponseType { | ||
let statusCode: Int? = 200 | ||
let data: Data? | ||
|
||
init(data: Data?) { | ||
self.data = data | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
Oops, something went wrong.