Skip to content

Commit

Permalink
fix: lazy initialize client
Browse files Browse the repository at this point in the history
  • Loading branch information
baszalmstra committed Jun 14, 2024
1 parent 735e987 commit 4f10a1a
Showing 1 changed file with 12 additions and 14 deletions.
26 changes: 12 additions & 14 deletions src/project/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,9 @@ impl SpecType {
pub struct Project {
/// Root folder of the project
root: PathBuf,
/// Reqwest client shared for this project
client: reqwest::Client,
/// Authenticated reqwest client shared for this project
authenticated_client: ClientWithMiddleware,
/// Reqwest client shared for this project.
/// This is wrapped in a `OnceLock` to allow for lazy initialization.
client: OnceLock<(reqwest::Client, ClientWithMiddleware)>,
/// The repodata gateway to use for answering queries about repodata.
/// This is wrapped in a `OnceLock` to allow for lazy initialization.
repodata_gateway: OnceLock<Gateway>,
Expand Down Expand Up @@ -140,12 +139,9 @@ impl Project {

let config = Config::load(&root);

let (client, authenticated_client) = build_reqwest_clients(Some(&config));

Self {
root,
client,
authenticated_client,
client: Default::default(),
manifest,
env_vars,
mapping_source: Default::default(),
Expand Down Expand Up @@ -228,12 +224,9 @@ impl Project {
// Load the user configuration from the local project and all default locations
let config = Config::load(root);

let (client, authenticated_client) = build_reqwest_clients(Some(&config));

Ok(Self {
root: root.to_owned(),
client,
authenticated_client,
client: Default::default(),
manifest,
env_vars,
mapping_source: Default::default(),
Expand Down Expand Up @@ -500,13 +493,18 @@ impl Project {

/// Returns the reqwest client used for http networking
pub fn client(&self) -> &reqwest::Client {
&self.client
&self.client_and_authenticated_client().0
}

/// Create an authenticated reqwest client for this project
/// use authentication from `rattler_networking`
pub fn authenticated_client(&self) -> &ClientWithMiddleware {
&self.authenticated_client
&self.client_and_authenticated_client().1
}

fn client_and_authenticated_client(&self) -> &(reqwest::Client, ClientWithMiddleware) {
self.client
.get_or_init(|| build_reqwest_clients(Some(&self.config)))
}

pub fn config(&self) -> &Config {
Expand Down

0 comments on commit 4f10a1a

Please sign in to comment.