| 291 | } |
| 292 | |
| 293 | fn aiter_bytes(slf: Py<Self>, py: Python) -> PyResult<PyResponseAsyncBytesIterator> { |
| 294 | let mut slf_ref = slf.borrow_mut(py); |
| 295 | |
| 296 | match slf_ref.inner_state { |
| 297 | InnerResponseState::Read => { |
| 298 | let content = slf_ref.content.clone(); |
| 299 | drop(slf_ref); |
| 300 | |
| 301 | Ok(PyResponseAsyncBytesIterator { |
| 302 | ready_content: content, |
| 303 | stream: None, |
| 304 | content_returned: false, |
| 305 | parent_response: Some(slf), |
| 306 | }) |
| 307 | } |
| 308 | InnerResponseState::Unread => { |
| 309 | slf_ref.inner_state = InnerResponseState::Streaming; |
| 310 | |
| 311 | let response = slf_ref |
| 312 | .inner |
| 313 | .take() |
| 314 | .ok_or(ImpitPyError(impit::errors::ImpitError::StreamClosed))?; |
| 315 | |
| 316 | drop(slf_ref); |
| 317 | |
| 318 | let stream: Pin<Box<dyn Stream<Item = reqwest::Result<Bytes>> + Send>> = |
| 319 | Box::pin(response.bytes_stream()); |
| 320 | let shared_stream = Arc::new(AsyncMutex::new(Some(stream))); |
| 321 | |
| 322 | Ok(PyResponseAsyncBytesIterator { |
| 323 | ready_content: None, |
| 324 | stream: Some(shared_stream), |
| 325 | content_returned: false, |
| 326 | parent_response: Some(slf), |
| 327 | }) |
| 328 | } |
| 329 | InnerResponseState::Streaming | InnerResponseState::StreamingClosed => { |
| 330 | drop(slf_ref); |
| 331 | Err(ImpitPyError(impit::errors::ImpitError::StreamConsumed).into()) |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | fn iter_bytes(slf: Py<Self>, py: Python) -> PyResult<PyResponseBytesIterator> { |
| 337 | let runtime = pyo3_async_runtimes::tokio::get_runtime().handle().clone(); |