Skip to main content

iii_helpers/
http.rs

1//! iii http helpers.
2
3use std::collections::HashMap;
4
5use serde::{Deserialize, Serialize};
6use serde_json::Value;
7
8/// HTTP method accepted by [`HttpInvocationConfig`]. Distinct from the core
9/// `builtin_triggers` HTTP method enum, which also covers HEAD/OPTIONS.
10#[derive(Debug, Clone, Serialize, Deserialize)]
11#[serde(rename_all = "UPPERCASE")]
12pub enum HttpMethod {
13    Get,
14    Post,
15    Put,
16    Patch,
17    Delete,
18}
19
20/// Authentication configuration for HTTP-invoked functions.
21///
22/// - `Hmac`: HMAC signature verification using a shared secret.
23/// - `Bearer`: Bearer token authentication.
24/// - `ApiKey`: API key sent via a custom header.
25#[derive(Debug, Clone, Serialize, Deserialize)]
26#[serde(tag = "type", rename_all = "lowercase")]
27pub enum HttpAuthConfig {
28    Hmac {
29        secret_key: String,
30    },
31    Bearer {
32        token_key: String,
33    },
34    #[serde(rename = "api_key")]
35    ApiKey {
36        header: String,
37        value_key: String,
38    },
39}
40
41/// Configuration for an HTTP-invoked function (Lambda, Cloudflare Workers, etc.).
42#[derive(Debug, Clone, Serialize, Deserialize)]
43pub struct HttpInvocationConfig {
44    /// URL to invoke.
45    pub url: String,
46    /// HTTP method. Defaults to `POST`.
47    #[serde(default = "default_http_method")]
48    pub method: HttpMethod,
49    /// Timeout in milliseconds.
50    #[serde(skip_serializing_if = "Option::is_none")]
51    pub timeout_ms: Option<u64>,
52    /// Custom headers to send with the request.
53    #[serde(default)]
54    pub headers: HashMap<String, String>,
55    /// Authentication configuration.
56    #[serde(skip_serializing_if = "Option::is_none")]
57    pub auth: Option<HttpAuthConfig>,
58}
59
60fn default_http_method() -> HttpMethod {
61    HttpMethod::Post
62}
63
64/// Buffered HTTP request received by a function handler.
65#[derive(Debug, Clone, Serialize, Deserialize)]
66pub struct HttpRequest<T = Value> {
67    /// Query-string parameters from the request URL.
68    #[serde(default)]
69    pub query_params: HashMap<String, String>,
70    /// Path parameters extracted from the matched route.
71    #[serde(default)]
72    pub path_params: HashMap<String, String>,
73    /// Request headers.
74    #[serde(default)]
75    pub headers: HashMap<String, String>,
76    /// Request path.
77    #[serde(default)]
78    pub path: String,
79    /// HTTP method of the request (e.g. `GET`, `POST`).
80    #[serde(default)]
81    pub method: String,
82    /// Parsed request body.
83    #[serde(default)]
84    pub body: T,
85}
86
87/// Buffered HTTP response returned from a function handler.
88#[derive(Debug, Clone, Serialize, Deserialize)]
89pub struct HttpResponse<T = Value> {
90    /// HTTP status code.
91    pub status_code: u16,
92    /// Response headers.
93    #[serde(default)]
94    pub headers: HashMap<String, String>,
95    /// Response body.
96    pub body: T,
97}