Skip to main content

canic_core/api/ic/
http.rs

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