diff --git a/src/lib.rs b/src/lib.rs index 0b21fbb..3a362b3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -242,15 +242,9 @@ impl Client { let params = params.or(self.params.clone()); let cookies = cookies.or(self.cookies.clone()); // Converts 'data' (if any) into a URL-encoded string for sending the data as `application/x-www-form-urlencoded` content type. - let data_str = data - .map(|data_pydict| url_encode(py, Some(data_pydict)).ok()) - .unwrap_or_else(|| None); + let data_str = data.map(|data| url_encode(py, &data.clone().unbind())).transpose()?; // Converts 'json' (if any) into a JSON string for sending the data as `application/json` content type. - let json_str = json - .map(|json_pydict| { - json_dumps(py).call1((json_pydict.clone().unbind(),)).unwrap().extract::().ok() - }) - .unwrap_or(None); + let json_str = json.map(|pydict| json_dumps(py, &pydict.clone().unbind())).transpose()?; let future = async move { // Check if method is POST || PUT || PATCH diff --git a/src/response.rs b/src/response.rs index 8f4b9b1..5932978 100644 --- a/src/response.rs +++ b/src/response.rs @@ -78,9 +78,7 @@ impl Response { } fn json(&mut self, py: Python) -> Result { - let result = json_loads(py) - .call1((&self.content,))? - .extract::()?; + let result = json_loads(py, &self.content)?; Ok(result) } diff --git a/src/utils.rs b/src/utils.rs index 352dc39..5788662 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -5,15 +5,15 @@ use anyhow::Result; use indexmap::IndexMap; use pyo3::prelude::*; use pyo3::sync::GILOnceCell; -use pyo3::types::{PyBool, PyDict}; +use pyo3::types::{PyBool, PyBytes, PyDict}; static JSON_DUMPS: GILOnceCell> = GILOnceCell::new(); static JSON_LOADS: GILOnceCell> = GILOnceCell::new(); static URLLIB_PARSE_URLENCODE: GILOnceCell> = GILOnceCell::new(); /// python json.dumps -pub fn json_dumps(py: Python<'_>) -> &Bound<'_, PyAny> { - JSON_DUMPS +pub fn json_dumps(py: Python<'_>, pydict: &Py) -> Result { + let json_dumps = JSON_DUMPS .get_or_init(py, || { py.import_bound("json") .unwrap() @@ -21,12 +21,14 @@ pub fn json_dumps(py: Python<'_>) -> &Bound<'_, PyAny> { .unwrap() .unbind() }) - .bind(py) + .bind(py); + let result = json_dumps.call1((pydict,))?.extract::()?; + Ok(result) } /// python json.loads -pub fn json_loads(py: Python<'_>) -> &Bound<'_, PyAny> { - JSON_LOADS +pub fn json_loads(py: Python<'_>, content: &Py) -> Result { + let json_loads = JSON_LOADS .get_or_init(py, || { py.import_bound("json") .unwrap() @@ -34,11 +36,13 @@ pub fn json_loads(py: Python<'_>) -> &Bound<'_, PyAny> { .unwrap() .unbind() }) - .bind(py) + .bind(py); + let result = json_loads.call1((content,))?.extract::()?; + Ok(result) } /// python urllib.parse.urlencode -pub fn url_encode(py: Python, pydict: Option<&Bound<'_, PyDict>>) -> Result { +pub fn url_encode(py: Python, pydict: &Py) -> Result { let urlencode = URLLIB_PARSE_URLENCODE .get_or_init(py, || { py.import_bound("urllib.parse") @@ -48,11 +52,9 @@ pub fn url_encode(py: Python, pydict: Option<&Bound<'_, PyDict>>) -> Result().call1(())?)))? .extract()?; - Ok(result) }