MCPcopy Create free account
hub / github.com/apify/impit / iter_bytes

Method iter_bytes

impit-python/src/response.rs:336–379  ·  view source on GitHub ↗
(slf: Py<Self>, py: Python)

Source from the content-addressed store, hash-verified

334 }
335
336 fn iter_bytes(slf: Py<Self>, py: Python) -> PyResult<PyResponseBytesIterator> {
337 let runtime = pyo3_async_runtimes::tokio::get_runtime().handle().clone();
338 let mut slf_ref = slf.borrow_mut(py);
339
340 match slf_ref.inner_state {
341 InnerResponseState::Read => {
342 let content = slf_ref.content.clone();
343 drop(slf_ref);
344
345 Ok(PyResponseBytesIterator {
346 ready_content: content,
347 stream: None,
348 runtime,
349 content_returned: false,
350 parent_response: Some(slf),
351 })
352 }
353 InnerResponseState::Unread => {
354 slf_ref.inner_state = InnerResponseState::Streaming;
355
356 let response = slf_ref
357 .inner
358 .take()
359 .ok_or(ImpitPyError(impit::errors::ImpitError::StreamClosed))?;
360
361 drop(slf_ref);
362
363 let stream: Pin<Box<dyn Stream<Item = reqwest::Result<Bytes>> + Send + Sync>> =
364 Box::pin(response.bytes_stream());
365
366 Ok(PyResponseBytesIterator {
367 ready_content: None,
368 stream: Some(stream),
369 runtime,
370 content_returned: false,
371 parent_response: Some(slf),
372 })
373 }
374 InnerResponseState::Streaming | InnerResponseState::StreamingClosed => {
375 drop(slf_ref);
376 Err(ImpitPyError(impit::errors::ImpitError::StreamConsumed).into())
377 }
378 }
379 }
380
381 fn aread(slf: Py<Self>, py: Python<'_>) -> PyResult<Bound<'_, PyAny>> {
382 pyo3_async_runtimes::tokio::future_into_py(py, async move {

Calls 2

ImpitPyErrorClass · 0.85
cloneMethod · 0.80