Files
purchases-ios/Sources/Networking/Operations/PostSubscriberAttributesOperation.swift
T
NachoSoto f7e36fb993 Trusted Entitlements: add support for signing POST body (#2753)
### Changes:
- `HTTPClient` has a new `X-Post-Params-Hash` request header
- Replaced `HTTPRequest.Method.post`'s `Encodable` value with a more
specific `HTTPRequestBody` (like `HTTPResponseBody`)
- `HTTPRequestBody` allows overriding `contentForSignature` (it defaults
to `[]`) which then `Signing` uses for the hash
- Fixed `LogInOperation` not passing the `VerificationResult` to the
result `CustomerInfo`. This is captured in unit and integration tests
now.
- Add support for signing `POST` requests in `Path.logIn` and
`Path.postReceiptData`

Depends on https://github.com/RevenueCat/khepri/pull/6246
2023-07-13 07:45:16 -07:00

82 lines
2.3 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
//
// 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
self.responseHandler = completion
super.init(configuration: configuration)
}
override func begin(completion: @escaping () -> Void) {
post(completion: completion)
}
private func post(completion: @escaping () -> Void) {
guard self.subscriberAttributes.count > 0 else {
Logger.warn(Strings.attribution.empty_subscriber_attributes)
self.responseHandler?(.emptySubscriberAttributes())
return
}
let appUserID = self.configuration.appUserID
guard appUserID.isNotEmpty else {
self.responseHandler?(.missingAppUserID())
completion()
return
}
let request = HTTPRequest(method: .post(Body(self.subscriberAttributes)),
path: .postSubscriberAttributes(appUserID: appUserID))
self.httpClient.perform(request) { (response: VerifiedHTTPResponse<HTTPEmptyResponseBody>.Result) in
defer {
completion()
}
self.responseHandler?(response.error.map(BackendError.networkError))
}
}
}
private extension PostSubscriberAttributesOperation {
struct Body: HTTPRequestBody {
let attributes: AnyEncodable
init(_ attributes: SubscriberAttribute.Dictionary) {
self.attributes = AnyEncodable(
SubscriberAttribute.map(subscriberAttributes: attributes)
)
}
}
}