-
Notifications
You must be signed in to change notification settings - Fork 646
Add procedure HTTP request API for WASM modules and the Rust module bindings library #3684
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
92311a9
Make procedures stuff in `bindings` be unstable
gefjon 5c1cd43
Add procedure HTTP request API
gefjon ac8ceb3
fmt, clippy
gefjon a25a14e
insta snaps
gefjon e13569d
feature-flag doc comments
gefjon 4b09d3c
Oh boy, more snaps!
gefjon 06906ed
Merge remote-tracking branch 'origin/master' into phoebe/procedure/ht…
gefjon 9696a88
trybuild: it's like insta, but different!
gefjon 21786f7
Remove UI test diagnostic that's suspiciously absent in CI
gefjon ccfef17
Merge remote-tracking branch 'origin/master' into phoebe/procedure/ht…
coolreader18 166cafe
Move Body out of HttpRequest and remove enums
coolreader18 694e803
Move procedure_http_request logic to InstanceEnv
coolreader18 b676ea9
Remove more wrappers, update doc comments to reflect different compat…
gefjon 940a862
snaps, lints
gefjon 93cc616
Merge remote-tracking branch 'origin/master' into phoebe/procedure/ht…
gefjon 22c24f1
Comments and minor changes from Mazdak's review.
gefjon fd75c7f
Return `BSATN_DECODE_ERROR` rather than trapping
gefjon 723420b
Add sdk-test tests for procedures which do HTTP requests
gefjon 254d4bc
clippy
gefjon 2d1318b
More lints
gefjon 28e682c
UI trybuild tests, and `cfg_attr` a function that's unused without un…
gefjon 9fc3c39
Additional docs
gefjon c5af956
Add imports to broken doc tests
gefjon 7feca00
Fix borrowck in doc tests
gefjon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,210 @@ | ||
| //! Types and utilities for performing HTTP requests in [procedures](crate::procedure). | ||
| //! | ||
| //! Perform an HTTP request using methods on [`crate::ProcedureContext::http`], | ||
| //! which is of type [`HttpClient`]. | ||
| //! The [`get`](HttpClient::get) helper can be used for simple `GET` requests, | ||
| //! while [`send`](HttpClient::send) allows more complex requests with headers, bodies and other methods. | ||
|
|
||
| use bytes::Bytes; | ||
| pub use http::{Request, Response}; | ||
| pub use spacetimedb_lib::http::{Error, Timeout}; | ||
|
|
||
| use crate::{ | ||
| rt::{read_bytes_source_as, read_bytes_source_into}, | ||
| IterBuf, | ||
| }; | ||
| use spacetimedb_lib::{bsatn, http as st_http}; | ||
|
|
||
| /// Allows performing HTTP requests via [`HttpClient::send`] and [`HttpClient::get`]. | ||
| /// | ||
| /// Access an `HttpClient` from within [procedures](crate::procedure) | ||
| /// via [the `http` field of the `ProcedureContext`](crate::ProcedureContext::http). | ||
| #[non_exhaustive] | ||
| pub struct HttpClient {} | ||
|
|
||
| impl HttpClient { | ||
| /// Send the HTTP request `request` and wait for its response. | ||
| /// | ||
| /// For simple `GET` requests with no headers, use [`HttpClient::get`] instead. | ||
| /// | ||
| /// Include a [`Timeout`] in the [`Request::extensions`] via [`http::request::RequestBuilder::extension`] | ||
| /// to impose a timeout on the request. | ||
| /// All HTTP requests in SpacetimeDB are subject to a maximum timeout of 500 milliseconds. | ||
| /// All other extensions in `request` are ignored. | ||
| /// | ||
| /// The returned [`Response`] may have a status code other than 200 OK. | ||
| /// Callers should inspect [`Response::status`] to handle errors returned from the remote server. | ||
| /// This method returns `Err(err)` only when a connection could not be initiated or was dropped, | ||
| /// e.g. due to DNS resolution failure or an unresponsive server. | ||
| /// | ||
| /// # Example | ||
| /// | ||
| /// Send a `POST` request with the header `Content-Type: text/plain`, a string body, | ||
| /// and a timeout of 100 milliseconds, then treat the response as a string and log it: | ||
| /// | ||
| /// ```norun | ||
| /// # use spacetimedb::{procedure, ProcedureContext}; | ||
| /// # use spacetimedb::http::{Request, Timeout}; | ||
| /// # use std::time::Duration; | ||
| /// # #[procedure] | ||
| /// # fn post_somewhere(ctx: &mut ProcedureContext) { | ||
| /// let request = Request::builder() | ||
| /// .uri("https://some-remote-host.invalid/upload") | ||
| /// .method("POST") | ||
| /// .header("Content-Type", "text/plain") | ||
| /// // Set a timeout of 100 ms, further restricting the default timeout. | ||
| /// .extension(Timeout::from(Duration::from_millis(100))) | ||
| /// .body("This is the body of the HTTP request") | ||
| /// .expect("Building `Request` object failed"); | ||
| /// | ||
| /// match ctx.http.send(request) { | ||
| /// Err(err) => { | ||
| /// log::error!("HTTP request failed: {err}"); | ||
| /// }, | ||
| /// Ok(response) => { | ||
| /// let (parts, body) = response.into_parts(); | ||
| /// log::info!( | ||
| /// "Got response with status {}, body {}", | ||
| /// parts.status, | ||
| /// body.into_string_lossy(), | ||
| /// ); | ||
| /// } | ||
| /// } | ||
| /// # } | ||
| /// | ||
| /// ``` | ||
| pub fn send<B: Into<Body>>(&self, request: Request<B>) -> Result<Response<Body>, Error> { | ||
| let (request, body) = request.map(Into::into).into_parts(); | ||
| let request = st_http::Request::from(request); | ||
| let request = bsatn::to_vec(&request).expect("Failed to BSATN-serialize `spacetimedb_lib::http::Request`"); | ||
|
|
||
| match spacetimedb_bindings_sys::procedure::http_request(&request, &body.into_bytes()) { | ||
| Ok((response_source, body_source)) => { | ||
| let response = read_bytes_source_as::<st_http::Response>(response_source); | ||
| let response = | ||
| http::response::Parts::try_from(response).expect("Invalid http response returned from host"); | ||
| let mut buf = IterBuf::take(); | ||
| read_bytes_source_into(body_source, &mut buf); | ||
| let body = Body::from_bytes(buf.clone()); | ||
|
|
||
| Ok(http::Response::from_parts(response, body)) | ||
| } | ||
| Err(err_source) => { | ||
| let error = read_bytes_source_as::<st_http::Error>(err_source); | ||
| Err(error) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Send a `GET` request to `uri` with no headers and wait for the response. | ||
| /// | ||
| /// # Example | ||
| /// | ||
| /// Send a `GET` request, then treat the response as a string and log it: | ||
| /// | ||
| /// ```no_run | ||
| /// # use spacetimedb::{procedure, ProcedureContext}; | ||
| /// # #[procedure] | ||
| /// # fn get_from_somewhere(ctx: &mut ProcedureContext) { | ||
| /// match ctx.http.get("https://some-remote-host.invalid/download") { | ||
| /// Err(err) => { | ||
| /// log::error!("HTTP request failed: {err}"); | ||
| /// } | ||
| /// Ok(response) => { | ||
| /// let (parts, body) = response.into_parts(); | ||
| /// log::info!( | ||
| /// "Got response with status {}, body {}", | ||
| /// parts.status, | ||
| /// body.into_string_lossy(), | ||
| /// ); | ||
| /// } | ||
| /// } | ||
| /// # } | ||
| /// ``` | ||
| pub fn get(&self, uri: impl TryInto<http::Uri, Error: Into<http::Error>>) -> Result<Response<Body>, Error> { | ||
| self.send( | ||
| http::Request::builder() | ||
| .method("GET") | ||
| .uri(uri) | ||
| .body(Body::empty()) | ||
| .map_err(|err| Error::from_display(&err))?, | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| /// Represents the body of an HTTP request or response. | ||
| pub struct Body { | ||
| inner: BodyInner, | ||
| } | ||
|
|
||
| impl Body { | ||
| /// Treat the body as a sequence of bytes. | ||
| pub fn into_bytes(self) -> Bytes { | ||
| match self.inner { | ||
| BodyInner::Bytes(bytes) => bytes, | ||
| } | ||
| } | ||
|
|
||
| /// Convert the body into a [`String`], erroring if it is not valid UTF-8. | ||
| pub fn into_string(self) -> Result<String, std::string::FromUtf8Error> { | ||
| String::from_utf8(self.into_bytes().into()) | ||
| } | ||
|
|
||
| /// Convert the body into a [`String`], replacing invalid UTF-8 with | ||
| /// `U+FFFD REPLACEMENT CHARACTER`, which looks like this: �. | ||
| /// | ||
| /// See [`String::from_utf8_lossy`] for more details on the conversion. | ||
| pub fn into_string_lossy(self) -> String { | ||
| self.into_string() | ||
| .unwrap_or_else(|e| String::from_utf8_lossy(e.as_bytes()).into_owned()) | ||
| } | ||
|
|
||
| /// Construct a `Body` consisting of `bytes`. | ||
| pub fn from_bytes(bytes: impl Into<Bytes>) -> Body { | ||
| Body { | ||
| inner: BodyInner::Bytes(bytes.into()), | ||
| } | ||
| } | ||
|
|
||
| /// An empty body, suitable for a `GET` request. | ||
| pub fn empty() -> Body { | ||
| ().into() | ||
| } | ||
|
|
||
| /// Is `self` exactly zero bytes? | ||
| pub fn is_empty(&self) -> bool { | ||
| match &self.inner { | ||
| BodyInner::Bytes(bytes) => bytes.is_empty(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl Default for Body { | ||
| fn default() -> Self { | ||
| Self::empty() | ||
| } | ||
| } | ||
|
|
||
| macro_rules! impl_body_from_bytes { | ||
| ($bytes:ident : $t:ty => $conv:expr) => { | ||
| impl From<$t> for Body { | ||
| fn from($bytes: $t) -> Body { | ||
| Body::from_bytes($conv) | ||
| } | ||
| } | ||
| }; | ||
| ($t:ty) => { | ||
| impl_body_from_bytes!(bytes : $t => bytes); | ||
| }; | ||
| } | ||
|
|
||
| impl_body_from_bytes!(String); | ||
| impl_body_from_bytes!(Vec<u8>); | ||
| impl_body_from_bytes!(Box<[u8]>); | ||
| impl_body_from_bytes!(&'static [u8]); | ||
| impl_body_from_bytes!(&'static str); | ||
| impl_body_from_bytes!(_unit: () => Bytes::new()); | ||
|
|
||
| enum BodyInner { | ||
| Bytes(Bytes), | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.