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

84 lines
2.5 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
//
// PostSubscriberAttributesOperation.swift
//
// Created by Joshua Liebowitz on 11/18/21.
import Foundation
class PostSubscriberAttributesOperation: NetworkOperation {
private let configuration: UserSpecificConfiguration
private let subscriberAttributes: SubscriberAttribute.Dictionary
private let responseHandler: CustomerAPI.SimpleResponseHandler?
init(configuration: UserSpecificConfiguration,
subscriberAttributes: SubscriberAttribute.Dictionary,
completion: CustomerAPI.SimpleResponseHandler?) {
self.configuration = configuration
self.subscriberAttributes = subscriberAttributes
2022-02-07 19:20:16 -07:00
self.responseHandler = completion
super.init(configuration: configuration)
}
2022-02-07 19:20:16 -07:00
override func begin(completion: @escaping () -> Void) {
post(completion: completion)
}
2022-02-07 19:20:16 -07:00
private func post(completion: @escaping () -> Void) {
guard self.subscriberAttributes.count > 0 else {
Logger.warn(Strings.attribution.empty_subscriber_attributes)
self.responseHandler?(.emptySubscriberAttributes())
2022-02-07 19:20:16 -07:00
return
}
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(self.subscriberAttributes)),
path: .postSubscriberAttributes(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 PostSubscriberAttributesOperation: @unchecked Sendable {}
private extension PostSubscriberAttributesOperation {
struct Body: HTTPRequestBody {
let attributes: AnyEncodable
init(_ attributes: SubscriberAttribute.Dictionary) {
self.attributes = AnyEncodable(
SubscriberAttribute.map(subscriberAttributes: attributes)
)
}
}
}