Files
purchases-ios/Sources/Networking/HTTPClient/HTTPResponseBody.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

75 lines
2.0 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
//
// HTTPResponseBody.swift
//
// Created by Nacho Soto on 3/30/22.
import Foundation
/// The content of an `HTTPResponse`
/// - Note: this can be removed in favor of `Decodable` when all responses implement `Decodable`.
protocol HTTPResponseBody {
static func create(with data: Data) throws -> Self
/// Returns a copy of this response body updating only the request date
/// This is useful for types that include a response date (like `CustomerInfo`), that need to
/// get the most up-to-date time coming from the response header.
///
/// - Note: The default implementation is a no-op.
func copy(with newRequestDate: Date) -> Self
}
extension HTTPResponseBody {
func copy(with newRequestDate: Date) -> Self { return self }
}
/// An empty `HTTPResponseBody` for responses with no content.
/// This can be used to obtain an `HTTPResponse` where the content of the response does not matter.
struct HTTPEmptyResponseBody: HTTPResponseBody {
static func create(with data: Data) throws -> HTTPEmptyResponseBody {
return .init()
}
}
// MARK: - Implementations
/// Default implementation of `HTTPResponseBody` for `Data`
extension Data: HTTPResponseBody {
static func create(with data: Data) throws -> Data {
return data
}
}
/// Default implementation of `HTTPResponseBody` for any `Decodable`
extension Decodable {
static func create(with data: Data) throws -> Self {
return try JSONDecoder.default.decode(jsonData: data)
}
}
/// Default implementation of `HTTPResponseBody` for an `Optional<HTTPResponseBody>`
extension Optional: HTTPResponseBody where Wrapped: HTTPResponseBody {
static func create(with data: Data) throws -> Wrapped? {
return try Wrapped.create(with: data)
}
}