Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl_buffer!(HeaderNameBuffer, HeaderName);
/// A helper function to fill buffer info
unsafe fn fill_buffer_info(
bytes: &[u8],
obj_ptr: *mut pyo3::ffi::PyObject,
obj_ptr: *mut ffi::PyObject,
view: *mut ffi::Py_buffer,
flags: c_int,
py: Python,
Expand Down
5 changes: 0 additions & 5 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,6 @@ pub fn websocket_disconnect_error() -> pyo3::PyErr {
PyRuntimeError::new_err("The WebSocket has been disconnected")
}

#[inline(always)]
pub fn stream_consumed_error() -> pyo3::PyErr {
BodyError::new_err("Stream is already consumed")
}

#[inline(always)]
pub fn wrap_invali_header_name_error(error: header::InvalidHeaderName) -> pyo3::PyErr {
PyRuntimeError::new_err(format!("Invalid header name: {:?}", error))
Expand Down
35 changes: 9 additions & 26 deletions src/typing/body.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,23 @@
use crate::error::stream_consumed_error;
use crate::stream::{AsyncStream, SyncStream};
use arc_swap::ArcSwapOption;
use bytes::Bytes;
use pyo3::prelude::*;
use pyo3::pybacked::{PyBackedBytes, PyBackedStr};
use pyo3::{FromPyObject, PyAny};
use std::sync::Arc;

/// The body to use for the request.
pub enum FromPyBody {
Text(Bytes),
Bytes(Bytes),
Iterator(Arc<ArcSwapOption<SyncStream>>),
Stream(Arc<ArcSwapOption<AsyncStream>>),
SyncStream(SyncStream),
AsyncStream(AsyncStream),
}

impl TryFrom<FromPyBody> for rquest::Body {
type Error = PyErr;

fn try_from(value: FromPyBody) -> Result<rquest::Body, Self::Error> {
impl From<FromPyBody> for rquest::Body {
fn from(value: FromPyBody) -> rquest::Body {
match value {
FromPyBody::Text(bytes) | FromPyBody::Bytes(bytes) => Ok(rquest::Body::from(bytes)),
FromPyBody::Iterator(iterator) => iterator
.swap(None)
.and_then(Arc::into_inner)
.map(Into::into)
.ok_or_else(stream_consumed_error),
FromPyBody::Stream(stream) => stream
.swap(None)
.and_then(Arc::into_inner)
.map(Into::into)
.ok_or_else(stream_consumed_error),
FromPyBody::Text(bytes) | FromPyBody::Bytes(bytes) => rquest::Body::from(bytes),
FromPyBody::SyncStream(stream) => rquest::Body::from(stream),
FromPyBody::AsyncStream(stream) => rquest::Body::from(stream),
}
}
}
Expand All @@ -48,15 +35,11 @@ impl FromPyObject<'_> for FromPyBody {
if ob.hasattr("asend")? {
pyo3_async_runtimes::tokio::into_stream_v2(ob.to_owned())
.map(AsyncStream::new)
.map(ArcSwapOption::from_pointee)
.map(Arc::new)
.map(Self::Stream)
.map(Self::AsyncStream)
} else {
ob.extract::<PyObject>()
.map(SyncStream::new)
.map(ArcSwapOption::from_pointee)
.map(Arc::new)
.map(Self::Iterator)
.map(Self::SyncStream)
}
}
}
41 changes: 8 additions & 33 deletions src/typing/multipart/part.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::{
error::{MIMEParseError, stream_consumed_error, wrap_io_error},
error::{MIMEParseError, wrap_io_error},
stream::{AsyncStream, SyncStream},
};
use arc_swap::ArcSwapOption;
use bytes::Bytes;
use pyo3::{
prelude::*,
Expand All @@ -12,7 +11,7 @@ use pyo3_stub_gen::{
PyStubType, TypeInfo,
derive::{gen_stub_pyclass, gen_stub_pymethods},
};
use std::{fmt::Debug, path::PathBuf, sync::Arc};
use std::path::PathBuf;

/// A part of a multipart form.
#[gen_stub_pyclass]
Expand All @@ -27,20 +26,8 @@ pub enum PartData {
Text(Bytes),
Bytes(Bytes),
File(PathBuf),
Iterator(Arc<ArcSwapOption<SyncStream>>),
Stream(Arc<ArcSwapOption<AsyncStream>>),
}

impl Debug for PartData {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Text(inner) => write!(f, "PartData::Text({:?})", inner),
Self::Bytes(inner) => write!(f, "PartData::Bytes({:?})", inner),
Self::File(inner) => write!(f, "PartData::File({:?})", inner),
Self::Iterator(_) => write!(f, "PartData::Iterator(...)"),
Self::Stream(_) => write!(f, "PartData::Stream(...)"),
}
}
SyncStream(SyncStream),
AsyncStream(AsyncStream),
}

impl PyStubType for PartData {
Expand Down Expand Up @@ -71,16 +58,8 @@ impl Part {
PartData::File(path) => pyo3_async_runtimes::tokio::get_runtime()
.block_on(rquest::multipart::Part::file(path))
.map_err(wrap_io_error)?,
PartData::Iterator(iterator) => iterator
.swap(None)
.and_then(Arc::into_inner)
.map(Into::into)
.ok_or_else(stream_consumed_error)?,
PartData::Stream(stream) => stream
.swap(None)
.and_then(Arc::into_inner)
.map(Into::into)
.ok_or_else(stream_consumed_error)?,
PartData::SyncStream(stream) => rquest::multipart::Part::stream(stream),
PartData::AsyncStream(stream) => rquest::multipart::Part::stream(stream),
};

// Set the filename and MIME type if provided
Expand Down Expand Up @@ -120,15 +99,11 @@ impl FromPyObject<'_> for PartData {
if ob.hasattr("asend")? {
pyo3_async_runtimes::tokio::into_stream_v2(ob.to_owned())
.map(AsyncStream::new)
.map(ArcSwapOption::from_pointee)
.map(Arc::new)
.map(Self::Stream)
.map(Self::AsyncStream)
} else {
ob.extract::<PyObject>()
.map(SyncStream::new)
.map(ArcSwapOption::from_pointee)
.map(Arc::new)
.map(Self::Iterator)
.map(Self::SyncStream)
}
}
}