| 466 | } |
| 467 | |
| 468 | fn read(&mut self, py: Python<'_>) -> PyResult<Vec<u8>> { |
| 469 | match self.inner_state { |
| 470 | InnerResponseState::Read => self |
| 471 | .content |
| 472 | .as_ref() |
| 473 | .cloned() |
| 474 | .ok_or_else(|| ImpitPyError(impit::errors::ImpitError::ResponseNotRead).into()), |
| 475 | InnerResponseState::Streaming | InnerResponseState::StreamingClosed => { |
| 476 | if self.is_stream_consumed { |
| 477 | Err(ImpitPyError(impit::errors::ImpitError::StreamConsumed).into()) |
| 478 | } else { |
| 479 | Err(ImpitPyError(impit::errors::ImpitError::StreamClosed).into()) |
| 480 | } |
| 481 | } |
| 482 | InnerResponseState::Unread => { |
| 483 | let response = self |
| 484 | .inner |
| 485 | .take() |
| 486 | .ok_or(ImpitPyError(impit::errors::ImpitError::StreamClosed))?; |
| 487 | |
| 488 | let runtime = pyo3_async_runtimes::tokio::get_runtime(); |
| 489 | let content = py.detach(|| { |
| 490 | runtime.block_on(async { |
| 491 | response |
| 492 | .bytes() |
| 493 | .await |
| 494 | .map(|b| b.to_vec()) |
| 495 | .map_err(|_| ImpitPyError(impit::errors::ImpitError::NetworkError)) |
| 496 | }) |
| 497 | })?; |
| 498 | |
| 499 | self.content = Some(content.clone()); |
| 500 | self.inner_state = InnerResponseState::Read; |
| 501 | self.is_stream_consumed = true; |
| 502 | self.is_closed = true; |
| 503 | |
| 504 | Ok(content) |
| 505 | } |
| 506 | } |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | impl ImpitPyResponse { |