Skip to content

Commit

Permalink
1.6.13
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Korney committed Oct 12, 2023
1 parent 0edfc2a commit f41f1eb
Show file tree
Hide file tree
Showing 49 changed files with 582 additions and 251 deletions.
2 changes: 1 addition & 1 deletion AffiseAttributionLib.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

Pod::Spec.new do |spec|
spec.name = "AffiseAttributionLib"
spec.version = ENV['LIB_VERSION'] || "1.6.12"
spec.version = ENV['LIB_VERSION'] || "1.6.13"
spec.summary = "Affise Attribution iOS library"
spec.description = "Affise SDK is a software you can use to collect app usage statistics, device identifiers, deeplink usage, track install referrer."
spec.homepage = "https://github.com/affise/sdk-ios"
Expand Down
25 changes: 24 additions & 1 deletion AffiseAttributionLib/Classes/Affise.swift
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public final class Affise: NSObject {
* Get module status
*/
@objc
public static func getStatus(_ module: AffiseModules, _ onComplete: @escaping (_ data: [AffiseKeyValue]) -> Void) {
public static func getStatus(_ module: AffiseModules, _ onComplete: @escaping OnKeyValueCallback) {
api?.moduleManager.status(module, onComplete)
}

Expand All @@ -215,4 +215,27 @@ public final class Affise: NSObject {
internal static func sendInternalEvent(_ event: InternalEvent) {
api?.storeInternalEventUseCase.storeInternalEvent(event: event)
}

public class Debug {
/**
* Won't work on Production
*
* Validate credentials
*/
@objc
public static func validate(_ callback: @escaping DebugOnValidateCallback) {
api?.debugValidateUseCase.validate(callback)
}

/**
* Won't work on Production
*
* Show request-response data
*/
@objc
public static func network(_ callback: @escaping DebugOnNetworkCallback) {
api?.debugNetworkUseCase.onRequest(callback)
}
}

}
4 changes: 3 additions & 1 deletion AffiseAttributionLib/Classes/AffiseApi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ internal protocol AffiseApi {
var preferencesUseCase: PreferencesUseCase {get}
var postBackModelFactory: PostBackModelFactory {get}
var moduleManager: AffiseModuleManager {get}
var storeInternalEventUseCase:StoreInternalEventUseCase {get}
var storeInternalEventUseCase: StoreInternalEventUseCase {get}
var debugValidateUseCase: DebugValidateUseCase {get}
var debugNetworkUseCase: DebugNetworkUseCase {get}

func isInitialized() -> Bool
}
21 changes: 17 additions & 4 deletions AffiseAttributionLib/Classes/AffiseComponent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@ internal class AffiseComponent: AffiseApi {
logsManager: logsManager,
isFirstForUserUseCase: isFirstForUserUseCase
)
lazy var storeInternalEventUseCase:StoreInternalEventUseCase = StoreInternalEventUseCaseImpl(repository: internalEventsRepository)
lazy var storeLogsUseCase:StoreLogsUseCase = StoreLogsUseCaseImpl(repository: logsRepository)
lazy var isFirstForUserStorage:IsFirstForUserStorage = IsFirstForUserStorageImpl(logsManager: logsManager, fileManager: fileManager)
lazy var isFirstForUserUseCase:IsFirstForUserUseCase = IsFirstForUserUseCaseImpl(isFirstForUserStorage: isFirstForUserStorage)
lazy var storeInternalEventUseCase: StoreInternalEventUseCase = StoreInternalEventUseCaseImpl(repository: internalEventsRepository)
lazy var storeLogsUseCase: StoreLogsUseCase = StoreLogsUseCaseImpl(repository: logsRepository)
lazy var isFirstForUserStorage: IsFirstForUserStorage = IsFirstForUserStorageImpl(logsManager: logsManager, fileManager: fileManager)
lazy var isFirstForUserUseCase: IsFirstForUserUseCase = IsFirstForUserUseCaseImpl(isFirstForUserStorage: isFirstForUserStorage)
lazy var initPropertiesStorage: InitPropertiesStorage = InitPropertiesStorageImpl()
lazy var setPropertiesWhenInitUseCase: SetPropertiesWhenAppInitializedUseCase = SetPropertiesWhenAppInitializedUseCaseImpl(
storage: initPropertiesStorage
Expand All @@ -138,6 +138,7 @@ internal class AffiseComponent: AffiseApi {
)

lazy var stringToSha256Converter: StringToSHA256Converter = StringToSHA256Converter()
lazy var providersToJsonStringConverter: ProvidersToJsonStringConverter = ProvidersToJsonStringConverter()
lazy var deviceUseCase: DeviceUseCase = DeviceUseCaseImpl()

/**
Expand All @@ -163,4 +164,16 @@ internal class AffiseComponent: AffiseApi {
logsManager: logsManager,
postBackModelFactory: postBackModelFactory
)

lazy var debugValidateUseCase: DebugValidateUseCase = DebugValidateUseCaseImpl(
initProperties: initProperties,
postBackModelFactory: postBackModelFactory,
logsManager: logsManager,
networkService: networkService,
converter: providersToJsonStringConverter
)
lazy var debugNetworkUseCase: DebugNetworkUseCase = DebugNetworkUseCaseImpl(
initProperties: initProperties,
networkService: networkService
)
}
2 changes: 1 addition & 1 deletion AffiseAttributionLib/Classes/AffiseShared.swift
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public class AffiseShared: NSObject {
* Get module status
*/
@available(*, deprecated, message: "use Affise.getStatus(_ module: _ onComplete:)")
public func getStatus(_ module: AffiseModules, onComplete: @escaping (_ data: [AffiseKeyValue]) -> Void) {
public func getStatus(_ module: AffiseModules, onComplete: @escaping OnKeyValueCallback) {
Affise.getStatus(module, onComplete)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import AffiseAttributionLib


class ProvidersToJsonStringConverter: Converter {
public class ProvidersToJsonStringConverter: Converter {

typealias T = [Provider]
typealias R = String
public typealias T = [Provider]
public typealias R = String

func convert(from: [Provider]) -> String {
public func convert(from: [Provider]) -> String {
let result = from.mapProviders().map {(key, value) in
(key.provider, value)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
protocol DebugNetworkUseCase {
func onRequest(_ onComplete: @escaping DebugOnNetworkCallback)
}
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)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
public typealias DebugOnNetworkCallback = (_ request: HttpRequest, _ response: HttpResponse) -> Void
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
public typealias DebugOnValidateCallback = (_ status: ValidationStatus) -> Void
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
protocol DebugValidateUseCase {
func validate(_ onComplete: @escaping DebugOnValidateCallback)
}
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 AffiseAttributionLib/Classes/debug/validate/ValidationStatus.swift
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 }
}
}
30 changes: 22 additions & 8 deletions AffiseAttributionLib/Classes/errors/Errors.swift
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()
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/**
* Event Contact
*
* @property userData any custom data.
* @property timeStampMillis the timestamp event in milliseconds.
*/
@objc
public class ContactEvent : NativeEvent {
Expand Down
Loading

0 comments on commit f41f1eb

Please sign in to comment.