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
9 changes: 2 additions & 7 deletions python/rnet/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -391,14 +391,9 @@ class Response:
Get the response into a `Streamer` of `bytes` from the body.
"""

async def text(self) -> str:
async def text(self, encoding: str | None = None) -> str:
r"""
Get the text content of the response.
"""

async def text_with_charset(self, encoding: str) -> str:
r"""
Get the full response text given a specific encoding.
Get the text content with the response encoding, defaulting to utf-8 when unspecified.
"""

async def json(self) -> Any:
Expand Down
10 changes: 2 additions & 8 deletions python/rnet/blocking.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,9 @@ def stream(self) -> Streamer:
"""
...

def text(self) -> str:
def text(self, encoding: str | None = None) -> str:
r"""
Get the text content of the response.
"""
...

def text_with_charset(self, encoding: str) -> str:
r"""
Get the full response text given a specific encoding.
Get the text content with the response encoding, defaulting to utf-8 when unspecified.
"""
...

Expand Down
21 changes: 8 additions & 13 deletions src/client/resp/ext.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use bytes::Bytes;
use pyo3::pybacked::PyBackedStr;

use crate::error::Error;

Expand All @@ -8,11 +9,8 @@ use crate::error::Error;
/// This trait wraps the underlying [`wreq::Response`] methods and converts their errors to our
/// custom `Error` type.
pub trait ResponseExt {
/// Returns an error if the body cannot be decoded as valid UTF-8.
async fn text(self) -> Result<String, Error>;

/// Returns an error if the encoding is unsupported or decoding fails.
async fn text_with_charset(self, encoding: impl AsRef<str>) -> Result<String, Error>;
async fn text(self, encoding: Option<PyBackedStr>) -> Result<String, Error>;

/// Returns an error if the body is not valid JSON or cannot be deserialized into `T`.
async fn json<T: serde::de::DeserializeOwned>(self) -> Result<T, Error>;
Expand All @@ -23,15 +21,12 @@ pub trait ResponseExt {

impl ResponseExt for wreq::Response {
#[inline]
async fn text(self) -> Result<String, Error> {
self.text().await.map_err(Error::Library)
}

#[inline]
async fn text_with_charset(self, encoding: impl AsRef<str>) -> Result<String, Error> {
self.text_with_charset(encoding)
.await
.map_err(Error::Library)
async fn text(self, encoding: Option<PyBackedStr>) -> Result<String, Error> {
match encoding {
Some(encoding) => self.text_with_charset(encoding).await,
None => self.text().await,
}
.map_err(Error::Library)
}

#[inline]
Expand Down
41 changes: 9 additions & 32 deletions src/client/resp/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,27 +219,17 @@ impl Response {
.map_err(Into::into)
}

/// Get the text content of the response.
pub async fn text(&self, #[pyo3(cancel_handle)] cancel: CancelHandle) -> PyResult<String> {
let fut = self
.clone()
.cache_response()
.and_then(ResponseExt::text)
.map_err(Into::into);
NoGIL::new(fut, cancel).await
}

/// Get the full response text given a specific encoding.
#[pyo3(signature = (encoding))]
pub async fn text_with_charset(
/// Get the text content with the response encoding, defaulting to utf-8 when unspecified.
#[pyo3(signature = (encoding = None))]
pub async fn text(
&self,
#[pyo3(cancel_handle)] cancel: CancelHandle,
encoding: PyBackedStr,
encoding: Option<PyBackedStr>,
) -> PyResult<String> {
let fut = self
.clone()
.cache_response()
.and_then(|resp| ResponseExt::text_with_charset(resp, encoding))
.and_then(|resp| ResponseExt::text(resp, encoding))
.map_err(Into::into);
NoGIL::new(fut, cancel).await
}
Expand Down Expand Up @@ -392,28 +382,15 @@ impl BlockingResponse {
self.0.stream()
}

/// Get the text content of the response.
pub fn text(&self, py: Python) -> PyResult<String> {
py.detach(|| {
let fut = self
.0
.clone()
.cache_response()
.and_then(ResponseExt::text)
.map_err(Into::into);
pyo3_async_runtimes::tokio::get_runtime().block_on(fut)
})
}

/// Get the full response text given a specific encoding.
#[pyo3(signature = (encoding))]
pub fn text_with_charset(&self, py: Python, encoding: PyBackedStr) -> PyResult<String> {
/// Get the text content with the response encoding, defaulting to utf-8 when unspecified.
#[pyo3(signature = (encoding = None))]
pub fn text(&self, py: Python, encoding: Option<PyBackedStr>) -> PyResult<String> {
py.detach(|| {
let fut = self
.0
.clone()
.cache_response()
.and_then(|resp| ResponseExt::text_with_charset(resp, encoding))
.and_then(|resp| ResponseExt::text(resp, encoding))
.map_err(Into::into);
pyo3_async_runtimes::tokio::get_runtime().block_on(fut)
})
Expand Down
Loading