Skip to content
Open
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
1 change: 1 addition & 0 deletions impit-node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ impl ImpitWrapper {
Either::B(_) => None,
}),
http3_prior_knowledge: force_http3.unwrap_or_default(),
..Default::default()
});

let method = method.unwrap_or_default();
Expand Down
80 changes: 80 additions & 0 deletions impit-python/python/impit/impit.pyi

Large diffs are not rendered by default.

79 changes: 62 additions & 17 deletions impit-python/src/async_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@ use pyo3::{ffi::c_str, prelude::*};
use crate::{
cookies::PythonCookieJar,
errors::ImpitPyError,
request::{parse_timeout, to_body, RequestBody, USE_CLIENT_DEFAULT_SENTINEL},
request::{
parse_timeout, redirect_behavior, to_body, RequestBody, USE_CLIENT_DEFAULT_SENTINEL,
},
response::ImpitPyResponse,
};

#[pyclass]
pub(crate) struct AsyncClient {
impit: Arc<Impit<PythonCookieJar>>,
default_encoding: Option<String>,
follow_redirects: bool,
max_redirects: u16,
}

#[pymethods]
Expand Down Expand Up @@ -87,12 +91,9 @@ impl AsyncClient {
_ => builder,
};

let builder = match follow_redirects {
Some(true) => builder.with_redirect(impit::impit::RedirectBehavior::FollowRedirect(
max_redirects.unwrap_or(20).into(),
)),
_ => builder.with_redirect(impit::impit::RedirectBehavior::ManualRedirect),
};
let follow_redirects = follow_redirects.unwrap_or(false);
let max_redirects = max_redirects.unwrap_or(20);
let builder = builder.with_redirect(redirect_behavior(follow_redirects, max_redirects));

