2022-01-28 13:36:43 -08:00
|
|
|
//
|
|
|
|
|
// Copyright RevenueCat Inc. All Rights Reserved.
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the MIT License (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// https://opensource.org/licenses/MIT
|
|
|
|
|
//
|
|
|
|
|
// PostAttributionDataOperation.swift
|
|
|
|
|
//
|
|
|
|
|
// Created by Joshua Liebowitz on 11/19/21.
|
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
|
|
|
|
class PostAttributionDataOperation: NetworkOperation {
|
|
|
|
|
|
|
|
|
|
private let configuration: UserSpecificConfiguration
|
|
|
|
|
private let attributionData: [String: Any]
|
|
|
|
|
private let network: AttributionNetwork
|
2022-06-16 14:00:40 -07:00
|
|
|
private let responseHandler: CustomerAPI.SimpleResponseHandler?
|
2022-01-28 13:36:43 -08:00
|
|
|
|
|
|
|
|
init(configuration: UserSpecificConfiguration,
|
|
|
|
|
attributionData: [String: Any],
|
|
|
|
|
network: AttributionNetwork,
|
2022-06-16 14:00:40 -07:00
|
|
|
responseHandler: CustomerAPI.SimpleResponseHandler?) {
|
2022-01-28 13:36:43 -08:00
|
|
|
self.attributionData = attributionData
|
|
|
|
|
self.network = network
|
|
|
|
|
self.configuration = configuration
|
2022-02-07 19:20:16 -07:00
|
|
|
self.responseHandler = responseHandler
|
2022-01-28 13:36:43 -08:00
|
|
|
|
|
|
|
|
super.init(configuration: configuration)
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-07 19:20:16 -07:00
|
|
|
override func begin(completion: @escaping () -> Void) {
|
|
|
|
|
self.post(completion: completion)
|
2022-01-28 13:36:43 -08:00
|
|
|
}
|
|
|
|
|
|
2022-02-07 19:20:16 -07:00
|
|
|
private func post(completion: @escaping () -> Void) {
|
2023-07-05 15:41:51 -07:00
|
|
|
let appUserID = self.configuration.appUserID
|
|
|
|
|
|
|
|
|
|
guard appUserID.isNotEmpty else {
|
2022-04-12 15:11:22 -07:00
|
|
|
self.responseHandler?(.missingAppUserID())
|
2022-02-07 19:20:16 -07:00
|
|
|
completion()
|
|
|
|
|
|
2022-01-28 13:36:43 -08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-08 12:56:13 -08:00
|
|
|
let request = HTTPRequest(method: .post(Body(network: self.network, attributionData: self.attributionData)),
|
2022-03-04 13:42:23 -08:00
|
|
|
path: .postAttributionData(appUserID: appUserID))
|
|
|
|
|
|
2023-06-30 12:24:53 -07:00
|
|
|
self.httpClient.perform(request) { (response: VerifiedHTTPResponse<HTTPEmptyResponseBody>.Result) in
|
2022-02-07 19:20:16 -07:00
|
|
|
defer {
|
|
|
|
|
completion()
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-12 15:11:22 -07:00
|
|
|
self.responseHandler?(response.error.map(BackendError.networkError))
|
2022-01-28 13:36:43 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2022-03-08 12:56:13 -08:00
|
|
|
|
2024-10-02 11:53:45 +02:00
|
|
|
// Restating inherited @unchecked Sendable from Foundation's Operation
|
|
|
|
|
extension PostAttributionDataOperation: @unchecked Sendable {}
|
|
|
|
|
|
2022-03-08 12:56:13 -08:00
|
|
|
private extension PostAttributionDataOperation {
|
|
|
|
|
|
2023-07-13 07:45:16 -07:00
|
|
|
struct Body: HTTPRequestBody {
|
2022-03-08 12:56:13 -08:00
|
|
|
|
|
|
|
|
let network: AttributionNetwork
|
|
|
|
|
let data: AnyEncodable
|
|
|
|
|
|
|
|
|
|
init(network: AttributionNetwork, attributionData: [String: Any]) {
|
|
|
|
|
self.network = network
|
|
|
|
|
self.data = AnyEncodable(attributionData)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|