Files
purchases-ios/Sources/Networking/Operations/PostAttributionDataOperation.swift
T
Joshua Liebowitz 858bef3f9b Extract Offerings-related things into OfferingsAPI (#1706)
Extract Offerings-related things into OfferingsAPI
2022-06-16 14:00:40 -07:00

76 lines
2.2 KiB
Swift

//
// 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
self.responseHandler = responseHandler
super.init(configuration: configuration)
}
override func begin(completion: @escaping () -> Void) {
self.post(completion: completion)
}
private func post(completion: @escaping () -> Void) {
guard let appUserID = try? self.configuration.appUserID.escapedOrError() else {
self.responseHandler?(.missingAppUserID())
completion()
return
}
let request = HTTPRequest(method: .post(Body(network: self.network, attributionData: self.attributionData)),
path: .postAttributionData(appUserID: appUserID))
self.httpClient.perform(request) { (response: HTTPResponse<HTTPEmptyResponseBody>.Result) in
defer {
completion()
}
self.responseHandler?(response.error.map(BackendError.networkError))
}
}
}
private extension PostAttributionDataOperation {
struct Body: Encodable {
let network: AttributionNetwork
let data: AnyEncodable
init(network: AttributionNetwork, attributionData: [String: Any]) {
self.network = network
self.data = AnyEncodable(attributionData)
}
}
}