canic_core/api/ic/
http.rs

1use crate::{
2    dto::{
3        error::Error,
4        http::{HttpRequestArgs, HttpRequestResult},
5    },
6    workflow::http::HttpWorkflow,
7};
8use serde::de::DeserializeOwned;
9
10///
11/// HttpApi
12///
13/// Stable HTTP API for canic users.
14/// Enforces metrics, limits, and IC-safe defaults.
15///
16
17pub struct HttpApi;
18
19impl HttpApi {
20    /// Perform a GET request and deserialize a JSON response.
21    /// Returns an error on non-2xx status codes or JSON decode failures.
22    pub async fn get<T: DeserializeOwned>(url: &str, headers: &[(&str, &str)]) -> Result<T, Error> {
23        HttpWorkflow::get(url, headers).await.map_err(Error::from)
24    }
25
26    /// Same as `get`, with an explicit metrics label.
27    /// Returns an error on non-2xx status codes or JSON decode failures.
28    pub async fn get_with_label<T: DeserializeOwned>(
29        url: &str,
30        headers: &[(&str, &str)],
31        label: &str,
32    ) -> Result<T, Error> {
33        HttpWorkflow::get_with_label(url, headers, label)
34            .await
35            .map_err(Error::from)
36    }
37
38    /// Perform a raw HTTP request with metrics, returning the response verbatim.
39    pub async fn get_raw(args: HttpRequestArgs) -> Result<HttpRequestResult, Error> {
40        HttpWorkflow::get_raw(args).await.map_err(Error::from)
41    }
42}