| 379 | } |
| 380 | |
| 381 | fn aread(slf: Py<Self>, py: Python<'_>) -> PyResult<Bound<'_, PyAny>> { |
| 382 | pyo3_async_runtimes::tokio::future_into_py(py, async move { |
| 383 | let (state, response_option, content_option, is_stream_consumed) = |
| 384 | Python::attach(|py| { |
| 385 | let mut slf_ref = slf.borrow_mut(py); |
| 386 | ( |
| 387 | slf_ref.inner_state, |
| 388 | slf_ref.inner.take(), |
| 389 | slf_ref.content.clone(), |
| 390 | slf_ref.is_stream_consumed, |
| 391 | ) |
| 392 | }); |
| 393 | |
| 394 | match state { |
| 395 | InnerResponseState::Read => Ok(content_option.unwrap_or_default()), |
| 396 | InnerResponseState::Unread => { |
| 397 | if let Some(response) = response_option { |
| 398 | let content = |
| 399 | response.bytes().await.map(|b| b.to_vec()).map_err(|_| { |
| 400 | ImpitPyError(impit::errors::ImpitError::NetworkError) |
| 401 | })?; |
| 402 | |
| 403 | Python::attach(|py| { |
| 404 | let mut slf_ref = slf.borrow_mut(py); |
| 405 | slf_ref.content = Some(content.clone()); |
| 406 | slf_ref.inner_state = InnerResponseState::Read; |
| 407 | slf_ref.is_stream_consumed = true; |
| 408 | slf_ref.is_closed = true; |
| 409 | }); |
| 410 | |
| 411 | Ok(content) |
| 412 | } else { |
| 413 | Err(ImpitPyError(impit::errors::ImpitError::StreamClosed).into()) |
| 414 | } |
| 415 | } |
| 416 | InnerResponseState::Streaming | InnerResponseState::StreamingClosed => { |
| 417 | if is_stream_consumed { |
| 418 | Err(ImpitPyError(impit::errors::ImpitError::StreamConsumed).into()) |
| 419 | } else { |
| 420 | Err(ImpitPyError(impit::errors::ImpitError::StreamClosed).into()) |
| 421 | } |
| 422 | } |
| 423 | } |
| 424 | }) |
| 425 | } |
| 426 | |
| 427 | pub fn close(&mut self) -> PyResult<()> { |
| 428 | if self.is_closed { |