Files
purchases-ios/Sources/Networking/HTTPClient/HTTPResponseBody.swift
T
Rick 6330937692 feat(remote-config): support RC Container format in network stack and use for remote-config request (#7037)
* feat(remote-config): request RC Container format

* fix(remote-config): handle no content responses

* chore(remote-config): allow snapshot todo

* fix(remote-config): disable response verification

* chore(remote-config): document disabled signing

* refactor(remote-config): parse RC containers in HTTP stack
2026-06-19 14:38:38 +02:00

88 lines
2.4 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`
protocol HTTPResponseBody {
static func create(with data: Data) throws -> Self
static func create(with data: Data, httpStatusCode: HTTPStatusCode) 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 {
static func create(with data: Data, httpStatusCode: HTTPStatusCode) throws -> Self {
return try Self.create(with: data)
}
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)
}
static func create(with data: Data, httpStatusCode: HTTPStatusCode) throws -> Wrapped? {
guard httpStatusCode != .noContent else {
return nil
}
return try Wrapped.create(with: data, httpStatusCode: httpStatusCode)
}
}