Skip to content

Commit

Permalink
Merge pull request #24 from blazzy/send-stream-response
Browse files Browse the repository at this point in the history
Ensure stream responses are Send
  • Loading branch information
blazzy authored Aug 23, 2024
2 parents 9400be1 + dc593eb commit a50ee12
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 12 deletions.
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ jobs:

- name: Build Podman
run: |
sudo apt-get update
sudo apt-get install \
btrfs-progs \
golang-go \
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased

* `Send` constraint added to byte stream responses ([#24](https://github.com/blazzy/podman-rest-client/pull/24))

## v0.12.2 - 2024-08-21

* Improve formatting in generated docs with extra whitespace and removal redundant lines
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub fn operations(
.unwrap_or_else(|| Ok(quote! { () }))?;

let return_type = if Some(true) == success.map(|m| m.data.is_stream()) {
quote! { Pin<Box<dyn futures::stream::Stream<Item = Result<bytes::Bytes, Error>> + 'a>> }
quote! { Pin<Box<dyn futures::stream::Stream<Item = Result<bytes::Bytes, Error>> + Send + 'a>> }
} else {
quote! { Pin<Box<dyn Future<Output=Result<#response, Error>> + Send + 'a>> }
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub async fn execute_request_bytes(
pub fn execute_request_stream<'a>(
config: &'a dyn ClientConfig,
request: Result<http::request::Request<String>, Error>,
) -> Pin<Box<dyn Stream<Item = Result<bytes::Bytes, Error>> + 'a>> {
) -> Pin<Box<dyn Stream<Item = Result<bytes::Bytes, Error>> + 'a + Send>> {
let result = async move {
let response = config.request(request?).await?;
let status = response.status();
Expand All @@ -73,6 +73,6 @@ pub fn execute_request_stream<'a>(
Box::pin(stream::once(result).flat_map(|result| match result {
Ok(stream) => stream,
Err(err) => Box::pin(stream::once(async { Err(err) }))
as Pin<Box<dyn Stream<Item = Result<_, Error>>>>,
as Pin<Box<dyn Stream<Item = Result<_, Error>> + Send>>,
}))
}
4 changes: 2 additions & 2 deletions src/api_common/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub async fn execute_request_bytes(
pub fn execute_request_stream<'a>(
config: &'a dyn ClientConfig,
request: Result<http::request::Request<String>, Error>,
) -> Pin<Box<dyn Stream<Item = Result<bytes::Bytes, Error>> + 'a>> {
) -> Pin<Box<dyn Stream<Item = Result<bytes::Bytes, Error>> + 'a + Send>> {
let result = async move {
let response = config.request(request?).await?;
let status = response.status();
Expand All @@ -73,6 +73,6 @@ pub fn execute_request_stream<'a>(
Box::pin(stream::once(result).flat_map(|result| match result {
Ok(stream) => stream,
Err(err) => Box::pin(stream::once(async { Err(err) }))
as Pin<Box<dyn Stream<Item = Result<_, Error>>>>,
as Pin<Box<dyn Stream<Item = Result<_, Error>> + Send>>,
}))
}
2 changes: 1 addition & 1 deletion src/v4/apis/images.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ pub trait Images: HasConfig + Send + Sync {
&'a self,
name: &'a str,
params: Option<crate::v4::params::ImageGetLibpod<'a>>,
) -> Pin<Box<dyn futures::stream::Stream<Item = Result<bytes::Bytes, Error>> + 'a>> {
) -> Pin<Box<dyn futures::stream::Stream<Item = Result<bytes::Bytes, Error>> + Send + 'a>> {
request::execute_request_stream(
self.get_config(),
(|| {
Expand Down
2 changes: 1 addition & 1 deletion src/v4/apis/images_compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ pub trait ImagesCompat: HasConfig + Send + Sync {
fn image_get<'a>(
&'a self,
name: &'a str,
) -> Pin<Box<dyn futures::stream::Stream<Item = Result<bytes::Bytes, Error>> + 'a>> {
) -> Pin<Box<dyn futures::stream::Stream<Item = Result<bytes::Bytes, Error>> + Send + 'a>> {
request::execute_request_stream(
self.get_config(),
(|| {
Expand Down
2 changes: 1 addition & 1 deletion src/v5/apis/containers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ pub trait Containers: HasConfig + Send + Sync {
fn container_export_libpod<'a>(
&'a self,
name: &'a str,
) -> Pin<Box<dyn futures::stream::Stream<Item = Result<bytes::Bytes, Error>> + 'a>> {
) -> Pin<Box<dyn futures::stream::Stream<Item = Result<bytes::Bytes, Error>> + Send + 'a>> {
request::execute_request_stream(
self.get_config(),
(|| {
Expand Down
4 changes: 2 additions & 2 deletions src/v5/apis/images.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ pub trait Images: HasConfig + Send + Sync {
&'a self,
name: &'a str,
params: Option<crate::v5::params::ImageGetLibpod<'a>>,
) -> Pin<Box<dyn futures::stream::Stream<Item = Result<bytes::Bytes, Error>> + 'a>> {
) -> Pin<Box<dyn futures::stream::Stream<Item = Result<bytes::Bytes, Error>> + Send + 'a>> {
request::execute_request_stream(
self.get_config(),
(|| {
Expand Down Expand Up @@ -526,7 +526,7 @@ pub trait Images: HasConfig + Send + Sync {
fn image_export_libpod<'a>(
&'a self,
params: Option<crate::v5::params::ImageExportLibpod<'a>>,
) -> Pin<Box<dyn futures::stream::Stream<Item = Result<bytes::Bytes, Error>> + 'a>> {
) -> Pin<Box<dyn futures::stream::Stream<Item = Result<bytes::Bytes, Error>> + Send + 'a>> {
request::execute_request_stream(
self.get_config(),
(|| {
Expand Down
2 changes: 1 addition & 1 deletion src/v5/apis/images_compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ pub trait ImagesCompat: HasConfig + Send + Sync {
fn image_get<'a>(
&'a self,
name: &'a str,
) -> Pin<Box<dyn futures::stream::Stream<Item = Result<bytes::Bytes, Error>> + 'a>> {
) -> Pin<Box<dyn futures::stream::Stream<Item = Result<bytes::Bytes, Error>> + Send + 'a>> {
request::execute_request_stream(
self.get_config(),
(|| {
Expand Down
30 changes: 29 additions & 1 deletion tests/v5.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ async fn it_can_create_a_volume() {
}

#[tokio::test]
async fn it_can_run_in_a_thread() {
async fn it_can_run_call_async_methods_in_threads() {
common::test_init().await;

let config = Config::guess().await.unwrap();
Expand All @@ -65,6 +65,34 @@ async fn it_can_run_in_a_thread() {
assert!(handle.await.is_ok());
}

#[tokio::test]
async fn it_can_call_stream_functions_in_a_thread() {
common::test_init().await;

let config = Config::guess().await.unwrap();
let client = PodmanRestClient::new(config).await.unwrap();

common::pull_nginx_image(&client).await;
common::create_nginx_container(&client, "podman_rest_client_stream_thread_test").await;

let config = Config::guess().await.unwrap();
let client = PodmanRestClient::new(config).await.unwrap();

let handle = tokio::spawn(async move {
let stream = client
.containers()
.container_export_libpod("podman_rest_client_stream_thread_test");
let bytes: Vec<u8> = stream.map_ok(|b| b.to_vec()).try_concat().await.unwrap();
let c = std::io::Cursor::new(bytes);

assert!(tar::Archive::new(c).entries().unwrap().count() > 0);

common::delete_container(&client, "podman_rest_client_stream_thread_test").await;
});

assert!(handle.await.is_ok());
}

#[tokio::test]
async fn it_can_pull_images() {
common::test_init().await;
Expand Down

0 comments on commit a50ee12

Please sign in to comment.