diff --git a/common/http-api-client/src/lib.rs b/common/http-api-client/src/lib.rs index 549c843958..824f4fa278 100644 --- a/common/http-api-client/src/lib.rs +++ b/common/http-api-client/src/lib.rs @@ -282,6 +282,48 @@ impl Client { } } + pub async fn create_delete_request( + &self, + path: PathSegments<'_>, + params: Params<'_, K, V>, + ) -> RequestBuilder + where + K: AsRef, + V: AsRef, + { + let url = sanitize_url(&self.base_url, path, params); + self.reqwest_client.delete(url) + } + + pub async fn send_delete_request( + &self, + path: PathSegments<'_>, + params: Params<'_, K, V>, + ) -> Result> + where + K: AsRef, + V: AsRef, + E: Display, + { + tracing::trace!("Sending DELETE request"); + let url = sanitize_url(&self.base_url, path, params); + + #[cfg(target_arch = "wasm32")] + { + Ok(wasmtimer::tokio::timeout( + self.request_timeout, + self.reqwest_client.delete(url).send(), + ) + .await + .map_err(|_timeout| HttpClientError::RequestTimeout)??) + } + + #[cfg(not(target_arch = "wasm32"))] + { + Ok(self.reqwest_client.delete(url).send().await?) + } + } + #[instrument(level = "debug", skip_all)] pub async fn get_json( &self, @@ -315,6 +357,21 @@ impl Client { parse_response(res, true).await } + pub async fn delete_json( + &self, + path: PathSegments<'_>, + params: Params<'_, K, V>, + ) -> Result> + where + for<'a> T: Deserialize<'a>, + K: AsRef, + V: AsRef, + E: Display + DeserializeOwned, + { + let res = self.send_delete_request(path, params).await?; + parse_response(res, false).await + } + #[instrument(level = "debug", skip_all)] pub async fn get_json_endpoint(&self, endpoint: S) -> Result> where @@ -380,6 +437,35 @@ impl Client { parse_response(res, true).await } + + pub async fn delete_json_endpoint(&self, endpoint: S) -> Result> + where + for<'a> T: Deserialize<'a>, + E: Display + DeserializeOwned, + S: AsRef, + { + #[cfg(target_arch = "wasm32")] + let res = { + wasmtimer::tokio::timeout( + self.request_timeout, + self.reqwest_client + .delete(self.base_url.join(endpoint.as_ref())?) + .send(), + ) + .await + .map_err(|_timeout| HttpClientError::RequestTimeout)?? + }; + + #[cfg(not(target_arch = "wasm32"))] + let res = { + self.reqwest_client + .delete(self.base_url.join(endpoint.as_ref())?) + .send() + .await? + }; + + parse_response(res, false).await + } } // define those methods on the trait for nicer extensions (and not having to type the thing twice) @@ -412,6 +498,17 @@ pub trait ApiClient { V: AsRef + Sync, E: Display + DeserializeOwned; + async fn delete_json( + &self, + path: PathSegments<'_>, + params: Params<'_, K, V>, + ) -> Result> + where + for<'a> T: Deserialize<'a>, + K: AsRef + Sync, + V: AsRef + Sync, + E: Display + DeserializeOwned; + /// `get` json data from the provided absolute endpoint, i.e. for example `"/api/v1/mixnodes?since=12345"` async fn get_json_from(&self, endpoint: S) -> Result> where @@ -429,6 +526,12 @@ pub trait ApiClient { for<'a> T: Deserialize<'a>, E: Display + DeserializeOwned, S: AsRef + Sync + Send; + + async fn delete_json_from(&self, endpoint: S) -> Result> + where + for<'a> T: Deserialize<'a>, + E: Display + DeserializeOwned, + S: AsRef + Sync + Send; } #[cfg_attr(target_arch = "wasm32", async_trait(?Send))] @@ -464,6 +567,20 @@ impl ApiClient for Client { self.post_json(path, params, json_body).await } + async fn delete_json( + &self, + path: PathSegments<'_>, + params: Params<'_, K, V>, + ) -> Result> + where + for<'a> T: Deserialize<'a>, + K: AsRef + Sync, + V: AsRef + Sync, + E: Display + DeserializeOwned, + { + self.delete_json(path, params).await + } + async fn get_json_from(&self, endpoint: S) -> Result> where for<'a> T: Deserialize<'a>, @@ -486,6 +603,15 @@ impl ApiClient for Client { { self.post_json_endpoint(endpoint, json_body).await } + + async fn delete_json_from(&self, endpoint: S) -> Result> + where + for<'a> T: Deserialize<'a>, + E: Display + DeserializeOwned, + S: AsRef + Sync + Send, + { + self.delete_json_endpoint(endpoint).await + } } // utility function that should solve the double slash problem in API urls forever.