grafbase_hooks/host_io/
http.rs

1//! A HTTP client module for executing HTTP requests in the host runtime.
2//! The functions are blocking from the guest, but are run asynchronously in the host.
3//! While the request is being executed, the host thread can continue to run other code.
4
5use crate::wit::HttpClient;
6pub use crate::wit::{HttpError, HttpMethod, HttpRequest, HttpResponse, HttpVersion};
7
8impl HttpMethod {
9    /// Returns string slice representation of HTTP Method.
10    pub fn as_str(&self) -> &str {
11        match self {
12            HttpMethod::Get => "GET",
13            HttpMethod::Post => "POST",
14            HttpMethod::Put => "PUT",
15            HttpMethod::Delete => "DELETE",
16            HttpMethod::Patch => "PATCH",
17            HttpMethod::Head => "HEAD",
18            HttpMethod::Options => "OPTIONS",
19            HttpMethod::Trace => "TRACE",
20            HttpMethod::Connect => "CONNECT",
21        }
22    }
23}
24
25/// Executes a single HTTP request in the host runtime.
26pub fn execute(request: &HttpRequest) -> Result<HttpResponse, HttpError> {
27    HttpClient::execute(request)
28}
29
30/// Executes multiple HTTP requests in the host runtime in parallel.
31/// Returns results in the same order as the requests.
32pub fn execute_many(requests: &[HttpRequest]) -> Vec<Result<HttpResponse, HttpError>> {
33    HttpClient::execute_many(requests)
34}