let builder = match (cookie_jar, cookies) {
(Some(_), Some(_)) => {
Expand Down Expand Up @@ -127,10 +128,12 @@ impl AsyncClient {
Ok(Self {
impit: Arc::new(impit),
default_encoding,
follow_redirects,
max_redirects,
})
}

#[pyo3(signature = (url, content=None, data=None, headers=None, timeout=Some(Right(USE_CLIENT_DEFAULT_SENTINEL)), force_http3=false))]
#[pyo3(signature = (url, content=None, data=None, headers=None, timeout=Some(Right(USE_CLIENT_DEFAULT_SENTINEL)), force_http3=false, follow_redirects=None, max_redirects=None))]
pub fn get<'python>(
&self,
py: Python<'python>,
Expand All @@ -140,6 +143,8 @@ impl AsyncClient {
headers: Option<HashMap<String, String>>,
timeout: Option<Either<f64, &str>>,
force_http3: Option<bool>,
follow_redirects: Option<bool>,
max_redirects: Option<u16>,
) -> Result<pyo3::Bound<'python, PyAny>, PyErr> {
self.request(
py,
Expand All @@ -151,10 +156,12 @@ impl AsyncClient {
timeout,
force_http3,
Some(false),
follow_redirects,
max_redirects,
)
}

#[pyo3(signature = (url, content=None, data=None, headers=None, timeout=Some(Right(USE_CLIENT_DEFAULT_SENTINEL)), force_http3=false))]
#[pyo3(signature = (url, content=None, data=None, headers=None, timeout=Some(Right(USE_CLIENT_DEFAULT_SENTINEL)), force_http3=false, follow_redirects=None, max_redirects=None))]
pub fn head<'python>(
&self,
py: Python<'python>,
Expand All @@ -164,6 +171,8 @@ impl AsyncClient {
headers: Option<HashMap<String, String>>,
timeout: Option<Either<f64, &str>>,
force_http3: Option<bool>,
follow_redirects: Option<bool>,
max_redirects: Option<u16>,
) -> Result<pyo3::Bound<'python, PyAny>, PyErr> {
self.request(
py,
Expand All @@ -175,10 +184,12 @@ impl AsyncClient {
timeout,
force_http3,
Some(false),
follow_redirects,
max_redirects,
)
}

#[pyo3(signature = (url, content=None, data=None, headers=None, timeout=Some(Right(USE_CLIENT_DEFAULT_SENTINEL)), force_http3=false))]
#[pyo3(signature = (url, content=None, data=None, headers=None, timeout=Some(Right(USE_CLIENT_DEFAULT_SENTINEL)), force_http3=false, follow_redirects=None, max_redirects=None))]
pub fn post<'python>(
&self,
py: Python<'python>,
Expand All @@ -188,6 +199,8 @@ impl AsyncClient {
headers: Option<HashMap<String, String>>,
timeout: Option<Either<f64, &str>>,
force_http3: Option<bool>,
follow_redirects: Option<bool>,
max_redirects: Option<u16>,
) -> Result<pyo3::Bound<'python, PyAny>, PyErr> {
self.request(
py,
Expand All @@ -199,10 +212,12 @@ impl AsyncClient {
timeout,
force_http3,
Some(false),
follow_redirects,
max_redirects,
)
}

#[pyo3(signature = (url, content=None, data=None, headers=None, timeout=Some(Right(USE_CLIENT_DEFAULT_SENTINEL)), force_http3=false))]
#[pyo3(signature = (url, content=None, data=None, headers=None, timeout=Some(Right(USE_CLIENT_DEFAULT_SENTINEL)), force_http3=false, follow_redirects=None, max_redirects=None))]
pub fn patch<'python>(
&self,
py: Python<'python>,
Expand All @@ -212,6 +227,8 @@ impl AsyncClient {
headers: Option<HashMap<String, String>>,
timeout: Option<Either<f64, &str>>,
force_http3: Option<bool>,
follow_redirects: Option<bool>,
max_redirects: Option<u16>,
) -> Result<pyo3::Bound<'python, PyAny>, PyErr> {
self.request(
py,
Expand All @@ -223,10 +240,12 @@ impl AsyncClient {
timeout,
force_http3,
Some(false),
follow_redirects,
max_redirects,
)
}

#[pyo3(signature = (url, content=None, data=None, headers=None, timeout=Some(Right(USE_CLIENT_DEFAULT_SENTINEL)), force_http3=false))]
#[pyo3(signature = (url, content=None, data=None, headers=None, timeout=Some(Right(USE_CLIENT_DEFAULT_SENTINEL)), force_http3=false, follow_redirects=None, max_redirects=None))]
pub fn put<'python>(
&self,
py: Python<'python>,
Expand All @@ -236,6 +255,8 @@ impl AsyncClient {
headers: Option<HashMap<String, String>>,
timeout: Option<Either<f64, &str>>,
force_http3: Option<bool>,
follow_redirects: Option<bool>,
max_redirects: Option<u16>,
) -> Result<pyo3::Bound<'python, PyAny>, PyErr> {
self.request(
py,
Expand All @@ -247,10 +268,12 @@ impl AsyncClient {
timeout,
force_http3,
Some(false),
follow_redirects,
max_redirects,
)
}

#[pyo3(signature = (url, content=None, data=None, headers=None, timeout=Some(Right(USE_CLIENT_DEFAULT_SENTINEL)), force_http3=false))]
#[pyo3(signature = (url, content=None, data=None, headers=None, timeout=Some(Right(USE_CLIENT_DEFAULT_SENTINEL)), force_http3=false, follow_redirects=None, max_redirects=None))]
pub fn delete<'python>(
&self,
py: Python<'python>,
Expand All @@ -260,6 +283,8 @@ impl AsyncClient {
headers: Option<HashMap<String, String>>,
timeout: Option<Either<f64, &str>>,
force_http3: Option<bool>,
follow_redirects: Option<bool>,
max_redirects: Option<u16>,
) -> Result<pyo3::Bound<'python, PyAny>, PyErr> {
self.request(
py,
Expand All @@ -271,10 +296,12 @@ impl AsyncClient {
timeout,
force_http3,
Some(false),
follow_redirects,
max_redirects,
)
}

#[pyo3(signature = (url, content=None, data=None, headers=None, timeout=Some(Right(USE_CLIENT_DEFAULT_SENTINEL)), force_http3=false))]
#[pyo3(signature = (url, content=None, data=None, headers=None, timeout=Some(Right(USE_CLIENT_DEFAULT_SENTINEL)), force_http3=false, follow_redirects=None, max_redirects=None))]
pub fn options<'python>(
&self,
py: Python<'python>,
Expand All @@ -284,6 +311,8 @@ impl AsyncClient {
headers: Option<HashMap<String, String>>,
timeout: Option<Either<f64, &str>>,
force_http3: Option<bool>,
follow_redirects: Option<bool>,
max_redirects: Option<u16>,
) -> Result<pyo3::Bound<'python, PyAny>, PyErr> {
self.request(
py,
Expand All @@ -295,10 +324,12 @@ impl AsyncClient {
timeout,
force_http3,
Some(false),
follow_redirects,
max_redirects,
)
}

#[pyo3(signature = (url, content=None, data=None, headers=None, timeout=Some(Right(USE_CLIENT_DEFAULT_SENTINEL)), force_http3=false))]
#[pyo3(signature = (url, content=None, data=None, headers=None, timeout=Some(Right(USE_CLIENT_DEFAULT_SENTINEL)), force_http3=false, follow_redirects=None, max_redirects=None))]
pub fn trace<'python>(
&self,
py: Python<'python>,
Expand All @@ -308,6 +339,8 @@ impl AsyncClient {
headers: Option<HashMap<String, String>>,
timeout: Option<Either<f64, &str>>,
force_http3: Option<bool>,
follow_redirects: Option<bool>,
max_redirects: Option<u16>,
) -> Result<pyo3::Bound<'python, PyAny>, PyErr> {
self.request(
py,
Expand All @@ -319,10 +352,12 @@ impl AsyncClient {
timeout,
force_http3,
Some(false),
follow_redirects,
max_redirects,
)
}

#[pyo3(signature = (method, url, content=None, data=None, headers=None, timeout=Some(Right(USE_CLIENT_DEFAULT_SENTINEL)), force_http3=false))]
#[pyo3(signature = (method, url, content=None, data=None, headers=None, timeout=Some(Right(USE_CLIENT_DEFAULT_SENTINEL)), force_http3=false, follow_redirects=None, max_redirects=None))]
pub fn stream<'python>(
&self,
py: Python<'python>,
Expand All @@ -333,6 +368,8 @@ impl AsyncClient {
headers: Option<HashMap<String, String>>,
timeout: Option<Either<f64, &str>>,
force_http3: Option<bool>,
follow_redirects: Option<bool>,
max_redirects: Option<u16>,
) -> Result<pyo3::Bound<'python, PyAny>, PyErr> {
let response = self.request(
py,
Expand All @@ -344,6 +381,8 @@ impl AsyncClient {
timeout,
force_http3,
Some(true),
follow_redirects,
max_redirects,
)?;

let fun: Py<PyAny> = PyModule::from_code(
Expand All @@ -368,7 +407,7 @@ impl AsyncClient {
Ok(wrapped_response.into_bound(py))
}

#[pyo3(signature = (method, url, content=None, data=None, headers=None, timeout=Some(Right(USE_CLIENT_DEFAULT_SENTINEL)), force_http3=false, stream=false))]
#[pyo3(signature = (method, url, content=None, data=None, headers=None, timeout=Some(Right(USE_CLIENT_DEFAULT_SENTINEL)), force_http3=false, stream=false, follow_redirects=None, max_redirects=None))]
pub fn request<'python>(
&self,
py: Python<'python>,
Expand All @@ -380,6 +419,8 @@ impl AsyncClient {
timeout: Option<Either<f64, &str>>,
force_http3: Option<bool>,
stream: Option<bool>,
follow_redirects: Option<bool>,
max_redirects: Option<u16>,
) -> Result<pyo3::Bound<'python, PyAny>, PyErr> {
if let Some(content) = content {
data = Some(content);
Expand All @@ -393,6 +434,10 @@ impl AsyncClient {
headers: headers.unwrap_or_default().into_iter().collect(),
timeout,
http3_prior_knowledge: force_http3.unwrap_or(false),
redirect: Some(redirect_behavior(
follow_redirects.unwrap_or(self.follow_redirects),
max_redirects.unwrap_or(self.max_redirects),
)),
};

let method_str = method.to_string();
Expand Down
Loading
Loading