1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use std::{collections::HashMap, fmt};

use thiserror::Error;

use crate::{core::messages::HttpResponse, global, internal::wit};

#[derive(Error, Debug, Clone)]
/// Errors that can occur when making an HTTP request.
pub struct HttpError(pub String);
impl fmt::Display for HttpError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "HTTP error: {}", self.0)
    }
}

/// Sends an HTTP GET request to the given URL, and returns the response body.
///
/// Any errors in sending or receiving will be returned as an [HttpError].
///
/// **NOTE**: This may be replaced with `wasi-http` support in the future,
/// which will allow the use of native Rust libraries like `reqwest`.
pub async fn get(
    url: impl AsRef<str>,
    headers: Option<HashMap<String, String>>,
) -> Result<Vec<u8>, HttpError> {
    let url = url.as_ref();
    let headers = headers.unwrap_or_default().into_iter().collect::<Vec<_>>();
    let response_id = wit::server_http::get(url, &headers);

    wait_for_response(response_id).await
}

/// Sends an HTTP POST request to the given URL, and returns the response body.
///
/// Any errors in sending or receiving will be returned as an [HttpError].
///
/// **NOTE**: This may be replaced with `wasi-http` support in the future,
/// which will allow the use of native Rust libraries like `reqwest`.
pub async fn post(
    url: impl AsRef<str>,
    headers: Option<HashMap<String, String>>,
    body: Option<&[u8]>,
) -> Result<Vec<u8>, HttpError> {
    let url = url.as_ref();
    let headers = headers.unwrap_or_default().into_iter().collect::<Vec<_>>();
    let response_id = wit::server_http::post(url, &headers, body);

    wait_for_response(response_id).await
}

async fn wait_for_response(response_id: u64) -> Result<Vec<u8>, HttpError> {
    let response = global::wait_for_runtime_message(move |message: &HttpResponse| {
        message.response_id == response_id
    })
    .await;

    match response.error {
        Some(error) => Err(HttpError(error)),
        None => Ok(response.body),
    }
}