Skip to content

Commit

Permalink
[utils] Recode fn json_dumps, json_loads
Browse files Browse the repository at this point in the history
  • Loading branch information
deedy5 committed Aug 15, 2024
1 parent 3c4da2d commit 8993dab
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 21 deletions.
10 changes: 2 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<String>().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
Expand Down
4 changes: 1 addition & 3 deletions src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,7 @@ impl Response {
}

fn json(&mut self, py: Python) -> Result<PyObject> {
let result = json_loads(py)
.call1((&self.content,))?
.extract::<PyObject>()?;
let result = json_loads(py, &self.content)?;
Ok(result)
}

Expand Down
22 changes: 12 additions & 10 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,44 @@ 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<Py<PyAny>> = GILOnceCell::new();
static JSON_LOADS: GILOnceCell<Py<PyAny>> = GILOnceCell::new();
static URLLIB_PARSE_URLENCODE: GILOnceCell<Py<PyAny>> = GILOnceCell::new();

/// python json.dumps
pub fn json_dumps(py: Python<'_>) -> &Bound<'_, PyAny> {
JSON_DUMPS
pub fn json_dumps(py: Python<'_>, pydict: &Py<PyDict>) -> Result<String> {
let json_dumps = JSON_DUMPS
.get_or_init(py, || {
py.import_bound("json")
.unwrap()
.getattr("dumps")
.unwrap()
.unbind()
})
.bind(py)
.bind(py);
let result = json_dumps.call1((pydict,))?.extract::<String>()?;
Ok(result)
}

/// python json.loads
pub fn json_loads(py: Python<'_>) -> &Bound<'_, PyAny> {
JSON_LOADS
pub fn json_loads(py: Python<'_>, content: &Py<PyBytes>) -> Result<PyObject> {
let json_loads = JSON_LOADS
.get_or_init(py, || {
py.import_bound("json")
.unwrap()
.getattr("loads")
.unwrap()
.unbind()
})
.bind(py)
.bind(py);
let result = json_loads.call1((content,))?.extract::<PyObject>()?;
Ok(result)
}

/// python urllib.parse.urlencode
pub fn url_encode(py: Python, pydict: Option<&Bound<'_, PyDict>>) -> Result<String> {
pub fn url_encode(py: Python, pydict: &Py<PyDict>) -> Result<String> {
let urlencode = URLLIB_PARSE_URLENCODE
.get_or_init(py, || {
py.import_bound("urllib.parse")
Expand All @@ -48,11 +52,9 @@ pub fn url_encode(py: Python, pydict: Option<&Bound<'_, PyDict>>) -> Result<Stri
.unbind()
})
.bind(py);

let result: String = urlencode
.call1((pydict, ("doseq", py.get_type_bound::<PyBool>().call1(())?)))?
.extract()?;

Ok(result)
}

Expand Down

0 comments on commit 8993dab

Please sign in to comment.