diff --git a/common/http-api-client/src/lib.rs b/common/http-api-client/src/lib.rs index 19826bd9cc..c826fe295b 100644 --- a/common/http-api-client/src/lib.rs +++ b/common/http-api-client/src/lib.rs @@ -324,6 +324,56 @@ impl Client { } } + pub fn create_patch_request( + &self, + path: PathSegments<'_>, + params: Params<'_, K, V>, + json_body: &B, + ) -> RequestBuilder + where + B: Serialize + ?Sized, + K: AsRef, + V: AsRef, + { + let url = sanitize_url(&self.base_url, path, params); + self.reqwest_client.patch(url).json(json_body) + } + + pub async fn send_patch_request( + &self, + path: PathSegments<'_>, + params: Params<'_, K, V>, + json_body: &B, + ) -> Result> + where + B: Serialize + ?Sized, + K: AsRef, + V: AsRef, + E: Display, + { + let url = sanitize_url(&self.base_url, path, params); + + #[cfg(target_arch = "wasm32")] + { + Ok(wasmtimer::tokio::timeout( + self.request_timeout, + self.reqwest_client.patch(url).json(json_body).send(), + ) + .await + .map_err(|_timeout| HttpClientError::RequestTimeout)??) + } + + #[cfg(not(target_arch = "wasm32"))] + { + Ok(self + .reqwest_client + .patch(url) + .json(json_body) + .send() + .await?) + } + } + #[instrument(level = "debug", skip_all)] pub async fn get_json( &self, @@ -372,6 +422,23 @@ impl Client { parse_response(res, false).await } + pub async fn patch_json( + &self, + path: PathSegments<'_>, + params: Params<'_, K, V>, + json_body: &B, + ) -> Result> + where + B: Serialize + ?Sized, + for<'a> T: Deserialize<'a>, + K: AsRef, + V: AsRef, + E: Display + DeserializeOwned, + { + let res = self.send_patch_request(path, params, json_body).await?; + parse_response(res, true).await + } + #[instrument(level = "debug", skip_all)] pub async fn get_json_endpoint(&self, endpoint: S) -> Result> where @@ -466,6 +533,42 @@ impl Client { parse_response(res, false).await } + + pub async fn patch_json_endpoint( + &self, + endpoint: S, + json_body: &B, + ) -> Result> + where + B: Serialize + ?Sized, + 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 + .patch(self.base_url.join(endpoint.as_ref())?) + .json(json_body) + .send(), + ) + .await + .map_err(|_timeout| HttpClientError::RequestTimeout)?? + }; + + #[cfg(not(target_arch = "wasm32"))] + let res = { + self.reqwest_client + .patch(self.base_url.join(endpoint.as_ref())?) + .json(json_body) + .send() + .await? + }; + + parse_response(res, true).await + } } // define those methods on the trait for nicer extensions (and not having to type the thing twice)