pub struct StreamedResponse { /* private fields */ }Expand description
A response whose body you read as it arrives, from
PendingRequest::stream.
Use it for large downloads, and for APIs that send their answer in parts, such as server-sent
events. The status and headers are available as soon as the response begins. Read the body
once, with chunks, events, or
into_response.
The request’s timeout limits each wait for more of the body, rather than the whole body, so a long stream that keeps sending data isn’t cut off.
use laravel::http::client;
use laravel::stream::StreamExt;
let mut events = client::get("https://example.com/updates")
.ensure_successful()
.stream()
.await?
.events();
while let Some(event) = events.next().await {
println!("{}", event?.data());
}Implementations§
Source§impl StreamedResponse
impl StreamedResponse
Sourcepub fn status(&self) -> StatusCode
pub fn status(&self) -> StatusCode
The response’s status, which compares with numbers, as in response.status() == 404.
Sourcepub fn successful(&self) -> bool
pub fn successful(&self) -> bool
Whether the status is 2xx.
Sourcepub fn redirect(&self) -> bool
pub fn redirect(&self) -> bool
Whether the status is 3xx.
The client follows redirects, so a response rarely has a 3xx status.
Sourcepub fn client_error(&self) -> bool
pub fn client_error(&self) -> bool
Whether the status is 4xx.
Sourcepub fn server_error(&self) -> bool
pub fn server_error(&self) -> bool
Whether the status is 5xx.
Sourcepub fn header(&self, name: &str) -> Option<&str>
pub fn header(&self, name: &str) -> Option<&str>
A header’s value, if the response has the header and its value is text.
Sourcepub fn chunks(
self,
) -> impl Stream<Item = Result<Bytes, Error>> + Send + Unpin + 'static
pub fn chunks( self, ) -> impl Stream<Item = Result<Bytes, Error>> + Send + Unpin + 'static
The body as a stream of byte chunks, in the order they arrive.
A faked body arrives one line per chunk. The stream ends when the body ends, or after an
error, such as an Error::Connection when no more of the body arrives in time.
Sourcepub fn events(
self,
) -> impl Stream<Item = Result<ServerSentEvent, Error>> + Send + Unpin + 'static
pub fn events( self, ) -> impl Stream<Item = Result<ServerSentEvent, Error>> + Send + Unpin + 'static
The body as a stream of server-sent events, each one yielded once it’s complete.
The stream skips comments, and it skips an event that the body ends before finishing.
use laravel::http::client;
use laravel::stream::StreamExt;
use serde_json::{Value, json};
let mut events = client::post("https://api.openai.com/v1/chat/completions")
.token(api_key)
.json(json!({ "model": "gpt", "stream": true, "messages": [] }))
.ensure_successful()
.stream()
.await?
.events();
while let Some(event) = events.next().await {
let event = event?;
if event.data() == "[DONE]" {
break;
}
let delta: Value = event.json()?;
print!("{}", delta["choices"][0]["delta"]["content"].as_str().unwrap_or_default());
}Trait Implementations§
Source§impl Debug for StreamedResponse
impl Debug for StreamedResponse
Source§impl From<Response> for StreamedResponse
impl From<Response> for StreamedResponse
Source§fn from(response: Response) -> StreamedResponse
fn from(response: Response) -> StreamedResponse
Auto Trait Implementations§
impl !Freeze for StreamedResponse
impl !RefUnwindSafe for StreamedResponse
impl !UnwindSafe for StreamedResponse
impl Forget for StreamedResponse
impl Move for StreamedResponse
impl Send for StreamedResponsewhere
NonZero<u16>: Send,
BoxRaw<[Pos]>: Send,
RawVecInner: Send,
Bytes: Send,
BoxRaw<HashMap<TypeId, Box<dyn AnyClone + Sync + Send>, BuildHasherDefault<IdHasher>>>: Send,
BoxRaw<dyn Body<Error = Box<dyn Error + Sync + Send>, Data = Bytes> + Sync + Send>: Send,
BoxRaw<Url>: Send,
NonZero<u64>: Send,
&'static (dyn Subscriber + Sync + Send): Send,
Arc<dyn Subscriber + Sync + Send>: Send,
&'static Metadata<'static>: Send,
impl Sync for StreamedResponsewhere
NonZero<u16>: Sync,
BoxRaw<[Pos]>: Sync,
RawVecInner: Sync,
Bytes: Sync,
BoxRaw<HashMap<TypeId, Box<dyn AnyClone + Sync + Send>, BuildHasherDefault<IdHasher>>>: Sync,
BoxRaw<dyn Body<Error = Box<dyn Error + Sync + Send>, Data = Bytes> + Sync + Send>: Sync,
BoxRaw<Url>: Sync,
NonZero<u64>: Sync,
Arc<dyn Subscriber + Sync + Send>: Sync,
impl Unpin for StreamedResponsewhere
NonZero<u16>: Unpin,
Box<[Pos]>: Unpin,
*const u8 is !null: Unpin,
*const u8: Unpin,
Align8<*mut ()>: Unpin,
&'static Vtable: Unpin,
Box<HashMap<TypeId, Box<dyn AnyClone + Sync + Send>, BuildHasherDefault<IdHasher>>>: Unpin,
Box<dyn Body<Error = Box<dyn Error + Sync + Send>, Data = Bytes> + Sync + Send>: Unpin,
Box<Url>: Unpin,
NonZero<u64>: Unpin,
&'static (dyn Subscriber + Sync + Send): Unpin,
Arc<dyn Subscriber + Sync + Send>: Unpin,
&'static Metadata<'static>: Unpin,
impl UnsafeUnpin for StreamedResponsewhere
NonZeroU16Inner: UnsafeUnpin,
*const [Pos] is !null: UnsafeUnpin,
PhantomData<[Pos]>: UnsafeUnpin,
*const u8 is !null: UnsafeUnpin,
PhantomData<Bucket<HeaderValue>>: UnsafeUnpin,
PhantomData<ExtraValue<HeaderValue>>: UnsafeUnpin,
PhantomData<u8>: UnsafeUnpin,
*const u8: UnsafeUnpin,
Align8<*mut ()>: UnsafeUnpin,
&'static Vtable: UnsafeUnpin,
*const HashMap<TypeId, Box<dyn AnyClone + Sync + Send>, BuildHasherDefault<IdHasher>> is !null: UnsafeUnpin,
PhantomData<HashMap<TypeId, Box<dyn AnyClone + Sync + Send>, BuildHasherDefault<IdHasher>>>: UnsafeUnpin,
*const dyn Body<Error = Box<dyn Error + Sync + Send>, Data = Bytes> + Sync + Send is !null: UnsafeUnpin,
PhantomData<dyn Body<Error = Box<dyn Error + Sync + Send>, Data = Bytes> + Sync + Send>: UnsafeUnpin,
*const Url is !null: UnsafeUnpin,
PhantomData<Url>: UnsafeUnpin,
NonZeroU64Inner: UnsafeUnpin,
&'static (dyn Subscriber + Sync + Send): UnsafeUnpin,
*const ArcInner<dyn Subscriber + Sync + Send> is !null: UnsafeUnpin,
PhantomData<ArcInner<dyn Subscriber + Sync + Send>>: UnsafeUnpin,
&'static Metadata<'static>: UnsafeUnpin,
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<K> ForeignKey<K> for K
impl<K> ForeignKey<K> for K
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
Source§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
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> ⓘ
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> ⓘ
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