Skip to content

Commit 3f7c839

Browse files
authored
Borrow self in RawResponse, remote into_stream (#3102)
There were problems with `BufResponse::into_stream()` masked by a build pipeline issue. Fixing the issue in a method I just added that we haven't yet released is not a priority for this release.
1 parent 2130213 commit 3f7c839

File tree

7 files changed

+33
-33
lines changed

7 files changed

+33
-33
lines changed

sdk/core/azure_core/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
- Added `http::response::BufResponseBody`, which also implements `Stream`.
1010
- Added a `Pipeline::stream()` to return a `Result<BufResponse>`.
1111
- Added `RawResponse::deconstruct()`.
12-
- Added `ResponseBody::collect_string()`.
12+
- Added `ResponseBody::into_string()`.
1313
- Added `ResponseBody::from_bytes()`.
1414
- Added the `cloud` module with types for configuring clients to use different Azure clouds.
1515
- Implemented `AsRef<[u8]>` and `Deref<Target = [u8]>` for `ResponseBody`.
@@ -25,6 +25,7 @@
2525
- Changed `RawResponse::json()` from `async` to a sync function. The body was already buffered.
2626
- Changed `RawResponse::xml()` from `async` to a sync function. The body was already buffered.
2727
- Changed `Response<T, F>` to fully sync; it holds a `RawResponse` that was already buffered entirely from the service so no longer needs or defines async functions.
28+
- Changed `ResponseBody::json()` and `xml()` to borrow `self`.
2829
- Removed `create_extensible_enum` and `create_enum` macros.
2930
- Removed `BufResponse::json()`.
3031
- Removed `BufResponse::xml()`.

sdk/cosmos/azure_data_cosmos/tests/cosmos_items.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ pub async fn item_create_read_replace_delete(context: TestContext) -> Result<(),
6868
let response = container_client
6969
.create_item("Partition1", &item, None)
7070
.await?;
71-
let body = response.into_raw_body().collect_string()?;
71+
let body = response.into_raw_body().into_string()?;
7272
assert_eq!("", body);
7373

7474
// Try to read the item
@@ -85,7 +85,7 @@ pub async fn item_create_read_replace_delete(context: TestContext) -> Result<(),
8585
let response = container_client
8686
.replace_item("Partition1", "Item1", &item, None)
8787
.await?;
88-
let body = response.into_raw_body().collect_string()?;
88+
let body = response.into_raw_body().into_string()?;
8989
assert_eq!("", body);
9090

9191
// Update again, but this time ask for the response
@@ -110,7 +110,7 @@ pub async fn item_create_read_replace_delete(context: TestContext) -> Result<(),
110110
let response = container_client
111111
.delete_item("Partition1", "Item1", None)
112112
.await?;
113-
let body = response.into_raw_body().collect_string()?;
113+
let body = response.into_raw_body().into_string()?;
114114
assert_eq!("", body);
115115

116116
// Try to read the item again, expecting a 404

sdk/storage/azure_storage_blob/tests/streaming.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ async fn upload<const CONTENT_LENGTH: usize>(client: &BlobClient) -> azure_core:
4646
#[tracing::instrument(skip_all, fields(content_length), err)]
4747
async fn download(client: &BlobClient) -> azure_core::Result<u64> {
4848
let mut len = 0;
49-
let mut response = client.download(None).await?.into_stream();
49+
let mut response = client.download(None).await?.into_body();
5050
while let Some(data) = response.try_next().await? {
5151
tracing::debug!("received {} bytes", data.len());
5252

sdk/typespec/src/http/response.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl ResponseBody {
6969
}
7070

7171
/// Collect the stream into a [`String`].
72-
pub fn collect_string(self) -> crate::Result<String> {
72+
pub fn into_string(self) -> crate::Result<String> {
7373
std::str::from_utf8(&self.0)
7474
.with_context(
7575
ErrorKind::DataConversion,
@@ -80,7 +80,7 @@ impl ResponseBody {
8080

8181
/// Deserialize the JSON stream into type `T`.
8282
#[cfg(feature = "json")]
83-
pub fn json<T>(self) -> crate::Result<T>
83+
pub fn json<T>(&self) -> crate::Result<T>
8484
where
8585
T: DeserializeOwned,
8686
{
@@ -89,7 +89,7 @@ impl ResponseBody {
8989

9090
/// Deserialize the XML stream into type `T`.
9191
#[cfg(feature = "xml")]
92-
pub fn xml<T>(self) -> crate::Result<T>
92+
pub fn xml<T>(&self) -> crate::Result<T>
9393
where
9494
T: DeserializeOwned,
9595
{

sdk/typespec/typespec_client_core/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
- Added `Error::with_error_fn()`.
99
- Added `http::response::BufResponseBody`, which also implements `Stream`.
1010
- Added `RawResponse::deconstruct()`.
11-
- Added `ResponseBody::collect_string()`.
11+
- Added `ResponseBody::into_string()`.
1212
- Added `ResponseBody::from_bytes()`.
1313
- Added a `Pipeline::stream()` to return a `Result<BufResponse>`.
1414
- Implemented `AsRef<[u8]>` and `Deref<Target = [u8]>` for `ResponseBody`.
@@ -23,6 +23,7 @@
2323
- Changed `RawResponse::json()` from `async` to a sync function. The body was already buffered.
2424
- Changed `RawResponse::xml()` from `async` to a sync function. The body was already buffered.
2525
- Changed `Response<T, F>` to fully sync; it holds a `RawResponse` that was already buffered entirely from the service so no longer needs or defines async functions.
26+
- Changed `ResponseBody::json()` and `xml()` to borrow `self`.
2627
- Removed `create_extensible_enum` and `create_enum` macros.
2728
- Removed `BufResponse::json()`.
2829
- Removed `BufResponse::xml()`.

sdk/typespec/typespec_client_core/examples/core_stream_response.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
1919

2020
// Normally you'd deserialize into a type or `collect()` the body,
2121
// but this better simulates fetching multiple chunks from a slow response.
22-
let mut body = response.into_stream();
22+
let mut body = response.into_body();
2323
while let Some(data) = body.next().await {
2424
// Assume bytes are a string in this example.
2525
let page = String::from_utf8(data?.into())?;

sdk/typespec/typespec_client_core/src/http/response.rs

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ impl<T, F> From<Response<T, F>> for RawResponse {
194194
/// The type parameter `T` is a marker type that identifies trait to deserialize defined headers;
195195
/// otherwise, it is the unit type `()` if no headers are defined.
196196
///
197-
/// Given an `AsyncResponse<T>`, a user can access the raw [`BufResponseBody`] using [`AsyncResponse::into_raw_body`].
197+
/// Given an `AsyncResponse<T>`, a user can access the raw [`BufResponseBody`] using [`AsyncResponse::into_body`].
198198
pub struct AsyncResponse<T = ()> {
199199
raw: BufResponse,
200200
phantom: PhantomData<T>,
@@ -226,33 +226,13 @@ impl<T> AsyncResponse<T> {
226226
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
227227
/// let response: AsyncResponse = unimplemented!();
228228
/// let body: Vec<u8> = response
229-
/// .into_raw_body()
229+
/// .into_body()
230230
/// .collect()
231231
/// .await?
232232
/// .to_vec();
233233
/// # Ok(()) }
234234
/// ```
235-
pub fn into_raw_body(self) -> BufResponseBody {
236-
self.raw.into_body()
237-
}
238-
239-
/// Get a [`Stream`] over the response body.
240-
///
241-
/// # Examples
242-
///
243-
/// ```no_run
244-
/// use typespec_client_core::http::response::AsyncResponse;
245-
///
246-
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
247-
/// let response: AsyncResponse = unimplemented!();
248-
/// let body: Vec<u8> = response
249-
/// .into_stream()
250-
/// .collect()
251-
/// .await?
252-
/// .to_vec();
253-
/// # Ok(()) }
254-
/// ```
255-
pub fn into_stream(self) -> impl Stream<Item = crate::Result<Bytes>> {
235+
pub fn into_body(self) -> BufResponseBody {
256236
self.raw.into_body()
257237
}
258238
}
@@ -360,7 +340,9 @@ impl fmt::Debug for BufResponseBody {
360340

361341
#[cfg(test)]
362342
mod tests {
343+
use super::*;
363344
use crate::http::{headers::Headers, BufResponse, RawResponse, Response, StatusCode};
345+
use futures::stream;
364346

365347
#[test]
366348
fn can_extract_raw_body() -> Result<(), Box<dyn std::error::Error>> {
@@ -407,6 +389,22 @@ mod tests {
407389
assert_eq!(b"Hello World", &*body);
408390
}
409391

392+
#[tokio::test]
393+
async fn into_body_collects_all_bytes() {
394+
let response: AsyncResponse = BufResponse::new(
395+
StatusCode::Ok,
396+
Headers::new(),
397+
stream::iter(vec![
398+
Ok(Bytes::from_static(&[0xde, 0xad])),
399+
Ok(Bytes::from_static(&[0xbe, 0xef])),
400+
])
401+
.boxed(),
402+
)
403+
.into();
404+
let buffer: Vec<u8> = response.into_body().collect().await.unwrap().to_vec();
405+
assert_eq!(buffer, vec![0xde, 0xad, 0xbe, 0xef]);
406+
}
407+
410408
mod json {
411409
use crate::{
412410
http::{headers::Headers, BufResponse, RawResponse, Response, StatusCode},

0 commit comments

Comments
 (0)