-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Sergey Korney
committed
Oct 12, 2023
1 parent
0edfc2a
commit f41f1eb
Showing
49 changed files
with
582 additions
and
251 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
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
8 changes: 4 additions & 4 deletions
8
...rter/ProvidersToJsonStringConverter.swift → ...rter/ProvidersToJsonStringConverter.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
3 changes: 3 additions & 0 deletions
3
AffiseAttributionLib/Classes/debug/network/DebugNetworkUseCase.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,3 @@ | ||
protocol DebugNetworkUseCase { | ||
func onRequest(_ onComplete: @escaping DebugOnNetworkCallback) | ||
} |
22 changes: 22 additions & 0 deletions
22
AffiseAttributionLib/Classes/debug/network/DebugNetworkUseCaseImpl.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 @@ | ||
class DebugNetworkUseCaseImpl { | ||
let initProperties: AffiseInitProperties | ||
let networkService: NetworkService | ||
|
||
init( | ||
initProperties: AffiseInitProperties, | ||
networkService: NetworkService | ||
) { | ||
self.initProperties = initProperties | ||
self.networkService = networkService | ||
} | ||
} | ||
|
||
extension DebugNetworkUseCaseImpl : DebugNetworkUseCase { | ||
|
||
func onRequest(_ onDebug: @escaping DebugOnNetworkCallback) { | ||
if initProperties.isProduction == true { | ||
return | ||
} | ||
networkService.setDebug(onDebug) | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
AffiseAttributionLib/Classes/debug/network/DebugOnNetworkCallback.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 @@ | ||
public typealias DebugOnNetworkCallback = (_ request: HttpRequest, _ response: HttpResponse) -> Void |
1 change: 1 addition & 0 deletions
1
AffiseAttributionLib/Classes/debug/validate/DebugOnValidateCallback.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 @@ | ||
public typealias DebugOnValidateCallback = (_ status: ValidationStatus) -> Void |
3 changes: 3 additions & 0 deletions
3
AffiseAttributionLib/Classes/debug/validate/DebugValidateUseCase.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,3 @@ | ||
protocol DebugValidateUseCase { | ||
func validate(_ onComplete: @escaping DebugOnValidateCallback) | ||
} |
117 changes: 117 additions & 0 deletions
117
AffiseAttributionLib/Classes/debug/validate/DebugValidateUseCaseImpl.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,117 @@ | ||
class DebugValidateUseCaseImpl { | ||
|
||
private static let TIME_DELAY_SENDING: TimeInterval = 5 | ||
private let ATTEMPTS_TO_SEND = 2 | ||
private let TIMEOUT_SEND: TimeInterval = 30 | ||
|
||
let KEY = "message" | ||
let INVALID_APP_ID = "Invalid affise_app_id" | ||
let INVALID_CHECK_SUM = "Failed to get application or check sum" | ||
let PACKAGE_NAME_NOT_FOUND = "Package name not found" | ||
|
||
let URL = "https://tracking.affattr.com/postback/validate" | ||
|
||
let initProperties: AffiseInitProperties | ||
let logsManager: LogsManager | ||
let converter: ProvidersToJsonStringConverter | ||
let networkService: NetworkService | ||
let providers: [Provider] | ||
|
||
init( | ||
initProperties: AffiseInitProperties, | ||
postBackModelFactory: PostBackModelFactory, | ||
logsManager: LogsManager, | ||
networkService: NetworkService, | ||
converter: ProvidersToJsonStringConverter | ||
) { | ||
self.initProperties = initProperties | ||
self.logsManager = logsManager | ||
self.networkService = networkService | ||
self.converter = converter | ||
|
||
self.providers = postBackModelFactory.getRequestProviders() | ||
} | ||
|
||
private func createRequest() -> HttpResponse { | ||
guard let httpsUrl = URL.toURL() else { return HttpResponse(0, "", nil) } | ||
|
||
//Create request | ||
return networkService.executeRequest( | ||
httpsUrl: httpsUrl, | ||
method: .POST, | ||
data: converter.convert(from: providers).toData(), | ||
timeout: TIMEOUT_SEND, | ||
headers: CloudConfig.headers | ||
) | ||
} | ||
|
||
private func getValidationStatus(_ response: HttpResponse) -> ValidationStatus? { // wtf server? | ||
if response.code == 0 { | ||
return ValidationStatus.NETWORK_ERROR | ||
} | ||
|
||
do { | ||
//Create json | ||
let dict = try JSONSerialization.jsonObject(with: (response.body ?? "{}").data(using: .utf8)!, options: .mutableContainers) as! [String: Any?] | ||
let message: String = dict[KEY] as? String ?? "" | ||
|
||
if message.caseInsensitiveCompare(INVALID_APP_ID) == .orderedSame { | ||
return ValidationStatus.INVALID_APP_ID | ||
} | ||
if message.caseInsensitiveCompare(PACKAGE_NAME_NOT_FOUND) == .orderedSame { | ||
return ValidationStatus.PACKAGE_NAME_NOT_FOUND | ||
} | ||
if message.caseInsensitiveCompare(INVALID_CHECK_SUM) == .orderedSame { | ||
return ValidationStatus.INVALID_SECRET_KEY | ||
} | ||
} catch { | ||
} | ||
|
||
if response.code == 200 { | ||
return ValidationStatus.VALID | ||
} | ||
return nil | ||
} | ||
} | ||
|
||
extension DebugValidateUseCaseImpl: DebugValidateUseCase { | ||
|
||
func validate(_ onComplete: @escaping DebugOnValidateCallback) { | ||
if initProperties.isProduction == true { | ||
onComplete(.NOT_WORKING_ON_PRODUCTION) | ||
return | ||
} | ||
|
||
DispatchQueue.global(qos:.background).async { [weak self] in | ||
self?.sendWithRepeat(onComplete) { | ||
Thread.sleep(forTimeInterval: DebugValidateUseCaseImpl.TIME_DELAY_SENDING) | ||
} | ||
} | ||
} | ||
|
||
func sendWithRepeat( _ onComplete: @escaping DebugOnValidateCallback, onFailedAttempt: () -> Void) { | ||
//attempts to send | ||
var attempts = ATTEMPTS_TO_SEND | ||
|
||
//Send or not | ||
var send = false | ||
|
||
//While has attempts and not send | ||
while (attempts != 0 && !send) { | ||
let status = getValidationStatus(createRequest()) | ||
if let status = status { | ||
onComplete(status) | ||
//Send is ok | ||
send = true | ||
} else { | ||
attempts = attempts - 1 | ||
//Check attempts | ||
if (attempts == 0) { | ||
onComplete(ValidationStatus.NETWORK_ERROR) | ||
} else { | ||
onFailedAttempt() | ||
} | ||
} | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
AffiseAttributionLib/Classes/debug/validate/ValidationStatus.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,31 @@ | ||
@objc | ||
public enum ValidationStatus: Int { | ||
case VALID | ||
case INVALID_APP_ID | ||
case INVALID_SECRET_KEY | ||
case PACKAGE_NAME_NOT_FOUND | ||
case NOT_WORKING_ON_PRODUCTION | ||
case NETWORK_ERROR | ||
case UNKNOWN_ERROR | ||
|
||
public var status: String { | ||
switch self { | ||
case .VALID: return "valid" | ||
case .INVALID_APP_ID: return "invalid_app_id" | ||
case .INVALID_SECRET_KEY: return "invalid_secret_key" | ||
case .PACKAGE_NAME_NOT_FOUND: return "package_name_not_found" | ||
case .NOT_WORKING_ON_PRODUCTION: return "not_working_on_production" | ||
case .NETWORK_ERROR: return "network_error" | ||
case .UNKNOWN_ERROR: return "unknown_error" | ||
} | ||
} | ||
} | ||
|
||
extension ValidationStatus: CaseIterable, CustomStringConvertible { | ||
public var description: String { status } | ||
|
||
public static func from(_ name: String?) -> ValidationStatus? { | ||
if name == nil { return nil } | ||
return allCases.first { name == $0.status } | ||
} | ||
} |
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 |
---|---|---|
@@ -1,15 +1,29 @@ | ||
// | ||
// Errors.swift | ||
// app | ||
// | ||
// Created by Sergey Korney | ||
// | ||
|
||
|
||
public enum AffiseError: Error { | ||
case cloud(url: String, error: Error, attempts: Int, retry: Bool) | ||
case network(status: Int, message: String?) | ||
case offlineModeEnabled | ||
case trackingDisabledException | ||
case backgroundTrackingDisabledException | ||
} | ||
|
||
extension AffiseError : LocalizedError { | ||
|
||
private var localized: String? { | ||
switch self { | ||
case .cloud(url: let url, error: let error, attempts: let attempts, retry: let retry): | ||
return NSLocalizedString("AffiseError.cloud(url=\(url), error=\(error.localizedDescription), attempts=\(attempts), retry=\(retry))", comment: "") | ||
case .network(status: let status, message: let message): | ||
return NSLocalizedString("AffiseError.network(status=\(status), message=\(message ?? ""))", comment: "") | ||
case .offlineModeEnabled: | ||
return NSLocalizedString("AffiseError.offlineModeEnabled", comment: "") | ||
case .trackingDisabledException: | ||
return NSLocalizedString("AffiseError.trackingDisabledException", comment: "") | ||
case .backgroundTrackingDisabledException: | ||
return NSLocalizedString("AffiseError.backgroundTrackingDisabledException", comment: "") | ||
} | ||
} | ||
|
||
public var errorDescription: String? { | ||
return localized?.toJsonGuardString() | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
2 changes: 0 additions & 2 deletions
2
AffiseAttributionLib/Classes/events/predefined/ContactEvent.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
Oops, something went wrong.