Skip to main content

reqwest_streams/
protobuf_body.rs

1//! Streaming a length-prefixed protobuf request body.
2
3use crate::stream_body::{ReqwestStreamBody, StreamBodyRequest};
4use futures::Stream;
5use http_streams_core::ProtobufStreamFormat;
6
7/// Extension trait for [`reqwest::RequestBuilder`] that streams a protobuf request body.
8///
9/// See [`ReqwestStreamBody`] for the HTTP caveats that apply to every streamed request body.
10pub trait ProtobufStreamRequest {
11    /// Streams `stream` as length-prefixed protobuf messages, setting
12    /// `Content-Type: application/x-protobuf-stream`.
13    fn protobuf_stream_body<S, T>(self, stream: S) -> reqwest::RequestBuilder
14    where
15        T: prost::Message + Send + 'static,
16        S: Stream<Item = T> + Send + 'static;
17
18    /// Streams a fallible `stream` as length-prefixed protobuf messages.
19    fn try_protobuf_stream_body<S, T, E>(self, stream: S) -> reqwest::RequestBuilder
20    where
21        T: prost::Message + Send + 'static,
22        S: Stream<Item = Result<T, E>> + Send + 'static,
23        E: Into<Box<dyn std::error::Error + Send + Sync>> + Send + 'static;
24}
25
26impl ProtobufStreamRequest for reqwest::RequestBuilder {
27    fn protobuf_stream_body<S, T>(self, stream: S) -> reqwest::RequestBuilder
28    where
29        T: prost::Message + Send + 'static,
30        S: Stream<Item = T> + Send + 'static,
31    {
32        self.stream_body(ReqwestStreamBody::new(ProtobufStreamFormat::new(), stream))
33    }
34
35    fn try_protobuf_stream_body<S, T, E>(self, stream: S) -> reqwest::RequestBuilder
36    where
37        T: prost::Message + Send + 'static,
38        S: Stream<Item = Result<T, E>> + Send + 'static,
39        E: Into<Box<dyn std::error::Error + Send + Sync>> + Send + 'static,
40    {
41        self.stream_body(ReqwestStreamBody::try_new(
42            ProtobufStreamFormat::new(),
43            stream,
44        ))
45    }
46}