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
|
|
|
|
|
//
|
|
|
|
|
// PostSubscriberAttributesOperation.swift
|
|
|
|
|
//
|
|
|
|
|
// Created by Joshua Liebowitz on 11/18/21.
|
|
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
|
|
|
|
class PostSubscriberAttributesOperation: NetworkOperation {
|
|
|
|
|
|
|
|
|
|
private let configuration: UserSpecificConfiguration
|
2022-06-17 10:00:51 -07:00
|
|
|
private let subscriberAttributes: SubscriberAttribute.Dictionary
|
2022-06-16 14:00:40 -07:00
|
|
|
private let responseHandler: CustomerAPI.SimpleResponseHandler?
|
2022-01-28 13:36:43 -08:00
|
|
|
|
|
|
|
|
init(configuration: UserSpecificConfiguration,
|
2022-06-17 10:00:51 -07:00
|
|
|
subscriberAttributes: SubscriberAttribute.Dictionary,
|
2022-06-16 14:00:40 -07:00
|
|
|
completion: CustomerAPI.SimpleResponseHandler?) {
|
2022-01-28 13:36:43 -08:00
|
|
|
self.configuration = configuration
|
|
|
|
|
self.subscriberAttributes = subscriberAttributes
|
2022-02-07 19:20:16 -07:00
|
|
|
self.responseHandler = completion
|
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) {
|
|
|
|
|
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) {
|
2022-01-28 13:36:43 -08:00
|
|
|
guard self.subscriberAttributes.count > 0 else {
|
|
|
|
|
Logger.warn(Strings.attribution.empty_subscriber_attributes)
|
2022-04-12 15:11:22 -07:00
|
|
|
self.responseHandler?(.emptySubscriberAttributes())
|
2022-02-07 19:20:16 -07:00
|
|
|
|
2022-01-28 13:36:43 -08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
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(self.subscriberAttributes)),
|
2022-03-04 13:42:23 -08:00
|
|
|
path: .postSubscriberAttributes(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 PostSubscriberAttributesOperation: @unchecked Sendable {}
|
|
|
|
|
|
2023-07-13 07:45:16 -07:00
|
|
|
private extension PostSubscriberAttributesOperation {
|
2022-03-08 12:56:13 -08:00
|
|
|
|
2023-07-13 07:45:16 -07:00
|
|
|
struct Body: HTTPRequestBody {
|
2022-03-08 12:56:13 -08:00
|
|
|
|
|
|
|
|
let attributes: AnyEncodable
|
|
|
|
|
|
2022-06-17 10:00:51 -07:00
|
|
|
init(_ attributes: SubscriberAttribute.Dictionary) {
|
2022-03-08 12:56:13 -08:00
|
|
|
self.attributes = AnyEncodable(
|
2022-06-17 10:00:51 -07:00
|
|
|
SubscriberAttribute.map(subscriberAttributes: attributes)
|
2022-03-08 12:56:13 -08:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|