-
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.
KM-4537: Migrate feature flags api to native (#47)
* KM-4537: Migrate feature flags API to native * KM-4537: Migrate feature flags API to native
- Loading branch information
1 parent
0bfa741
commit 0f0daed
Showing
8 changed files
with
382 additions
and
20 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
23 changes: 23 additions & 0 deletions
23
Sources/PIALibrary/Account/Data/Networking/FeatureFlagsRequestConfiguration.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,23 @@ | ||
|
||
import Foundation | ||
import NWHttpConnection | ||
|
||
struct FeatureFlagsRequestConfiguration: NetworkRequestConfigurationType { | ||
|
||
let networkRequestModule: NetworkRequestModule = .account | ||
let path: RequestAPI.Path = .iosFeatureFlag | ||
let httpMethod: NWHttpConnection.NWConnectionHTTPMethod = .get | ||
let contentType: NetworkRequestContentType = .json | ||
let inlcudeAuthHeaders: Bool = false | ||
let urlQueryParameters: [String : String]? = nil | ||
let responseDataType: NWDataResponseType = .jsonData | ||
|
||
var otherHeaders: [String : String]? = nil | ||
var body: Data? = nil | ||
|
||
let timeout: TimeInterval = 10 | ||
let requestQueue: DispatchQueue? = DispatchQueue(label: "feature_flags_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
15 changes: 15 additions & 0 deletions
15
Sources/PIALibrary/Account/Domain/Entities/FeatureFlagsInformation.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,15 @@ | ||
|
||
import Foundation | ||
|
||
|
||
struct FeatureFlagsInformation: Codable { | ||
let flags: [String] | ||
} | ||
|
||
extension FeatureFlagsInformation { | ||
static func makeWith(data: Data) -> FeatureFlagsInformation? { | ||
try? JSONDecoder().decode(FeatureFlagsInformation.self, from: data) | ||
} | ||
} | ||
|
||
|
54 changes: 54 additions & 0 deletions
54
Sources/PIALibrary/Account/Domain/UseCases/FeatureFlagsUseCase.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,54 @@ | ||
|
||
import Foundation | ||
|
||
|
||
protocol FeatureFlagsUseCaseType { | ||
typealias Completion = ((Result<FeatureFlagsInformation, NetworkRequestError>) -> Void) | ||
func callAsFunction(completion: @escaping Completion) | ||
} | ||
|
||
|
||
class FeatureFlagsUseCase: FeatureFlagsUseCaseType { | ||
|
||
let networkClient: NetworkRequestClientType | ||
let refreshAuthTokensChecker: RefreshAuthTokensCheckerType | ||
|
||
init(networkClient: NetworkRequestClientType, refreshAuthTokensChecker: RefreshAuthTokensCheckerType) { | ||
self.networkClient = networkClient | ||
self.refreshAuthTokensChecker = refreshAuthTokensChecker | ||
} | ||
|
||
func callAsFunction(completion: @escaping Completion) { | ||
// The API auth token is not required in the feature flags request | ||
// so refreshing the tokens can happen in parallel | ||
refreshAuthTokensChecker.refreshIfNeeded { _ in } | ||
|
||
// Get feature flags request | ||
executeRequest(with: completion) | ||
} | ||
} | ||
|
||
private extension FeatureFlagsUseCase { | ||
|
||
func executeRequest(with completion: @escaping Completion) { | ||
let configuration = FeatureFlagsRequestConfiguration() | ||
networkClient.executeRequest(with: configuration) { error, dataResponse in | ||
if let error { | ||
completion(.failure(error)) | ||
} else { | ||
guard let data = dataResponse?.data else { | ||
completion(.failure(.noDataContent)) | ||
return | ||
} | ||
|
||
guard let flagsInfo = FeatureFlagsInformation.makeWith(data: data) else { | ||
completion(.failure(.unableToDecodeData)) | ||
return | ||
} | ||
|
||
completion(.success(flagsInfo)) | ||
} | ||
} | ||
} | ||
|
||
} |
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
Oops, something went wrong.