Files
purchases-ios/Sources/Networking/Operations/PostAttributionDataOperation.swift
T

80 lines
2.3 KiB
Swift
Raw Normal View History

//
// 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
private let responseHandler: CustomerAPI.SimpleResponseHandler?
init(configuration: UserSpecificConfiguration,
attributionData: [String: Any],
network: AttributionNetwork,
responseHandler: CustomerAPI.SimpleResponseHandler?) {
self.attributionData = attributionData
self.network = network
self.configuration = configuration
2022-02-07 19:20:16 -07:00
self.responseHandler = responseHandler
super.init(configuration: configuration)
}
2022-02-07 19:20:16 -07:00
override func begin(completion: @escaping () -> Void) {
self.post(completion: completion)
}
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 {
self.responseHandler?(.missingAppUserID())
2022-02-07 19:20:16 -07:00
completion()
return
}
let request = HTTPRequest(method: .post(Body(network: self.network, attributionData: self.attributionData)),
path: .postAttributionData(appUserID: appUserID))
self.httpClient.perform(request) { (response: VerifiedHTTPResponse<HTTPEmptyResponseBody>.Result) in
2022-02-07 19:20:16 -07:00
defer {
completion()
}
self.responseHandler?(response.error.map(BackendError.networkError))
}
}
}
2024-10-02 11:53:45 +02:00
// Restating inherited @unchecked Sendable from Foundation's Operation
extension PostAttributionDataOperation: @unchecked Sendable {}
private extension PostAttributionDataOperation {
struct Body: HTTPRequestBody {
let network: AttributionNetwork
let data: AnyEncodable
init(network: AttributionNetwork, attributionData: [String: Any]) {
self.network = network
self.data = AnyEncodable(attributionData)
}
}
}