Files
purchases-ios/Sources/Networking/Operations/Handling/CustomerInfoResponseHandler.swift
T
NachoSoto da9ba397a3 Update CustomerInfo.requestDate from 304 responses (#2310)
### Purpose:

This is a prerequisite for #2288. The new grace period means that if we
didn't update the date from the cached responses, entitlements would
become stale and expired after 3 days.

### Changes:
- Added `HTTPResponseBody.copy(with newRequestDate:)` to be able to
modify responses from the header request date
- `CustomerInfo` implements this method to modify its request date
- Added `HTTPResponse.requestDate` to be able to keep track of the
request date from the server
- Added `VerificationResult.from(cache:response:)` to determine the most
restrictive verification result based on what's cached or checked from a
response
- Added `HTTPResponse.copy(with:)` to modify the verification result of
a response using the previous method
- `HTTPClient` now uses the most restrictive verification result
- `HTTPClient` updates request date from server responses or cached
responses (unless verification failed). This was the missing piece for
#2288.

### Tests:
- Verify that `ETagManager` does not use an ETag if verification was
previously not enabled
- Verify that `ETagManager` returns the request date from the server
when returning a cached response
- Verify that `HTTPClient` updates the request date from the server or
from a cached response
- Verify that `HTTPClient` does not update request date if verification
failed
- Tests for `HTTPResponse` request date parsing
- Test for `CustomerInfoResponseHandler` updating request date
- Tests for `CustomerInfo.copy(with newRequestDate:)`


### Other smaller changes:
- Moved `CustomerInfo.asData()` into `Encodable.asJSONEncodedData()`
- Renamed `requestTime` to `requestDate` everywhere for consistency
2023-03-01 09:24:32 -08:00

62 lines
1.8 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
//
// CustomerInfoResponseHandler.swift
//
// Created by Joshua Liebowitz on 11/18/21.
import Foundation
class CustomerInfoResponseHandler {
init() { }
func handle(customerInfoResponse response: HTTPResponse<Response>.Result,
completion: CustomerAPI.CustomerInfoResponseHandler) {
let result: Result<CustomerInfo, BackendError> = response
.map { response in
// If the response was successful we always want to return the `CustomerInfo`.
if !response.body.errorResponse.attributeErrors.isEmpty {
// If there are any, log attribute errors.
// Creating the error implicitly logs it.
_ = response.body.errorResponse.asBackendError(with: response.statusCode)
}
return response.body.customerInfo.copy(with: response.verificationResult)
}
.mapError(BackendError.networkError)
completion(result)
}
}
extension CustomerInfoResponseHandler {
struct Response: HTTPResponseBody {
var customerInfo: CustomerInfo
var errorResponse: ErrorResponse
static func create(with data: Data) throws -> Self {
return .init(customerInfo: try CustomerInfo.create(with: data),
errorResponse: ErrorResponse.from(data))
}
func copy(with newRequestDate: Date) -> Self {
var copy = self
copy.customerInfo = copy.customerInfo.copy(with: newRequestDate)
return copy
}
}
}