component_store/
http.rs

1#[cfg(feature = "http")]
2use reqwest::blocking::Client;
3#[cfg(feature = "http")]
4use reqwest::header::{ACCEPT, USER_AGENT};
5#[cfg(feature = "http")]
6use url::Url;
7
8use crate::StoreError;
9
10#[cfg(feature = "http")]
11const USER_AGENT_VALUE: &str = concat!("greentic-component/", env!("CARGO_PKG_VERSION"));
12
13#[cfg(feature = "http")]
14pub fn build_client() -> Result<Client, StoreError> {
15    Client::builder()
16        .user_agent(USER_AGENT_VALUE)
17        .build()
18        .map_err(StoreError::from)
19}
20
21#[cfg(feature = "http")]
22pub fn fetch(client: &Client, url: &Url) -> Result<Vec<u8>, StoreError> {
23    let response = client
24        .get(url.clone())
25        .header(USER_AGENT, USER_AGENT_VALUE)
26        .header(ACCEPT, "application/wasm,application/octet-stream")
27        .send()?;
28    let response = response.error_for_status()?;
29    Ok(response.bytes()?.to_vec())
30}
31
32#[cfg(not(feature = "http"))]
33pub fn build_client() -> Result<(), StoreError> {
34    Err(StoreError::UnsupportedScheme("http".into()))
35}
36
37#[cfg(not(feature = "http"))]
38pub fn fetch(_client: &(), _url: &url::Url) -> Result<Vec<u8>, StoreError> {
39    Err(StoreError::UnsupportedScheme("http".into()))
40}