Skip to content

Commit

Permalink
cargo clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
deedy5 committed Jan 7, 2025
1 parent 3c1e736 commit 97dad44
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 88 deletions.
144 changes: 66 additions & 78 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(clippy::too_many_arguments)]
use std::str::FromStr;
use std::sync::{Arc, LazyLock, Mutex};
use std::time::Duration;
Expand Down Expand Up @@ -33,6 +34,8 @@ use traits::{CookiesTraits, HeadersTraits};
mod utils;
use utils::load_ca_certs;

type IndexMapSSR = IndexMap<String, String, RandomState>;

// Tokio global one-thread runtime
static RUNTIME: LazyLock<Runtime> = LazyLock::new(|| {
runtime::Builder::new_current_thread()
Expand All @@ -50,7 +53,7 @@ pub struct Client {
#[pyo3(get, set)]
auth_bearer: Option<String>,
#[pyo3(get, set)]
params: Option<IndexMap<String, String, RandomState>>,
params: Option<IndexMapSSR>,
#[pyo3(get, set)]
proxy: Option<String>,
#[pyo3(get, set)]
Expand Down Expand Up @@ -115,9 +118,9 @@ impl Client {
fn new(
auth: Option<(String, Option<String>)>,
auth_bearer: Option<String>,
params: Option<IndexMap<String, String, RandomState>>,
headers: Option<IndexMap<String, String, RandomState>>,
cookies: Option<IndexMap<String, String, RandomState>>,
params: Option<IndexMapSSR>,
headers: Option<IndexMapSSR>,
cookies: Option<IndexMapSSR>,
cookie_store: Option<bool>,
referer: Option<bool>,
proxy: Option<String>,
Expand All @@ -135,8 +138,8 @@ impl Client {

// Impersonate
if let Some(impersonation_type) = impersonate {
let impersonation = Impersonate::from_str(impersonation_type)
.map_err(|err| PyValueError::new_err(err))?;
let impersonation =
Impersonate::from_str(impersonation_type).map_err(PyValueError::new_err)?;
client_builder = client_builder.impersonate(impersonation);
}

Expand Down Expand Up @@ -214,18 +217,15 @@ impl Client {
}

#[getter]
pub fn get_headers(&self) -> Result<IndexMap<String, String, RandomState>> {
pub fn get_headers(&self) -> Result<IndexMapSSR> {
let mut client = self.client.lock().unwrap();
let mut headers = client.headers_mut().clone();
headers.remove(COOKIE);
Ok(headers.to_indexmap())
}

#[setter]
pub fn set_headers(
&self,
new_headers: Option<IndexMap<String, String, RandomState>>,
) -> Result<()> {
pub fn set_headers(&self, new_headers: Option<IndexMapSSR>) -> Result<()> {
let mut client = self.client.lock().unwrap();
let headers = client.headers_mut();
headers.clear();
Expand All @@ -238,11 +238,10 @@ impl Client {
}

#[getter]
pub fn get_cookies(&self) -> Result<IndexMap<String, String, RandomState>> {
pub fn get_cookies(&self) -> Result<IndexMapSSR> {
let mut client = self.client.lock().unwrap();
let headers = client.headers_mut();
let mut cookies: IndexMap<String, String, RandomState> =
IndexMap::with_hasher(RandomState::default());
let mut cookies: IndexMapSSR = IndexMap::with_hasher(RandomState::default());
if let Some(cookie_header) = headers.get(COOKIE) {
for part in cookie_header.to_str()?.split(';') {
if let Some((key, value)) = part.trim().split_once('=') {
Expand All @@ -254,10 +253,7 @@ impl Client {
}

#[setter]
pub fn set_cookies(
&self,
cookies: Option<IndexMap<String, String, RandomState>>,
) -> Result<()> {
pub fn set_cookies(&self, cookies: Option<IndexMapSSR>) -> Result<()> {
let mut client = self.client.lock().unwrap();
let headers = client.headers_mut();
if let Some(cookies) = cookies {
Expand Down Expand Up @@ -312,9 +308,9 @@ impl Client {
py: Python,
method: &str,
url: &str,
params: Option<IndexMap<String, String, RandomState>>,
headers: Option<IndexMap<String, String, RandomState>>,
cookies: Option<IndexMap<String, String, RandomState>>,
params: Option<IndexMapSSR>,
headers: Option<IndexMapSSR>,
cookies: Option<IndexMapSSR>,
content: Option<Vec<u8>>,
data: Option<&Bound<'_, PyAny>>,
json: Option<&Bound<'_, PyAny>>,
Expand Down Expand Up @@ -397,11 +393,11 @@ impl Client {
let resp = request_builder.send().await?;

// Response items
let cookies: IndexMap<String, String, RandomState> = resp
let cookies: IndexMapSSR = resp
.cookies()
.map(|cookie| (cookie.name().to_string(), cookie.value().to_string()))
.collect();
let headers: IndexMap<String, String, RandomState> = resp.headers().to_indexmap();
let headers: IndexMapSSR = resp.headers().to_indexmap();
let status_code = resp.status().as_u16();
let url = resp.url().to_string();
let buf = resp.bytes().await?;
Expand All @@ -412,16 +408,8 @@ impl Client {

// Execute an async future, releasing the Python GIL for concurrency.
// Use Tokio global runtime to block on the future.
let result: Result<
(
Bytes,
IndexMap<String, String, RandomState>,
IndexMap<String, String, RandomState>,
u16,
String,
),
Error,
> = py.allow_threads(|| RUNTIME.block_on(future));
let result: Result<(Bytes, IndexMapSSR, IndexMapSSR, u16, String), Error> =
py.allow_threads(|| RUNTIME.block_on(future));
let (f_buf, f_cookies, f_headers, f_status_code, f_url) = result?;

Ok(Response {
Expand All @@ -439,9 +427,9 @@ impl Client {
&self,
py: Python,
url: &str,
params: Option<IndexMap<String, String, RandomState>>,
headers: Option<IndexMap<String, String, RandomState>>,
cookies: Option<IndexMap<String, String, RandomState>>,
params: Option<IndexMapSSR>,
headers: Option<IndexMapSSR>,
cookies: Option<IndexMapSSR>,
auth: Option<(String, Option<String>)>,
auth_bearer: Option<String>,
timeout: Option<f64>,
Expand All @@ -468,9 +456,9 @@ impl Client {
&self,
py: Python,
url: &str,
params: Option<IndexMap<String, String, RandomState>>,
headers: Option<IndexMap<String, String, RandomState>>,
cookies: Option<IndexMap<String, String, RandomState>>,
params: Option<IndexMapSSR>,
headers: Option<IndexMapSSR>,
cookies: Option<IndexMapSSR>,
auth: Option<(String, Option<String>)>,
auth_bearer: Option<String>,
timeout: Option<f64>,
Expand All @@ -497,9 +485,9 @@ impl Client {
&self,
py: Python,
url: &str,
params: Option<IndexMap<String, String, RandomState>>,
headers: Option<IndexMap<String, String, RandomState>>,
cookies: Option<IndexMap<String, String, RandomState>>,
params: Option<IndexMapSSR>,
headers: Option<IndexMapSSR>,
cookies: Option<IndexMapSSR>,
auth: Option<(String, Option<String>)>,
auth_bearer: Option<String>,
timeout: Option<f64>,
Expand All @@ -526,9 +514,9 @@ impl Client {
&self,
py: Python,
url: &str,
params: Option<IndexMap<String, String, RandomState>>,
headers: Option<IndexMap<String, String, RandomState>>,
cookies: Option<IndexMap<String, String, RandomState>>,
params: Option<IndexMapSSR>,
headers: Option<IndexMapSSR>,
cookies: Option<IndexMapSSR>,
auth: Option<(String, Option<String>)>,
auth_bearer: Option<String>,
timeout: Option<f64>,
Expand Down Expand Up @@ -556,9 +544,9 @@ impl Client {
&self,
py: Python,
url: &str,
params: Option<IndexMap<String, String, RandomState>>,
headers: Option<IndexMap<String, String, RandomState>>,
cookies: Option<IndexMap<String, String, RandomState>>,
params: Option<IndexMapSSR>,
headers: Option<IndexMapSSR>,
cookies: Option<IndexMapSSR>,
content: Option<Vec<u8>>,
data: Option<&Bound<'_, PyAny>>,
json: Option<&Bound<'_, PyAny>>,
Expand Down Expand Up @@ -590,9 +578,9 @@ impl Client {
&self,
py: Python,
url: &str,
params: Option<IndexMap<String, String, RandomState>>,
headers: Option<IndexMap<String, String, RandomState>>,
cookies: Option<IndexMap<String, String, RandomState>>,
params: Option<IndexMapSSR>,
headers: Option<IndexMapSSR>,
cookies: Option<IndexMapSSR>,
content: Option<Vec<u8>>,
data: Option<&Bound<'_, PyAny>>,
json: Option<&Bound<'_, PyAny>>,
Expand Down Expand Up @@ -624,9 +612,9 @@ impl Client {
&self,
py: Python,
url: &str,
params: Option<IndexMap<String, String, RandomState>>,
headers: Option<IndexMap<String, String, RandomState>>,
cookies: Option<IndexMap<String, String, RandomState>>,
params: Option<IndexMapSSR>,
headers: Option<IndexMapSSR>,
cookies: Option<IndexMapSSR>,
content: Option<Vec<u8>>,
data: Option<&Bound<'_, PyAny>>,
json: Option<&Bound<'_, PyAny>>,
Expand Down Expand Up @@ -662,9 +650,9 @@ fn request(
py: Python,
method: &str,
url: &str,
params: Option<IndexMap<String, String, RandomState>>,
headers: Option<IndexMap<String, String, RandomState>>,
cookies: Option<IndexMap<String, String, RandomState>>,
params: Option<IndexMapSSR>,
headers: Option<IndexMapSSR>,
cookies: Option<IndexMapSSR>,
content: Option<Vec<u8>>,
data: Option<&Bound<'_, PyAny>>,
json: Option<&Bound<'_, PyAny>>,
Expand Down Expand Up @@ -717,9 +705,9 @@ fn request(
fn get(
py: Python,
url: &str,
params: Option<IndexMap<String, String, RandomState>>,
headers: Option<IndexMap<String, String, RandomState>>,
cookies: Option<IndexMap<String, String, RandomState>>,
params: Option<IndexMapSSR>,
headers: Option<IndexMapSSR>,
cookies: Option<IndexMapSSR>,
auth: Option<(String, Option<String>)>,
auth_bearer: Option<String>,
timeout: Option<f64>,
Expand Down Expand Up @@ -763,9 +751,9 @@ fn get(
fn head(
py: Python,
url: &str,
params: Option<IndexMap<String, String, RandomState>>,
headers: Option<IndexMap<String, String, RandomState>>,
cookies: Option<IndexMap<String, String, RandomState>>,
params: Option<IndexMapSSR>,
headers: Option<IndexMapSSR>,
cookies: Option<IndexMapSSR>,
auth: Option<(String, Option<String>)>,
auth_bearer: Option<String>,
timeout: Option<f64>,
Expand Down Expand Up @@ -809,9 +797,9 @@ fn head(
fn options(
py: Python,
url: &str,
params: Option<IndexMap<String, String, RandomState>>,
headers: Option<IndexMap<String, String, RandomState>>,
cookies: Option<IndexMap<String, String, RandomState>>,
params: Option<IndexMapSSR>,
headers: Option<IndexMapSSR>,
cookies: Option<IndexMapSSR>,
auth: Option<(String, Option<String>)>,
auth_bearer: Option<String>,
timeout: Option<f64>,
Expand Down Expand Up @@ -855,9 +843,9 @@ fn options(
fn delete(
py: Python,
url: &str,
params: Option<IndexMap<String, String, RandomState>>,
headers: Option<IndexMap<String, String, RandomState>>,
cookies: Option<IndexMap<String, String, RandomState>>,
params: Option<IndexMapSSR>,
headers: Option<IndexMapSSR>,
cookies: Option<IndexMapSSR>,
auth: Option<(String, Option<String>)>,
auth_bearer: Option<String>,
timeout: Option<f64>,
Expand Down Expand Up @@ -902,9 +890,9 @@ fn delete(
fn post(
py: Python,
url: &str,
params: Option<IndexMap<String, String, RandomState>>,
headers: Option<IndexMap<String, String, RandomState>>,
cookies: Option<IndexMap<String, String, RandomState>>,
params: Option<IndexMapSSR>,
headers: Option<IndexMapSSR>,
cookies: Option<IndexMapSSR>,
content: Option<Vec<u8>>,
data: Option<&Bound<'_, PyAny>>,
json: Option<&Bound<'_, PyAny>>,
Expand Down Expand Up @@ -957,9 +945,9 @@ fn post(
fn put(
py: Python,
url: &str,
params: Option<IndexMap<String, String, RandomState>>,
headers: Option<IndexMap<String, String, RandomState>>,
cookies: Option<IndexMap<String, String, RandomState>>,
params: Option<IndexMapSSR>,
headers: Option<IndexMapSSR>,
cookies: Option<IndexMapSSR>,
content: Option<Vec<u8>>,
data: Option<&Bound<'_, PyAny>>,
json: Option<&Bound<'_, PyAny>>,
Expand Down Expand Up @@ -1012,9 +1000,9 @@ fn put(
fn patch(
py: Python,
url: &str,
params: Option<IndexMap<String, String, RandomState>>,
headers: Option<IndexMap<String, String, RandomState>>,
cookies: Option<IndexMap<String, String, RandomState>>,
params: Option<IndexMapSSR>,
headers: Option<IndexMapSSR>,
cookies: Option<IndexMapSSR>,
content: Option<Vec<u8>>,
data: Option<&Bound<'_, PyAny>>,
json: Option<&Bound<'_, PyAny>>,
Expand Down
2 changes: 1 addition & 1 deletion src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl Response {
let (decoded_str, detected_encoding, _) = encoding.decode(raw_bytes);

// Update self.encoding based on the detected encoding
if &self.encoding != detected_encoding.name() {
if self.encoding != detected_encoding.name() {
self.encoding = detected_encoding.name().to_string();
}

Expand Down
12 changes: 7 additions & 5 deletions src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ use indexmap::IndexMap;

use rquest::header::{HeaderMap, HeaderName, HeaderValue};

type IndexMapSSR = IndexMap<String, String, RandomState>;

pub trait HeadersTraits {
fn to_indexmap(&self) -> IndexMap<String, String, RandomState>;
fn to_indexmap(&self) -> IndexMapSSR;
fn to_headermap(&self) -> HeaderMap;
fn insert_key_value(&mut self, key: String, value: String) -> Result<(), Error>;
}

impl HeadersTraits for IndexMap<String, String, RandomState> {
fn to_indexmap(&self) -> IndexMap<String, String, RandomState> {
impl HeadersTraits for IndexMapSSR {
fn to_indexmap(&self) -> IndexMapSSR {
self.clone()
}
fn to_headermap(&self) -> HeaderMap {
Expand All @@ -33,7 +35,7 @@ impl HeadersTraits for IndexMap<String, String, RandomState> {
}

impl HeadersTraits for HeaderMap {
fn to_indexmap(&self) -> IndexMap<String, String, RandomState> {
fn to_indexmap(&self) -> IndexMapSSR {
self.iter()
.map(|(k, v)| {
(
Expand Down Expand Up @@ -64,7 +66,7 @@ pub trait CookiesTraits {
fn to_string(&self) -> String;
}

impl CookiesTraits for IndexMap<String, String, RandomState> {
impl CookiesTraits for IndexMapSSR {
fn to_string(&self) -> String {
self.iter()
.map(|(k, v)| format!("{}={}", k, v))
Expand Down
6 changes: 2 additions & 4 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@ use rquest::boring::{
pub fn load_ca_certs() -> Option<&'static X509Store> {
static CERT_STORE: LazyLock<Result<X509Store, ErrorStack>> = LazyLock::new(|| {
let mut ca_store = X509StoreBuilder::new()?;
if let Some(ca_cert_path) = std::env::var("PRIMP_CA_BUNDLE")
.or(std::env::var("CA_CERT_FILE"))
.ok()
if let Ok(ca_cert_path) = std::env::var("PRIMP_CA_BUNDLE").or(std::env::var("CA_CERT_FILE"))
{
// Use CA certificate bundle from env var PRIMP_CA_BUNDLE
let cert_file = &std::fs::read(ca_cert_path)
.expect("Failed to read file from env var PRIMP_CA_BUNDLE");
let certs = X509::stack_from_pem(&cert_file)?;
let certs = X509::stack_from_pem(cert_file)?;
for cert in certs {
ca_store.add_cert(cert)?;
}
Expand Down

0 comments on commit 97dad44

Please sign in to comment.