api-swift / Sources / IrdAPI / Core / RawAPI.swift
231 words
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import Foundation

/// Untyped access: any spec path (with the client's base URL, credential and gate), or
/// an absolute URL on another host (node-hosted endpoints such as upload grants).
public struct RawAPI: Sendable {
    let client: IrdClient

    /// Calls `method path` with the base URL, credential and gate of the client. `path`
    /// may be a template (`/tunnels/{id}`) filled from `params`, which is also how the
    /// gate table recognises a gated mutation.
    public func request(method: HTTPMethod, path: String, params: [String: String] = [:], query: [QueryItem] = [],
                        body: JSONValue? = nil, security: IrdSecurity = .keyOrJwt) async throws -> JSONValue {
        let op = IrdOperation(method: method, pathTemplate: path, module: "raw", name: "\(method.rawValue) \(path)", security: security)
        return try await client.perform(op, params: params, query: query, body: body, gated: true).json
    }

    /// Calls an absolute URL: `Accept: application/json`, the given bearer when non-nil,
    /// no gate, no base URL. Non-2xx answers throw `.api` like every other call.
    public func request(method: HTTPMethod, url: URL, token: String?, query: [QueryItem] = [],
                        headers: [String: String] = [:], body: Data? = nil,
                        contentType: String = "application/octet-stream") async throws -> JSONValue {
        guard let full = URL(string: url.absoluteString + RequestBuilder.queryString(query)) else {
            throw IrdError.config("invalid URL \(url)")
        }
        let request = RequestBuilder.build(url: full, method: method, token: token, body: body, contentType: contentType,
                                           headers: headers, timeout: client.configuration.timeout)
        let response = try await client.configuration.transport.send(request)
        return try ResponseDecoder.decode(status: response.status, body: response.body).json
    }
}

static mirror of HEAD · about · clone: git clone https://ird.cod-sursa.ro/api-swift.git