Skip to content

Commit

Permalink
Make a lazy static reqwest client and use it (#325)
Browse files Browse the repository at this point in the history
* Make a lazy static reqwest client and use it

* Touch to trigger CI

now that its been re-enabled
  • Loading branch information
skeet70 authored Sep 10, 2024
1 parent 95405fa commit 330dafe
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
6 changes: 4 additions & 2 deletions src/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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,
}
Expand Down
38 changes: 31 additions & 7 deletions src/internal/rest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use serde::{de::DeserializeOwned, Deserialize, Serialize};
use std::{
borrow::BorrowMut,
fmt::{Display, Error, Formatter},
hash::Hash,
marker::PhantomData,
ops::Deref,
};
Expand Down Expand Up @@ -302,20 +303,37 @@ 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 {
fn default() -> Self {
*OUR_REQUEST
}
}
impl Hash for IronCoreRequest {
fn hash<H: std::hash::Hasher>(&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 {
Expand Down Expand Up @@ -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}]"#;
Expand Down Expand Up @@ -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))]);
Expand All @@ -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);
Expand Down

0 comments on commit 330dafe

Please sign in to comment.