Skip to content

Commit

Permalink
optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
deedy5 committed Jan 3, 2025
1 parent 57c4f12 commit c24e786
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 24 deletions.
24 changes: 7 additions & 17 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,24 +320,14 @@ impl Client {
timeout: Option<f64>,
) -> Result<Response> {
let client = Arc::clone(&self.client);
// Method
let method = match method {
"GET" => Method::GET,
"POST" => Method::POST,
"HEAD" => Method::HEAD,
"OPTIONS" => Method::OPTIONS,
"PUT" => Method::PUT,
"PATCH" => Method::PATCH,
"DELETE" => Method::DELETE,
_ => panic!("Unrecognized HTTP method"),
};
let method = Method::from_bytes(method.as_bytes())?;
let is_post_put_patch = matches!(method, Method::POST | Method::PUT | Method::PATCH);
let params = params.or_else(|| self.params.clone());
let data_value: Option<Value> = data.map(|data| depythonize(&data)).transpose()?;
let json_value: Option<Value> = json.map(|json| depythonize(&json)).transpose()?;
let auth = auth.or_else(|| self.auth.clone());
let auth_bearer = auth_bearer.or_else(|| self.auth_bearer.clone());
let is_post_put_patch = method == "POST" || method == "PUT" || method == "PATCH";
let timeout: Option<f64> = timeout.or_else(|| self.timeout);
let data_value: Option<Value> = data.map(depythonize).transpose()?;
let json_value: Option<Value> = json.map(depythonize).transpose()?;
let auth = auth.or(self.auth.clone());
let auth_bearer = auth_bearer.or(self.auth_bearer.clone());
let timeout: Option<f64> = timeout.or(self.timeout);

let future = async {
// Create request builder
Expand Down
12 changes: 5 additions & 7 deletions src/traits.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use std::str::FromStr;

use anyhow::{Error, Ok};
use anyhow::{Error, Ok, Result};
use foldhash::fast::RandomState;
use indexmap::IndexMap;

Expand All @@ -20,9 +18,9 @@ impl HeadersTraits for IndexMap<String, String, RandomState> {
self.iter()
.map(|(k, v)| {
(
HeaderName::from_str(k)
HeaderName::from_bytes(k.as_bytes())
.unwrap_or_else(|k| panic!("Invalid header name: {k:?}")),
HeaderValue::from_str(v)
HeaderValue::from_bytes(v.as_bytes())
.unwrap_or_else(|v| panic!("Invalid header value: {v:?}")),
)
})
Expand Down Expand Up @@ -53,9 +51,9 @@ impl HeadersTraits for HeaderMap {
}

fn insert_key_value(&mut self, key: String, value: String) -> Result<(), Error> {
let header_name = HeaderName::from_str(key.as_str())
let header_name = HeaderName::from_bytes(key.as_bytes())
.unwrap_or_else(|k| panic!("Invalid header name: {k:?}"));
let header_value = HeaderValue::from_str(value.as_str())
let header_value = HeaderValue::from_bytes(value.as_bytes())
.unwrap_or_else(|k| panic!("Invalid header value: {k:?}"));
self.insert(header_name, header_value);
Ok(())
Expand Down

0 comments on commit c24e786

Please sign in to comment.