Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/client/config.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::fmt::Display;
use std::io::ErrorKind;
use std::path::Path;

Expand Down Expand Up @@ -60,3 +61,20 @@ impl ClientConfiguration {
self
}
}

impl Display for ClientConfiguration {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "ClientConfiguration(host: ")?;
match self.host {
Some(ref h) => write!(f, "{h}")?,
None => write!(f, "None")?,
}
write!(f, ", auth: ")?;
match self.auth {
Some(ref a) => write!(f, "{a}")?,
None => write!(f, "None")?,
}
write!(f, ")")?;
Ok(())
}
}
26 changes: 21 additions & 5 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use graphql_client::{GraphQLQuery, Response};
use reqwest::Client;
use serde::de::DeserializeOwned;
use serde::Serialize;
use tracing::info;
use url::Url;

use crate::cli::client::{ClientCommand, ClientOptions, ConfigurationOptions};
Expand All @@ -25,13 +26,18 @@ pub async fn run_client(options: ClientOptions) {
} = options;

let conf = match ClientConfiguration::from_default_file().await {
Ok(conf) => conf.with_host(connection.host).with_auth(connection.auth),
Ok(conf) => {
info!("Configuration from file: {conf}");
conf.with_host(connection.host).with_auth(connection.auth)
}
Err(e) => {
println!("Could not read configuration: {e}");
return;
}
};

info!("Configuration with CLI args included: {conf}");

let client = match NumtrackerClient::from_config(conf).await {
Ok(client) => client,
Err(e) => {
Expand Down Expand Up @@ -86,14 +92,16 @@ struct ConfigureMutation;

impl NumtrackerClient {
async fn from_config(config: ClientConfiguration) -> Result<Self, ClientError> {
let host = config
.host
.unwrap_or(Url::parse("http://localhost:8000").expect("Constant URL is valid"));
let host = config.host.unwrap_or_else(|| {
info!("No host specified, defaulting to localhost:8000");
Url::parse("http://localhost:8000").expect("Constant URL is valid")
});

let auth = match config.auth {
Some(auth) => Some(cli_auth::get_access_token(&auth).await?),
None => None,
};
info!("Querying {host} with auth: {auth:?}");
Ok(NumtrackerClient { auth, host })
}

Expand All @@ -110,7 +118,13 @@ impl NumtrackerClient {
None => client,
Some(token) => client.bearer_auth(token),
};
client.json(&content).send().await?.json().await
client
.json(&content)
.send()
.await?
.error_for_status()?
.json()
.await
}

async fn query_configuration(self, instrument: Option<Vec<String>>) -> Result<(), ClientError> {
Expand All @@ -136,6 +150,8 @@ impl NumtrackerClient {
conf.tracker_file_extension.unwrap_or(conf.instrument)
);
}
} else if data.errors.is_none() {
println!("Query returned no data or errors");
}
Ok(())
}
Expand Down
Loading