|
|
@@ -9,6 +9,7 @@ use crate::response::{RawResponse, Response};
|
|
|
///
|
|
|
/// This trait abstracts over different HTTP client backends (bitreq, reqwest)
|
|
|
/// and provides a unified interface for building and sending HTTP requests.
|
|
|
+#[cfg(not(target_arch = "wasm32"))]
|
|
|
pub trait RequestBuilderExt: Sized + Send {
|
|
|
/// Add a header to the request
|
|
|
fn header(self, key: impl AsRef<str>, value: impl AsRef<str>) -> Self;
|
|
|
@@ -28,6 +29,31 @@ pub trait RequestBuilderExt: Sized + Send {
|
|
|
) -> impl std::future::Future<Output = Response<R>> + Send;
|
|
|
}
|
|
|
|
|
|
+#[cfg(target_arch = "wasm32")]
|
|
|
+/// Trait for building and sending HTTP requests
|
|
|
+///
|
|
|
+/// This trait abstracts over different HTTP client backends (bitreq, reqwest)
|
|
|
+/// and provides a unified interface for building and sending HTTP requests.
|
|
|
+pub trait RequestBuilderExt: Sized {
|
|
|
+ /// Add a header to the request
|
|
|
+ fn header(self, key: impl AsRef<str>, value: impl AsRef<str>) -> Self;
|
|
|
+
|
|
|
+ /// Set the request body as JSON
|
|
|
+ fn json<T: Serialize>(self, body: &T) -> Self;
|
|
|
+
|
|
|
+ /// Set the request body as form data
|
|
|
+ fn form<T: Serialize>(self, body: &T) -> Self;
|
|
|
+
|
|
|
+ /// Send the request and return a raw response
|
|
|
+ fn send(self) -> impl std::future::Future<Output = Response<RawResponse>>;
|
|
|
+
|
|
|
+ /// Send the request and deserialize the response as JSON
|
|
|
+ fn send_json<R: DeserializeOwned>(
|
|
|
+ self,
|
|
|
+ ) -> impl std::future::Future<Output = Response<R>>;
|
|
|
+}
|
|
|
+
|
|
|
+#[cfg(not(target_arch = "wasm32"))]
|
|
|
#[allow(clippy::manual_async_fn)]
|
|
|
impl<T: RequestBuilderExt> RequestBuilderExt for Box<T> {
|
|
|
fn header(self, key: impl AsRef<str>, value: impl AsRef<str>) -> Self {
|
|
|
@@ -52,3 +78,29 @@ impl<T: RequestBuilderExt> RequestBuilderExt for Box<T> {
|
|
|
async move { (*self).send_json().await }
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+#[cfg(target_arch = "wasm32")]
|
|
|
+#[allow(clippy::manual_async_fn)]
|
|
|
+impl<T: RequestBuilderExt> RequestBuilderExt for Box<T> {
|
|
|
+ fn header(self, key: impl AsRef<str>, value: impl AsRef<str>) -> Self {
|
|
|
+ Box::new((*self).header(key, value))
|
|
|
+ }
|
|
|
+
|
|
|
+ fn json<B: Serialize>(self, body: &B) -> Self {
|
|
|
+ Box::new((*self).json(body))
|
|
|
+ }
|
|
|
+
|
|
|
+ fn form<F: Serialize>(self, body: &F) -> Self {
|
|
|
+ Box::new((*self).form(body))
|
|
|
+ }
|
|
|
+
|
|
|
+ fn send(self) -> impl std::future::Future<Output = Response<RawResponse>> {
|
|
|
+ async move { (*self).send().await }
|
|
|
+ }
|
|
|
+
|
|
|
+ fn send_json<R: DeserializeOwned>(
|
|
|
+ self,
|
|
|
+ ) -> impl std::future::Future<Output = Response<R>> {
|
|
|
+ async move { (*self).send_json().await }
|
|
|
+ }
|
|
|
+}
|