Files
purchases-ios/Sources/Networking/HTTPClient/HTTPRequest.swift
T
NachoSoto 833524314d HTTPClient: extracted HTTPRequestPath protocol (#2986)
This paves the way to add a second set of paths with a separate server
URL to be able to shed requests to offerings and product entitlement
mapping to a separate server.

### Changes:
- New `HTTPRequestPath` with the same properties as `HTTPRequest.Path` +
a new `serverHostURL`
- Moved proxy logic from `SystemInfo`/`HTTPClient` to `HTTPRequestPath`
- Simplified `HTTPRequest` implementation
2023-08-10 15:23:13 -07:00

75 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
//
// HTTPRequest.swift
//
// Created by Nacho Soto on 2/27/22.
import Foundation
/// A request to be made by ``HTTPClient``
struct HTTPRequest {
typealias Headers = [String: String]
var method: Method
var path: HTTPRequestPath
/// If present, this will be used by the server to compute a checksum of the response signed with a private key.
var nonce: Data?
init(method: Method, path: HTTPRequest.Path, nonce: Data? = nil) {
self.init(method: method, requestPath: path, nonce: nonce)
}
private init(method: Method, requestPath: HTTPRequestPath, nonce: Data? = nil) {
assert(nonce == nil || nonce?.count == Data.nonceLength,
"Invalid nonce: \(nonce?.description ?? "")")
self.method = method
self.path = requestPath
self.nonce = nonce
}
}
// MARK: - Method
extension HTTPRequest {
enum Method {
case get
case post(HTTPRequestBody)
}
}
extension HTTPRequest {
var requestBody: HTTPRequestBody? {
switch self.method {
case let .post(body): return body
case .get: return nil
}
}
}
extension HTTPRequest.Method {
var httpMethod: String {
switch self {
case .get: return "GET"
case .post: return "POST"
}
}
}