grafbase_sdk/types/
hooks.rs

1use crate::{types::Headers, wit};
2
3/// Represents the parts of an HTTP request, including the URL, method, and headers.
4#[non_exhaustive]
5pub struct HttpRequestParts {
6    /// The URL of the HTTP request.
7    pub url: String,
8    /// The HTTP method of the request, such as GET, POST, etc.
9    pub method: http::Method,
10    /// The headers of the HTTP request.
11    pub headers: Headers,
12}
13
14impl From<wit::HttpRequestParts> for HttpRequestParts {
15    fn from(parts: wit::HttpRequestParts) -> Self {
16        Self {
17            url: parts.url,
18            method: parts.method.into(),
19            headers: parts.headers.into(),
20        }
21    }
22}
23
24impl From<HttpRequestParts> for wit::HttpRequestParts {
25    fn from(parts: HttpRequestParts) -> Self {
26        Self {
27            url: parts.url,
28            method: parts.method.into(),
29            headers: parts.headers.into(),
30        }
31    }
32}
33
34/// Output type for the [on_request()](crate::HooksExtension::on_request()) hook.
35#[derive(Default)]
36pub struct OnRequestOutput {
37    pub(crate) context: Vec<u8>,
38    pub(crate) contract_key: Option<String>,
39}
40
41impl OnRequestOutput {
42    /// Creates a new [OnRequestOutput] instance with default values.
43    pub fn new() -> Self {
44        Self::default()
45    }
46
47    /// Sets the contract key for the request.
48    pub fn contract_key(mut self, contract_key: impl Into<String>) -> Self {
49        self.contract_key = Some(contract_key.into());
50        self
51    }
52
53    /// Set the Hooks context for the request.
54    /// Accessible by other extensions.
55    pub fn context(mut self, context: impl Into<Vec<u8>>) -> Self {
56        self.context = context.into();
57        self
58    }
59}