diff --git a/client/CHANGELOG.md b/client/CHANGELOG.md index cdd704f..40a52c0 100644 --- a/client/CHANGELOG.md +++ b/client/CHANGELOG.md @@ -4,6 +4,10 @@ All significant changes to the `percase-client` crate will be documented in this ## Unreleased +### New features + +* Added `Client::put_owned` to avoid an extra copy when the caller has ownership of the data. + ## v0.3.0 (2025-10-14) ### Breaking changes diff --git a/client/src/client.rs b/client/src/client.rs index 7db128c..ce502c4 100644 --- a/client/src/client.rs +++ b/client/src/client.rs @@ -17,6 +17,7 @@ use std::time::Duration; use std::time::Instant; use fastrace_reqwest::traceparent_headers; +use reqwest::Body; use reqwest::StatusCode; use reqwest::Url; use reqwest::redirect::Policy; @@ -155,6 +156,35 @@ impl Client { } } + /// Set the value associated with the given key. + /// + /// This method exists to avoid an extra copy when the caller has ownership of the data: + /// + /// * `&'static str` + /// * `Vec` + /// * `bytes::Bytes` + /// * `reqwest::Body` + pub async fn put_owned>(&self, key: &str, value: T) -> Result<(), Error> { + self.update_route_table_if_needed().await?; + + let url = self.route(key).join(key).map_err(make_opaque_error)?; + + let resp = self + .client + .put(url) + .headers(traceparent_headers()) + .body(value) + .send() + .await + .map_err(make_opaque_error)?; + + match resp.status() { + StatusCode::OK | StatusCode::CREATED => Ok(()), + StatusCode::TOO_MANY_REQUESTS => Err(Error::TooManyRequests), + status => Err(make_opaque_error(status)), + } + } + /// Delete the value associated with the given key. pub async fn delete(&self, key: &str) -> Result<(), Error> { self.update_route_table_if_needed().await?;