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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
//! A HTTP client for use with Crux
//!
//! `crux_http` allows Crux apps to make HTTP requests by asking the Shell to perform them.
//!
//! This is still work in progress and large parts of HTTP are not yet supported.
// #![warn(missing_docs)]
use crux_core::capability::CapabilityContext;
use http::Method;
use url::Url;
mod config;
mod error;
mod expect;
mod request;
mod request_builder;
mod response;
pub mod client;
pub mod middleware;
pub mod protocol;
pub mod testing;
pub use http_types::{self as http};
pub use self::{
config::Config,
error::HttpError,
request::Request,
request_builder::RequestBuilder,
response::{Response, ResponseAsync},
};
use client::Client;
pub type Result<T> = std::result::Result<T, HttpError>;
/// The Http capability API.
pub struct Http<Ev> {
context: CapabilityContext<protocol::HttpRequest, Ev>,
client: Client,
}
impl<Ev> crux_core::Capability<Ev> for Http<Ev> {
type Operation = protocol::HttpRequest;
type MappedSelf<MappedEv> = Http<MappedEv>;
fn map_event<F, NewEv>(&self, f: F) -> Self::MappedSelf<NewEv>
where
F: Fn(NewEv) -> Ev + Send + Sync + 'static,
Ev: 'static,
NewEv: 'static + Send,
{
Http::new(self.context.map_event(f))
}
#[cfg(feature = "typegen")]
fn register_types(generator: &mut crux_core::typegen::TypeGen) -> crux_core::typegen::Result {
generator.register_type::<HttpError>()?;
generator.register_type::<Self::Operation>()?;
generator
.register_type::<<Self::Operation as crux_core::capability::Operation>::Output>()?;
Ok(())
}
}
impl<Ev> Clone for Http<Ev> {
fn clone(&self) -> Self {
Self {
context: self.context.clone(),
client: self.client.clone(),
}
}
}
impl<Ev> Http<Ev>
where
Ev: 'static,
{
pub fn new(context: CapabilityContext<protocol::HttpRequest, Ev>) -> Self {
Self {
client: Client::new(context.clone()),
context,
}
}
/// Instruct the Shell to perform a HTTP GET request to the provided `url`.
///
/// The request can be configured via associated functions on `RequestBuilder`
/// and then sent with `RequestBuilder::send`
///
/// When finished, the response will be wrapped in an event and dispatched to
/// the app's `update function.
///
/// # Panics
///
/// This will panic if a malformed URL is passed.
///
/// # Examples
///
/// ```no_run
/// # enum Event { ReceiveResponse(crux_http::Result<crux_http::Response<Vec<u8>>>) }
/// # struct Capabilities { http: crux_http::Http<Event> }
/// # fn update(caps: &Capabilities) {
/// caps.http.get("https://httpbin.org/get").send(Event::ReceiveResponse)
/// # }
/// ```
pub fn get(&self, url: impl AsRef<str>) -> RequestBuilder<Ev> {
RequestBuilder::new(Method::Get, url.as_ref().parse().unwrap(), self.clone())
}
/// Instruct the Shell to perform a HTTP HEAD request to the provided `url`.
///
/// The request can be configured via associated functions on `RequestBuilder`
/// and then sent with `RequestBuilder::send`
///
/// When finished, the response will be wrapped in an event and dispatched to
/// the app's `update function.
///
/// # Panics
///
/// This will panic if a malformed URL is passed.
///
/// # Examples
///
/// ```no_run
/// # enum Event { ReceiveResponse(crux_http::Result<crux_http::Response<Vec<u8>>>) }
/// # struct Capabilities { http: crux_http::Http<Event> }
/// # fn update(caps: &Capabilities) {
/// caps.http.head("https://httpbin.org/get").send(Event::ReceiveResponse)
/// # }
/// ```
pub fn head(&self, url: impl AsRef<str>) -> RequestBuilder<Ev> {
RequestBuilder::new(Method::Head, url.as_ref().parse().unwrap(), self.clone())
}
/// Instruct the Shell to perform a HTTP POST request to the provided `url`.
///
/// The request can be configured via associated functions on `RequestBuilder`
/// and then sent with `RequestBuilder::send`
///
/// When finished, the response will be wrapped in an event and dispatched to
/// the app's `update function.
///
/// # Panics
///
/// This will panic if a malformed URL is passed.
///
/// # Examples
///
/// ```no_run
/// # enum Event { ReceiveResponse(crux_http::Result<crux_http::Response<Vec<u8>>>) }
/// # struct Capabilities { http: crux_http::Http<Event> }
/// # fn update(caps: &Capabilities) {
/// caps.http.post("https://httpbin.org/post").send(Event::ReceiveResponse)
/// # }
/// ```
pub fn post(&self, url: impl AsRef<str>) -> RequestBuilder<Ev> {
RequestBuilder::new(Method::Post, url.as_ref().parse().unwrap(), self.clone())
}
/// Instruct the Shell to perform a HTTP PUT request to the provided `url`.
///
/// The request can be configured via associated functions on `RequestBuilder`
/// and then sent with `RequestBuilder::send`
///
/// When finished, the response will be wrapped in an event and dispatched to
/// the app's `update function.
///
/// # Panics
///
/// This will panic if a malformed URL is passed.
///
/// # Examples
///
/// ```no_run
/// # enum Event { ReceiveResponse(crux_http::Result<crux_http::Response<Vec<u8>>>) }
/// # struct Capabilities { http: crux_http::Http<Event> }
/// # fn update(caps: &Capabilities) {
/// caps.http.put("https://httpbin.org/post").send(Event::ReceiveResponse)
/// # }
/// ```
pub fn put(&self, url: impl AsRef<str>) -> RequestBuilder<Ev> {
RequestBuilder::new(Method::Put, url.as_ref().parse().unwrap(), self.clone())
}
/// Instruct the Shell to perform a HTTP DELETE request to the provided `url`.
///
/// The request can be configured via associated functions on `RequestBuilder`
/// and then sent with `RequestBuilder::send`
///
/// When finished, the response will be wrapped in an event and dispatched to
/// the app's `update function.
///
/// # Panics
///
/// This will panic if a malformed URL is passed.
///
/// # Examples
///
/// ```no_run
/// # enum Event { ReceiveResponse(crux_http::Result<crux_http::Response<Vec<u8>>>) }
/// # struct Capabilities { http: crux_http::Http<Event> }
/// # fn update(caps: &Capabilities) {
/// caps.http.delete("https://httpbin.org/post").send(Event::ReceiveResponse)
/// # }
/// ```
pub fn delete(&self, url: impl AsRef<str>) -> RequestBuilder<Ev> {
RequestBuilder::new(Method::Delete, url.as_ref().parse().unwrap(), self.clone())
}
/// Instruct the Shell to perform a HTTP CONNECT request to the provided `url`.
///
/// The request can be configured via associated functions on `RequestBuilder`
/// and then sent with `RequestBuilder::send`
///
/// When finished, the response will be wrapped in an event and dispatched to
/// the app's `update function.
///
/// # Panics
///
/// This will panic if a malformed URL is passed.
///
/// # Examples
///
/// ```no_run
/// # enum Event { ReceiveResponse(crux_http::Result<crux_http::Response<Vec<u8>>>) }
/// # struct Capabilities { http: crux_http::Http<Event> }
/// # fn update(caps: &Capabilities) {
/// caps.http.connect("https://httpbin.org/get").send(Event::ReceiveResponse)
/// # }
/// ```
pub fn connect(&self, url: impl AsRef<str>) -> RequestBuilder<Ev> {
RequestBuilder::new(Method::Connect, url.as_ref().parse().unwrap(), self.clone())
}
/// Instruct the Shell to perform a HTTP OPTIONS request to the provided `url`.
///
/// The request can be configured via associated functions on `RequestBuilder`
/// and then sent with `RequestBuilder::send`
///
/// When finished, the response will be wrapped in an event and dispatched to
/// the app's `update function.
///
/// # Panics
///
/// This will panic if a malformed URL is passed.
///
/// # Examples
///
/// ```no_run
/// # enum Event { ReceiveResponse(crux_http::Result<crux_http::Response<Vec<u8>>>) }
/// # struct Capabilities { http: crux_http::Http<Event> }
/// # fn update(caps: &Capabilities) {
/// caps.http.options("https://httpbin.org/get").send(Event::ReceiveResponse)
/// # }
/// ```
pub fn options(&self, url: impl AsRef<str>) -> RequestBuilder<Ev> {
RequestBuilder::new(Method::Options, url.as_ref().parse().unwrap(), self.clone())
}
/// Instruct the Shell to perform a HTTP TRACE request to the provided `url`.
///
/// The request can be configured via associated functions on `RequestBuilder`
/// and then sent with `RequestBuilder::send`
///
/// When finished, the response will be wrapped in an event and dispatched to
/// the app's `update function.
///
/// # Panics
///
/// This will panic if a malformed URL is passed.
///
/// # Examples
///
/// ```no_run
/// # enum Event { ReceiveResponse(crux_http::Result<crux_http::Response<Vec<u8>>>) }
/// # struct Capabilities { http: crux_http::Http<Event> }
/// # fn update(caps: &Capabilities) {
/// caps.http.trace("https://httpbin.org/get").send(Event::ReceiveResponse)
/// # }
/// ```
pub fn trace(&self, url: impl AsRef<str>) -> RequestBuilder<Ev> {
RequestBuilder::new(Method::Trace, url.as_ref().parse().unwrap(), self.clone())
}
/// Instruct the Shell to perform a HTTP PATCH request to the provided `url`.
///
/// The request can be configured via associated functions on `RequestBuilder`
/// and then sent with `RequestBuilder::send`
///
/// When finished, the response will be wrapped in an event and dispatched to
/// the app's `update function.
///
/// # Panics
///
/// This will panic if a malformed URL is passed.
pub fn patch(&self, url: impl AsRef<str>) -> RequestBuilder<Ev> {
RequestBuilder::new(Method::Patch, url.as_ref().parse().unwrap(), self.clone())
}
/// Instruct the Shell to perform an HTTP request with the provided `method` and `url`.
///
/// The request can be configured via associated functions on `RequestBuilder`
/// and then sent with `RequestBuilder::send`
///
/// When finished, the response will be wrapped in an event and dispatched to
/// the app's `update function.
pub fn request(&self, method: http::Method, url: Url) -> RequestBuilder<Ev> {
RequestBuilder::new(method, url, self.clone())
}
}