Skip to content

Commit

Permalink
[Client]: add ca_cert_file parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
deedy5 committed Aug 14, 2024
1 parent f3290b6 commit 156c5c6
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 42 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ jobs:
for version in 3.8 3.9 3.10 3.11 3.12; do
uv venv --preview --python $version
source .venv/bin/activate
uv pip install certifi pytest
uv pip install primp --no-index --find-links dist --force-reinstall
uv pip install pytest
pytest
done
- name: pytest
Expand All @@ -86,8 +86,8 @@ jobs:
for version in 3.8 3.9 3.10 3.11 3.12; do
uv venv --preview --python $version
source .venv/bin/activate
uv pip install certifi pytest
uv pip install primp --no-index --find-links dist --force-reinstall
uv pip install pytest
pytest
done
Expand Down Expand Up @@ -145,8 +145,8 @@ jobs:
for version in 3.8 3.9 3.10 3.11 3.12; do
python$version -m venv .venv
source .venv/bin/activate
pip install certifi pytest
pip install primp --no-index --find-links dist --force-reinstall
pip install pytest
pytest
done
Expand Down Expand Up @@ -187,8 +187,8 @@ jobs:
for version in 3.8 3.9 3.10 3.11 3.12; do
uv venv --preview --python $version
source .venv/Scripts/activate
uv pip install certifi pytest
uv pip install primp --no-index --find-links dist --force-reinstall
uv pip install pytest
pytest
done
Expand Down Expand Up @@ -226,8 +226,8 @@ jobs:
for version in 3.8 3.9 3.10 3.11 3.12; do
uv venv --preview --python $version
source .venv/bin/activate
uv pip install certifi pytest
uv pip install primp --no-index --find-links dist --force-reinstall
uv pip install pytest
pytest
done
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class Client:
follow_redirects (bool, optional): Whether to follow redirects. Default is True.
max_redirects (int, optional): Maximum redirects to follow. Default 20. Applies if `follow_redirects` is True.
verify (bool, optional): Verify SSL certificates. Default is True.
ca_cert_file (str, optional): Path to CA certificate store. Default is None.
http1 (bool, optional): Use only HTTP/1.1. Default is None.
http2 (bool, optional): Use only HTTP/2. Default is None.
Expand Down Expand Up @@ -198,6 +199,12 @@ print(r.text)
resp = primp.Client(proxy="http://127.0.0.1:8080").get("https://tls.peet.ws/api/all")
print(resp.json())

# Using custom CA certificate store: file or certifi.where()
resp = primp.Client(ca_cert_file="/cert/cacert.pem").get("https://tls.peet.ws/api/all")
print(resp.json())
resp = primp.Client(ca_cert_file=certifi.where()).get("https://tls.peet.ws/api/all")
print(resp.json())

# You can also use convenience functions that use a default Client instance under the hood:
# primp.get() | primp.head() | primp.options() | primp.delete() | primp.post() | primp.patch() | primp.put()
# These functions can accept the `impersonate` parameter:
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ dependencies = []

[project.optional-dependencies]
dev = [
"certifi",
"pytest>=8.1.1",
]

