-
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-1847: Migrate subscriptions API to native
- Loading branch information
1 parent
5c784fe
commit 09a5299
Showing
10 changed files
with
160 additions
and
121 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
22 changes: 22 additions & 0 deletions
22
Sources/PIALibrary/Account/Data/Networking/SubscriptionsRequestConfiguration.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,22 @@ | ||
|
||
|
||
import Foundation | ||
import NWHttpConnection | ||
|
||
struct SubscriptionsRequestConfiguration: NetworkRequestConfigurationType { | ||
|
||
let networkRequestModule: NetworkRequestModule = .account | ||
let path: RequestAPI.Path = .iosSubscriptions | ||
let httpMethod: NWHttpConnection.NWConnectionHTTPMethod = .get | ||
let contentType: NetworkRequestContentType = .json | ||
let inlcudeAuthHeaders: Bool = false | ||
var urlQueryParameters: [String : String]? = nil | ||
let responseDataType: NWDataResponseType = .jsonData | ||
|
||
var body: Data? = nil | ||
var otherHeaders: [String : String]? = nil | ||
|
||
let timeout: TimeInterval = 10 | ||
let requestQueue: DispatchQueue? = DispatchQueue(label: "subscriptions_request.queue") | ||
} | ||
|
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
64 changes: 64 additions & 0 deletions
64
Sources/PIALibrary/Account/Domain/UseCases/SubscriptionsUseCase.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,64 @@ | ||
|
||
import Foundation | ||
|
||
protocol SubscriptionsUseCaseType { | ||
typealias Completion = ((Result<AppStoreInformation?, NetworkRequestError>) -> Void) | ||
func callAsFunction(receiptBase64: String?, completion: @escaping Completion) | ||
} | ||
class SubscriptionsUseCase: SubscriptionsUseCaseType { | ||
let networkClient: NetworkRequestClientType | ||
let refreshAuthTokensChecker: RefreshAuthTokensCheckerType | ||
|
||
init(networkClient: NetworkRequestClientType, refreshAuthTokensChecker: RefreshAuthTokensCheckerType) { | ||
self.networkClient = networkClient | ||
self.refreshAuthTokensChecker = refreshAuthTokensChecker | ||
} | ||
|
||
func callAsFunction(receiptBase64: String?, completion: @escaping Completion) { | ||
|
||
// The auth token is not required in the Subscriptions request | ||
// That's why refreshing the tokens if needed can be executed in parallel | ||
refreshAuthTokensChecker.refreshIfNeeded { _ in } | ||
|
||
executeRequest(with: receiptBase64, completion: completion) | ||
|
||
} | ||
|
||
|
||
} | ||
|
||
private extension SubscriptionsUseCase { | ||
|
||
func executeRequest(with receiptBase64: String?, completion: @escaping Completion) { | ||
var configuration = SubscriptionsRequestConfiguration() | ||
|
||
var queryParams: [String: String] = [ | ||
"type": "subscription" | ||
] | ||
|
||
if let receiptBase64 { | ||
queryParams["receipt"] = receiptBase64 | ||
} | ||
|
||
configuration.urlQueryParameters = queryParams | ||
|
||
networkClient.executeRequest(with: configuration) { error, dataResponse in | ||
|
||
if let error { | ||
completion(.failure(error)) | ||
} else { | ||
if let dataContent = dataResponse?.data { | ||
if let appStoreInfo = try? JSONDecoder().decode(AppStoreInformation.self, from: dataContent) { | ||
completion(.success(appStoreInfo)) | ||
} else { | ||
completion(.failure(.unableToDecodeData)) | ||
} | ||
} else { | ||
completion(.failure(.noDataContent)) | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
} |
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
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