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")]
11pub fn build_client() -> Result<Client, StoreError> {
12    Client::builder()
13        .user_agent("greentic-component/0.1")
14        .build()
15        .map_err(StoreError::from)
16}
17
18#[cfg(feature = "http")]
19pub fn fetch(client: &Client, url: &Url) -> Result<Vec<u8>, StoreError> {
20    let response = client
21        .get(url.clone())
22        .header(USER_AGENT, "greentic-component/0.1")
23        .header(ACCEPT, "application/wasm,application/octet-stream")
24        .send()?;
25    let response = response.error_for_status()?;
26    Ok(response.bytes()?.to_vec())
27}
28
29#[cfg(not(feature = "http"))]
30pub fn build_client() -> Result<(), StoreError> {
31    Err(StoreError::UnsupportedScheme("http".into()))
32}
33
34#[cfg(not(feature = "http"))]
35pub fn fetch(_client: &(), _url: &url::Url) -> Result<Vec<u8>, StoreError> {
36    Err(StoreError::UnsupportedScheme("http".into()))
37}