Expand Down
51 changes: 40 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ impl Client {
/// * `follow_redirects` - A boolean to enable or disable following redirects. Default is `true`.
/// * `max_redirects` - The maximum number of redirects to follow. Default is 20. Applies if `follow_redirects` is `true`.
/// * `verify` - An optional boolean indicating whether to verify SSL certificates. Default is `true`.
/// * `ca_cert_file` - Path to CA certificate store. Default is None.
/// * `http1` - An optional boolean indicating whether to use only HTTP/1.1. Default is `false`.
/// * `http2` - An optional boolean indicating whether to use only HTTP/2. Default is `false`.
///
Expand All @@ -85,14 +86,16 @@ impl Client {
/// impersonate="chrome_123",
/// follow_redirects=True,
/// max_redirects=1,
/// verify=False,
/// verify=True,
/// ca_cert_file="/cert/cacert.pem",
/// http1=True,
/// http2=False,
/// )
/// ```
#[new]
#[pyo3(signature = (auth=None, auth_bearer=None, params=None, headers=None, cookies=None, cookie_store=None, referer=None,
proxy=None, timeout=None, impersonate=None, follow_redirects=None, max_redirects=None, verify=None, http1=None, http2=None))]
#[pyo3(signature = (auth=None, auth_bearer=None, params=None, headers=None, cookies=None,
cookie_store=None, referer=None, proxy=None, timeout=None, impersonate=None, follow_redirects=None,
max_redirects=None, verify=None, ca_cert_file=None, http1=None, http2=None))]
fn new(
auth: Option<(String, Option<String>)>,
auth_bearer: Option<String>,
Expand All @@ -107,6 +110,7 @@ impl Client {
follow_redirects: Option<bool>,
max_redirects: Option<usize>,
verify: Option<bool>,
ca_cert_file: Option<String>,
http1: Option<bool>,
http2: Option<bool>,
) -> Result<Self> {
Expand Down Expand Up @@ -172,6 +176,11 @@ impl Client {
client_builder = client_builder.danger_accept_invalid_certs(true);
}

// Ca_cert_file
if let Some(ca_cert_file) = ca_cert_file {
client_builder = client_builder.ca_cert_file(ca_cert_file);
}

// Http version: http1 || http2
match (http1, http2) {
(Some(true), Some(true)) => return Err(anyhow!("Both http1 and http2 cannot be true")),
Expand Down Expand Up @@ -601,7 +610,8 @@ impl Client {
/// Convenience functions that use a default Client instance under the hood
#[pyfunction]
#[pyo3(signature = (method, url, params=None, headers=None, cookies=None, content=None, data=None,
json=None, files=None, auth=None, auth_bearer=None, timeout=None, impersonate=None, verify=None))]
json=None, files=None, auth=None, auth_bearer=None, timeout=None, impersonate=None, verify=None,
ca_cert_file=None))]
fn request(
py: Python,
method: &str,
Expand All @@ -618,6 +628,7 @@ fn request(
timeout: Option<f64>,
impersonate: Option<&str>,
verify: Option<bool>,
ca_cert_file: Option<String>,
) -> Result<Response> {
let client = Client::new(
None,
Expand All @@ -633,6 +644,7 @@ fn request(
None,
None,
verify,
ca_cert_file,
None,
None,
)?;
Expand All @@ -655,7 +667,7 @@ fn request(

#[pyfunction]
#[pyo3(signature = (url, params=None, headers=None, cookies=None, auth=None, auth_bearer=None,
timeout=None, impersonate=None, verify=None))]
timeout=None, impersonate=None, verify=None, ca_cert_file=None))]
fn get(
py: Python,
url: &str,
Expand All @@ -667,6 +679,7 @@ fn get(
timeout: Option<f64>,
impersonate: Option<&str>,
verify: Option<bool>,
ca_cert_file: Option<String>,
) -> Result<Response> {
let client = Client::new(
None,
Expand All @@ -682,6 +695,7 @@ fn get(
None,
None,
verify,
ca_cert_file,
None,
None,
)?;
Expand All @@ -699,7 +713,7 @@ fn get(

#[pyfunction]
#[pyo3(signature = (url, params=None, headers=None, cookies=None, auth=None, auth_bearer=None,
timeout=None, impersonate=None, verify=None))]
timeout=None, impersonate=None, verify=None, ca_cert_file=None))]
fn head(
py: Python,
url: &str,
Expand All @@ -711,6 +725,7 @@ fn head(
timeout: Option<f64>,
impersonate: Option<&str>,
verify: Option<bool>,
ca_cert_file: Option<String>,
) -> Result<Response> {
let client = Client::new(
None,
Expand All @@ -726,6 +741,7 @@ fn head(
None,
None,
verify,
ca_cert_file,
None,
None,
)?;
Expand All @@ -743,7 +759,7 @@ fn head(

#[pyfunction]
#[pyo3(signature = (url, params=None, headers=None, cookies=None, auth=None, auth_bearer=None,
timeout=None, impersonate=None, verify=None))]
timeout=None, impersonate=None, verify=None, ca_cert_file=None))]
fn options(
py: Python,
url: &str,
Expand All @@ -755,6 +771,7 @@ fn options(
timeout: Option<f64>,
impersonate: Option<&str>,
verify: Option<bool>,
ca_cert_file: Option<String>,
) -> Result<Response> {
let client = Client::new(
None,
Expand All @@ -770,6 +787,7 @@ fn options(
None,
None,
verify,
ca_cert_file,
None,
None,
)?;
Expand All @@ -787,7 +805,7 @@ fn options(

#[pyfunction]
#[pyo3(signature = (url, params=None, headers=None, cookies=None, auth=None, auth_bearer=None,
timeout=None, impersonate=None, verify=None))]
timeout=None, impersonate=None, verify=None, ca_cert_file=None))]
fn delete(
py: Python,
url: &str,
Expand All @@ -799,6 +817,7 @@ fn delete(
timeout: Option<f64>,
impersonate: Option<&str>,
verify: Option<bool>,
ca_cert_file: Option<String>,
) -> Result<Response> {
let client = Client::new(
None,
Expand All @@ -814,6 +833,7 @@ fn delete(
None,
None,
verify,
ca_cert_file,
None,
None,
)?;
Expand All @@ -831,7 +851,8 @@ fn delete(

#[pyfunction]
#[pyo3(signature = (url, params=None, headers=None, cookies=None, content=None, data=None,
json=None, files=None, auth=None, auth_bearer=None, timeout=None, impersonate=None, verify=None))]
json=None, files=None, auth=None, auth_bearer=None, timeout=None, impersonate=None, verify=None,
ca_cert_file=None))]
fn post(
py: Python,
url: &str,
Expand All @@ -847,6 +868,7 @@ fn post(
timeout: Option<f64>,
impersonate: Option<&str>,
verify: Option<bool>,
ca_cert_file: Option<String>,
) -> Result<Response> {
let client = Client::new(
None,
Expand All @@ -862,6 +884,7 @@ fn post(
None,
None,
verify,
ca_cert_file,
None,
None,
)?;
Expand All @@ -883,7 +906,8 @@ fn post(

#[pyfunction]
#[pyo3(signature = (url, params=None, headers=None, cookies=None, content=None, data=None,
json=None, files=None, auth=None, auth_bearer=None, timeout=None, impersonate=None, verify=None))]
json=None, files=None, auth=None, auth_bearer=None, timeout=None, impersonate=None, verify=None,
ca_cert_file=None))]
fn put(
py: Python,
url: &str,
Expand All @@ -899,6 +923,7 @@ fn put(
timeout: Option<f64>,
impersonate: Option<&str>,
verify: Option<bool>,
ca_cert_file: Option<String>,
) -> Result<Response> {
let client = Client::new(
None,
Expand All @@ -914,6 +939,7 @@ fn put(
None,
None,
verify,
ca_cert_file,
None,
None,
)?;
Expand All @@ -935,7 +961,8 @@ fn put(

#[pyfunction]
#[pyo3(signature = (url, params=None, headers=None, cookies=None, content=None, data=None,
json=None, files=None, auth=None, auth_bearer=None, timeout=None, impersonate=None, verify=None))]
json=None, files=None, auth=None, auth_bearer=None, timeout=None, impersonate=None, verify=None,
ca_cert_file=None))]
fn patch(
py: Python,
url: &str,
Expand All @@ -951,6 +978,7 @@ fn patch(
timeout: Option<f64>,
impersonate: Option<&str>,
verify: Option<bool>,
ca_cert_file: Option<String>,
) -> Result<Response> {
let client = Client::new(
None,
Expand All @@ -966,6 +994,7 @@ fn patch(
None,
None,
verify,
ca_cert_file,
None,
None,
)?;
Expand Down
Loading

0 comments on commit 156c5c6

Please sign in to comment.