http_client.rs

  1//! An HTTP client.
  2
  3pub use crate::wit::zed::extension::http_client::{
  4    HttpMethod, HttpRequest, HttpResponse, HttpResponseStream, RedirectPolicy, fetch, fetch_stream,
  5};
  6
  7impl HttpRequest {
  8    /// Returns a builder for an [`HttpRequest`].
  9    pub const fn builder() -> HttpRequestBuilder {
 10        HttpRequestBuilder::new()
 11    }
 12
 13    /// Executes the [`HttpRequest`] with [`fetch`].
 14    pub fn fetch(&self) -> Result<HttpResponse, String> {
 15        fetch(self)
 16    }
 17
 18    /// Executes the [`HttpRequest`] with [`fetch_stream`].
 19    pub fn fetch_stream(&self) -> Result<HttpResponseStream, String> {
 20        fetch_stream(self)
 21    }
 22}
 23
 24/// A builder for an [`HttpRequest`].
 25#[derive(Clone)]
 26pub struct HttpRequestBuilder {
 27    method: Option<HttpMethod>,
 28    url: Option<String>,
 29    headers: Vec<(String, String)>,
 30    body: Option<Vec<u8>>,
 31    redirect_policy: RedirectPolicy,
 32}
 33
 34impl Default for HttpRequestBuilder {
 35    fn default() -> Self {
 36        Self::new()
 37    }
 38}
 39
 40impl HttpRequestBuilder {
 41    /// Returns a new [`HttpRequestBuilder`].
 42    pub const fn new() -> Self {
 43        HttpRequestBuilder {
 44            method: None,
 45            url: None,
 46            headers: Vec::new(),
 47            body: None,
 48            redirect_policy: RedirectPolicy::NoFollow,
 49        }
 50    }
 51
 52    /// Sets the HTTP method for the request.
 53    pub const fn method(mut self, method: HttpMethod) -> Self {
 54        self.method = Some(method);
 55        self
 56    }
 57
 58    /// Sets the URL for the request.
 59    pub fn url(mut self, url: impl Into<String>) -> Self {
 60        self.url = Some(url.into());
 61        self
 62    }
 63
 64    /// Adds a header to the request.
 65    pub fn header(mut self, name: impl Into<String>, value: impl Into<String>) -> Self {
 66        self.headers.push((name.into(), value.into()));
 67        self
 68    }
 69
 70    /// Adds the specified headers to the request.
 71    pub fn headers(mut self, headers: impl IntoIterator<Item = (String, String)>) -> Self {
 72        self.headers.extend(headers);
 73        self
 74    }
 75
 76    /// Sets the body of the request.
 77    pub fn body(mut self, body: impl Into<Vec<u8>>) -> Self {
 78        self.body = Some(body.into());
 79        self
 80    }
 81
 82    /// Sets the redirect policy for the request.
 83    pub const fn redirect_policy(mut self, policy: RedirectPolicy) -> Self {
 84        self.redirect_policy = policy;
 85        self
 86    }
 87
 88    /// Builds the [`HttpRequest`].
 89    pub fn build(self) -> Result<HttpRequest, String> {
 90        let method = self.method.ok_or_else(|| "Method not set".to_string())?;
 91        let url = self.url.ok_or_else(|| "URL not set".to_string())?;
 92
 93        Ok(HttpRequest {
 94            method,
 95            url,
 96            headers: self.headers,
 97            body: self.body,
 98            redirect_policy: self.redirect_policy,
 99        })
100    }
101}