Files
purchases-ios/Sources/Networking/HTTPClient/HTTPResponseBody.swift
T
Joshua Liebowitz 858bef3f9b Extract Offerings-related things into OfferingsAPI (#1706)
Extract Offerings-related things into OfferingsAPI
2022-06-16 14:00:40 -07:00

62 lines
1.6 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
}
/// 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)
}
}