canic_core/api/ic/
http.rs

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