pub struct StreamingResponse<S> {
pub status: StatusCode,
pub headers: HeaderMap,
pub stream: S,
}Expand description
Streaming HTTP Response
Fields§
§status: StatusCode§headers: HeaderMap§stream: SImplementations§
Source§impl<S> StreamingResponse<S>
impl<S> StreamingResponse<S>
Sourcepub fn new(stream: S) -> Self
pub fn new(stream: S) -> Self
Create a new streaming response with OK status
§Examples
use reinhardt_http::StreamingResponse;
use hyper::StatusCode;
use futures::stream;
use bytes::Bytes;
let data = vec![Ok(Bytes::from("chunk1")), Ok(Bytes::from("chunk2"))];
let stream = stream::iter(data);
let response = StreamingResponse::new(stream);
assert_eq!(response.status, StatusCode::OK);Sourcepub fn with_status(stream: S, status: StatusCode) -> Self
pub fn with_status(stream: S, status: StatusCode) -> Self
Create a streaming response with a specific status code
§Examples
use reinhardt_http::StreamingResponse;
use hyper::StatusCode;
use futures::stream;
use bytes::Bytes;
let data = vec![Ok(Bytes::from("data"))];
let stream = stream::iter(data);
let response = StreamingResponse::with_status(stream, StatusCode::PARTIAL_CONTENT);
assert_eq!(response.status, StatusCode::PARTIAL_CONTENT);Sourcepub fn status(self, status: StatusCode) -> Self
pub fn status(self, status: StatusCode) -> Self
Set the status code
§Examples
use reinhardt_http::StreamingResponse;
use hyper::StatusCode;
use futures::stream;
use bytes::Bytes;
let data = vec![Ok(Bytes::from("data"))];
let stream = stream::iter(data);
let response = StreamingResponse::new(stream).status(StatusCode::ACCEPTED);
assert_eq!(response.status, StatusCode::ACCEPTED);Sourcepub fn header(self, key: HeaderName, value: HeaderValue) -> Self
pub fn header(self, key: HeaderName, value: HeaderValue) -> Self
Add a header to the streaming response
§Examples
use reinhardt_http::StreamingResponse;
use hyper::header::{HeaderName, HeaderValue, CACHE_CONTROL};
use futures::stream;
use bytes::Bytes;
let data = vec![Ok(Bytes::from("data"))];
let stream = stream::iter(data);
let response = StreamingResponse::new(stream)
.header(CACHE_CONTROL, HeaderValue::from_static("no-cache"));
assert_eq!(
response.headers.get(CACHE_CONTROL).unwrap().to_str().unwrap(),
"no-cache"
);Sourcepub fn media_type(self, media_type: &str) -> Self
pub fn media_type(self, media_type: &str) -> Self
Set the Content-Type header (media type)
§Examples
use reinhardt_http::StreamingResponse;
use hyper::header::CONTENT_TYPE;
use futures::stream;
use bytes::Bytes;
let data = vec![Ok(Bytes::from("data"))];
let stream = stream::iter(data);
let response = StreamingResponse::new(stream).media_type("video/mp4");
assert_eq!(
response.headers.get(CONTENT_TYPE).unwrap().to_str().unwrap(),
"video/mp4"
);Source§impl<S> StreamingResponse<S>
impl<S> StreamingResponse<S>
Sourcepub fn into_stream(self) -> S
pub fn into_stream(self) -> S
Consume the response and return the underlying stream
§Examples
use reinhardt_http::StreamingResponse;
use futures::stream::{self, StreamExt};
use bytes::Bytes;
let data = vec![Ok(Bytes::from("chunk1")), Ok(Bytes::from("chunk2"))];
let stream = stream::iter(data);
let response = StreamingResponse::new(stream);
let mut extracted_stream = response.into_stream();
let first_chunk = extracted_stream.next().await.unwrap().unwrap();
assert_eq!(first_chunk, Bytes::from("chunk1"));Auto Trait Implementations§
impl<S> Freeze for StreamingResponse<S>where
S: Freeze,
impl<S> RefUnwindSafe for StreamingResponse<S>where
S: RefUnwindSafe,
impl<S> Send for StreamingResponse<S>where
S: Send,
impl<S> Sync for StreamingResponse<S>where
S: Sync,
impl<S> Unpin for StreamingResponse<S>where
S: Unpin,
impl<S> UnsafeUnpin for StreamingResponse<S>where
S: UnsafeUnpin,
impl<S> UnwindSafe for StreamingResponse<S>where
S: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more