From c24e786fbdc700c54bfd342d14bd44ef969833c2 Mon Sep 17 00:00:00 2001 From: deedy5 <65482418+deedy5@users.noreply.github.com> Date: Fri, 3 Jan 2025 13:48:43 +0300 Subject: [PATCH] optimize code --- src/lib.rs | 24 +++++++----------------- src/traits.rs | 12 +++++------- 2 files changed, 12 insertions(+), 24 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index fe125cb..7f6dab9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -320,24 +320,14 @@ impl Client { timeout: Option, ) -> Result { 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 = data.map(|data| depythonize(&data)).transpose()?; - let json_value: Option = 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 = timeout.or_else(|| self.timeout); + let data_value: Option = data.map(depythonize).transpose()?; + let json_value: Option = json.map(depythonize).transpose()?; + let auth = auth.or(self.auth.clone()); + let auth_bearer = auth_bearer.or(self.auth_bearer.clone()); + let timeout: Option = timeout.or(self.timeout); let future = async { // Create request builder diff --git a/src/traits.rs b/src/traits.rs index 006232d..b8ce4bc 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -1,6 +1,4 @@ -use std::str::FromStr; - -use anyhow::{Error, Ok}; +use anyhow::{Error, Ok, Result}; use foldhash::fast::RandomState; use indexmap::IndexMap; @@ -20,9 +18,9 @@ impl HeadersTraits for IndexMap { 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:?}")), ) }) @@ -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(())