Skip to content

Commit

Permalink
support async read body for client
Browse files Browse the repository at this point in the history
  • Loading branch information
4t145 committed Jul 2, 2024
1 parent 4eb5ef9 commit e402c42
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions tardis/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ poem-grpc = { version = "0.4", optional = true }
reqwest = { version = "0.12", features = [
"json",
"multipart",
"stream"
], optional = true }

# Websocket Client
Expand Down
16 changes: 15 additions & 1 deletion tardis/src/web/web_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::time::Duration;

use reqwest::{Client, IntoUrl, Method, RequestBuilder, Response};
use serde::Deserialize;
use tokio::io::AsyncRead;
use tracing::{error, info, trace};

use crate::basic::error::TardisError;
Expand Down Expand Up @@ -33,6 +34,19 @@ impl TardisRequestBody for () {
}
}

/// Async Read for [`TardisWebClient`],
pub struct Read<R>(R);

impl<R> TardisRequestBody for Read<R>
where
R: AsyncRead + Send + Sync + Unpin + 'static,
{
fn apply_on(self, builder: RequestBuilder) -> RequestBuilder {
let stream = tokio_util::io::ReaderStream::new(self.0);
builder.body(reqwest::Body::wrap_stream(stream))
}
}

/// Plain text body for [`TardisWebClient`],
pub struct PlainText<T>(T);

Expand Down Expand Up @@ -253,7 +267,7 @@ impl TardisWebClient {
self.to_json::<T>(code, headers, response).await
}

async fn request<K, V>(
pub async fn request<K, V>(
&self,
method: Method,
url: impl IntoUrl,
Expand Down

0 comments on commit e402c42

Please sign in to comment.