From 330dafec3cb9477fe9459a0713857fa8c1fac43c Mon Sep 17 00:00:00 2001 From: Murph Murphy Date: Tue, 10 Sep 2024 10:34:10 -0600 Subject: [PATCH] Make a lazy static reqwest client and use it (#325) * Make a lazy static reqwest client and use it * Touch to trigger CI now that its been re-enabled --- README.md | 2 +- src/internal.rs | 6 ++++-- src/internal/rest.rs | 38 +++++++++++++++++++++++++++++++------- 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 0e20c11..16c0bad 100644 --- a/README.md +++ b/README.md @@ -48,5 +48,5 @@ To run the unit test suite, use: IronOxide is licensed under the [GNU Affero General Public License](LICENSE). We also offer commercial licenses - [email](mailto:info@ironcorelabs.com) for more information. -Copyright (c) 2023 IronCore Labs, Inc. +Copyright (c) 2024 IronCore Labs, Inc. All rights reserved. diff --git a/src/internal.rs b/src/internal.rs index ed36536..eee1922 100644 --- a/src/internal.rs +++ b/src/internal.rs @@ -45,7 +45,9 @@ lazy_static! { .to_string(), _ => "https://api.ironcorelabs.com/api/1/".to_string(), }; - pub static ref OUR_REQUEST: IronCoreRequest = IronCoreRequest::new(URL_STRING.as_str()); + static ref SHARED_CLIENT: reqwest::Client = reqwest::Client::new(); + pub static ref OUR_REQUEST: IronCoreRequest = + IronCoreRequest::new(URL_STRING.as_str(), &SHARED_CLIENT); } #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] @@ -399,7 +401,7 @@ impl DeviceContext { account_id, segment_id, signing_private_key, - request: IronCoreRequest::new(OUR_REQUEST.base_url()), + request: IronCoreRequest::default(), }, device_private_key, } diff --git a/src/internal/rest.rs b/src/internal/rest.rs index 3dbe924..21f7be0 100644 --- a/src/internal/rest.rs +++ b/src/internal/rest.rs @@ -18,6 +18,7 @@ use serde::{de::DeserializeOwned, Deserialize, Serialize}; use std::{ borrow::BorrowMut, fmt::{Display, Error, Formatter}, + hash::Hash, marker::PhantomData, ops::Deref, }; @@ -302,9 +303,15 @@ impl<'a> HeaderIronCoreRequestSig<'a> { } ///A struct which holds the basic info that will be needed for making requests to an ironcore service. Currently just the base_url. -#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)] +#[derive(Clone, Copy, Debug, Serialize, Deserialize)] pub struct IronCoreRequest { base_url: &'static str, + #[serde(skip_serializing, skip_deserializing, default = "default_client")] + client: &'static reqwest::Client, +} + +fn default_client() -> &'static reqwest::Client { + OUR_REQUEST.client } impl Default for IronCoreRequest { @@ -312,10 +319,21 @@ impl Default for IronCoreRequest { *OUR_REQUEST } } +impl Hash for IronCoreRequest { + fn hash(&self, state: &mut H) { + self.base_url.hash(state); + } +} +impl PartialEq for IronCoreRequest { + fn eq(&self, other: &Self) -> bool { + self.base_url == other.base_url + } +} +impl Eq for IronCoreRequest {} impl IronCoreRequest { - pub const fn new(base_url: &'static str) -> IronCoreRequest { - IronCoreRequest { base_url } + pub const fn new(base_url: &'static str, client: &'static reqwest::Client) -> IronCoreRequest { + IronCoreRequest { base_url, client } } pub fn base_url(&self) -> &str { @@ -1031,6 +1049,14 @@ mod tests { use recrypt::api::{Ed25519Signature, PublicSigningKey}; + lazy_static! { + static ref SHARED_CLIENT: reqwest::Client = reqwest::Client::new(); + static ref TEST_REQUEST: IronCoreRequest = IronCoreRequest { + base_url: "https://example.com", + client: &SHARED_CLIENT + }; + } + #[test] fn deserialize_errors() { let raw_string = r#"[{"message":"foo","code":2},{"message":"bar","code":3}]"#; @@ -1350,10 +1376,9 @@ mod tests { #[test] fn query_params_encoded_correctly() { - let icl_req = IronCoreRequest::new("https://example.com"); let mut req = Request::new( Method::GET, - url::Url::parse(&format!("{}/{}", icl_req.base_url(), "users")).unwrap(), + url::Url::parse(&format!("{}/{}", TEST_REQUEST.base_url(), "users")).unwrap(), ); let q = "!\"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"; IronCoreRequest::req_add_query(&mut req, &[("id".to_string(), url_encode(q))]); @@ -1364,10 +1389,9 @@ mod tests { #[test] fn empty_query_params_encoded_correctly() { - let icl_req = IronCoreRequest::new("https://example.com"); let mut req = Request::new( Method::GET, - url::Url::parse(&format!("{}/{}", icl_req.base_url(), "policies")).unwrap(), + url::Url::parse(&format!("{}/{}", TEST_REQUEST.base_url(), "policies")).unwrap(), ); IronCoreRequest::req_add_query(&mut req, &[]); assert_eq!(req.url().query(), None);