2022-03-04 13:42:23 -08:00
|
|
|
//
|
|
|
|
|
// 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.
|
|
|
|
|
|
2023-02-08 09:05:20 -08:00
|
|
|
import Foundation
|
|
|
|
|
|
2022-03-04 13:42:23 -08:00
|
|
|
/// A request to be made by ``HTTPClient``
|
|
|
|
|
struct HTTPRequest {
|
|
|
|
|
|
2023-02-22 11:30:20 -08:00
|
|
|
typealias Headers = [String: String]
|
|
|
|
|
|
2023-02-16 10:27:04 -07:00
|
|
|
var method: Method
|
2023-08-10 15:23:13 -07:00
|
|
|
var path: HTTPRequestPath
|
2023-02-08 09:05:20 -08:00
|
|
|
/// If present, this will be used by the server to compute a checksum of the response signed with a private key.
|
2023-02-16 10:27:04 -07:00
|
|
|
var nonce: Data?
|
2023-02-08 09:05:20 -08:00
|
|
|
|
2023-08-10 15:23:13 -07:00
|
|
|
init(method: Method, path: HTTPRequest.Path, nonce: Data? = nil) {
|
|
|
|
|
self.init(method: method, requestPath: path, nonce: nonce)
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-08 13:21:06 -07:00
|
|
|
init(method: Method, path: HTTPRequest.PaywallPath, nonce: Data? = nil) {
|
|
|
|
|
self.init(method: method, requestPath: path, nonce: nonce)
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-10 13:00:48 +02:00
|
|
|
init(method: Method, path: HTTPRequest.DiagnosticsPath, nonce: Data? = nil) {
|
|
|
|
|
self.init(method: method, requestPath: path, nonce: nonce)
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-10 15:23:13 -07:00
|
|
|
private init(method: Method, requestPath: HTTPRequestPath, nonce: Data? = nil) {
|
2023-02-08 09:05:20 -08:00
|
|
|
assert(nonce == nil || nonce?.count == Data.nonceLength,
|
|
|
|
|
"Invalid nonce: \(nonce?.description ?? "")")
|
|
|
|
|
|
|
|
|
|
self.method = method
|
2023-08-10 15:23:13 -07:00
|
|
|
self.path = requestPath
|
2023-02-08 09:05:20 -08:00
|
|
|
self.nonce = nonce
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-16 10:27:04 -07:00
|
|
|
}
|
|
|
|
|
|
2022-03-04 13:42:23 -08:00
|
|
|
// MARK: - Method
|
|
|
|
|
|
|
|
|
|
extension HTTPRequest {
|
|
|
|
|
|
|
|
|
|
enum Method {
|
|
|
|
|
|
|
|
|
|
case get
|
2023-07-13 07:45:16 -07:00
|
|
|
case post(HTTPRequestBody)
|
2022-03-04 13:42:23 -08:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extension HTTPRequest {
|
|
|
|
|
|
2023-07-13 07:45:16 -07:00
|
|
|
var requestBody: HTTPRequestBody? {
|
2022-03-04 13:42:23 -08:00
|
|
|
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"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|