|
| 1 | +use crate::Response; |
| 2 | +use anyhow::{anyhow, Result}; |
| 3 | +use std::time::Duration; |
| 4 | +use url::Url; |
| 5 | +use wasi::http::{ |
| 6 | + outgoing_handler, |
| 7 | + types::{FieldValue, Headers, Method, OutgoingBody, OutgoingRequest, RequestOptions, Scheme}, |
| 8 | +}; |
| 9 | + |
| 10 | +pub struct RequestBuilder { |
| 11 | + method: Method, |
| 12 | + url: String, |
| 13 | + headers: Headers, |
| 14 | + body: Vec<u8>, |
| 15 | + connect_timeout: Option<u64>, |
| 16 | +} |
| 17 | + |
| 18 | +impl RequestBuilder { |
| 19 | + pub fn new(method: Method, url: &str) -> Self { |
| 20 | + Self { |
| 21 | + method, |
| 22 | + url: url.to_string(), |
| 23 | + headers: Headers::new(), |
| 24 | + body: vec![], |
| 25 | + connect_timeout: None, |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + pub fn header(self, key: &str, value: &str) -> Result<Self> { |
| 30 | + self.headers |
| 31 | + .set(&key.to_string(), &[FieldValue::from(value)])?; |
| 32 | + Ok(self) |
| 33 | + } |
| 34 | + |
| 35 | + pub fn body(mut self, body: &[u8]) -> Self { |
| 36 | + self.body = Vec::from(body); |
| 37 | + self |
| 38 | + } |
| 39 | + |
| 40 | + pub fn connect_timeout(mut self, timeout: Duration) -> Self { |
| 41 | + self.connect_timeout = Some(timeout.as_nanos() as u64); |
| 42 | + self |
| 43 | + } |
| 44 | + |
| 45 | + pub fn send(self) -> Result<Response> { |
| 46 | + let req = OutgoingRequest::new(self.headers); |
| 47 | + req.set_method(&self.method) |
| 48 | + .map_err(|()| anyhow!("failed to set method"))?; |
| 49 | + |
| 50 | + let url = Url::parse(self.url.as_str())?; |
| 51 | + let scheme = match url.scheme() { |
| 52 | + "http" => Scheme::Http, |
| 53 | + "https" => Scheme::Https, |
| 54 | + other => Scheme::Other(other.to_string()), |
| 55 | + }; |
| 56 | + req.set_scheme(Some(&scheme)) |
| 57 | + .map_err(|()| anyhow!("failed to set scheme"))?; |
| 58 | + |
| 59 | + req.set_authority(Some(url.authority())) |
| 60 | + .map_err(|()| anyhow!("failed to set authority"))?; |
| 61 | + |
| 62 | + let path = match url.query() { |
| 63 | + Some(query) => format!("{}?{query}", url.path()), |
| 64 | + None => url.path().to_string(), |
| 65 | + }; |
| 66 | + req.set_path_with_query(Some(&path)) |
| 67 | + .map_err(|()| anyhow!("failed to set path_with_query"))?; |
| 68 | + |
| 69 | + let outgoing_body = req |
| 70 | + .body() |
| 71 | + .map_err(|_| anyhow!("outgoing request write failed"))?; |
| 72 | + if !self.body.is_empty() { |
| 73 | + let request_body = outgoing_body |
| 74 | + .write() |
| 75 | + .map_err(|_| anyhow!("outgoing request write failed"))?; |
| 76 | + request_body.blocking_write_and_flush(&self.body)?; |
| 77 | + } |
| 78 | + OutgoingBody::finish(outgoing_body, None)?; |
| 79 | + |
| 80 | + let options = RequestOptions::new(); |
| 81 | + options |
| 82 | + .set_connect_timeout(self.connect_timeout) |
| 83 | + .map_err(|()| anyhow!("failed to set connect_timeout"))?; |
| 84 | + |
| 85 | + let future_response = outgoing_handler::handle(req, Some(options))?; |
| 86 | + let incoming_response = match future_response.get() { |
| 87 | + Some(result) => result.map_err(|()| anyhow!("response already taken"))?, |
| 88 | + None => { |
| 89 | + let pollable = future_response.subscribe(); |
| 90 | + pollable.block(); |
| 91 | + |
| 92 | + future_response |
| 93 | + .get() |
| 94 | + .expect("incoming response available") |
| 95 | + .map_err(|()| anyhow!("response already taken"))? |
| 96 | + } |
| 97 | + }?; |
| 98 | + drop(future_response); |
| 99 | + |
| 100 | + Response::new(incoming_response) |
| 101 | + } |
| 102 | +} |
0 commit comments