Skip to content

Commit

Permalink
Refactoring: simplified BaseHTTPClient interface
Browse files Browse the repository at this point in the history
  • Loading branch information
mchf committed Oct 16, 2024
1 parent 4647706 commit 6b485e0
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 21 deletions.
17 changes: 11 additions & 6 deletions rust/agama-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,19 +181,24 @@ async fn allowed_insecure_api(use_insecure: bool, api_url: String) -> Result<boo
pub async fn run_command(cli: Cli) -> Result<(), ServiceError> {
// somehow check whether we need to ask user for self-signed certificate acceptance
let api_url = cli.opts.api.trim_end_matches('/').to_string();
let insecure = allowed_insecure_api(cli.opts.insecure, api_url.clone()).await?;

let mut client = BaseHTTPClient::default();

client.base_url = api_url.clone();

if allowed_insecure_api(cli.opts.insecure, api_url.clone()).await? {
client = client.insecure();
}

// we need to distinguish commands on those which assume that authentication JWT is already
// available and those which not (or don't need it)
let mut client = if let Commands::Auth(_) = cli.command {
BaseHTTPClient::unauthenticated(insecure)
client = if let Commands::Auth(_) = cli.command {
client.unauthenticated()?
} else {
// this deals with authentication need inside
BaseHTTPClient::new_with_params(insecure)?
client.authenticated()?
};

client.base_url = api_url.clone();

match cli.command {
Commands::Config(subcommand) => run_config_cmd(client, subcommand).await?,
Commands::Probe => {
Expand Down
2 changes: 1 addition & 1 deletion rust/agama-cli/src/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ async fn import(url_string: String, dir: Option<PathBuf>) -> anyhow::Result<()>
}

async fn store_settings<P: AsRef<Path>>(path: P) -> anyhow::Result<()> {
let store = SettingsStore::new(BaseHTTPClient::new()?).await?;
let store = SettingsStore::new(BaseHTTPClient::default().authenticated()?).await?;
let settings = InstallSettings::from_file(&path)?;
store.store(&settings).await?;
Ok(())
Expand Down
31 changes: 18 additions & 13 deletions rust/agama-lib/src/base_http_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ use crate::{auth::AuthToken, error::ServiceError};
#[derive(Clone)]
pub struct BaseHTTPClient {
client: reqwest::Client,
insecure: bool,
pub base_url: String,
}

Expand All @@ -56,34 +57,38 @@ impl Default for BaseHTTPClient {
fn default() -> Self {
Self {
client: reqwest::Client::new(),
insecure: false,
base_url: API_URL.to_owned(),
}
}
}

impl BaseHTTPClient {
pub fn new() -> Result<Self, ServiceError> {
Self::new_with_params(false)
/// Allows the client to connect to remote API with insecure certificate (e.g. self-signed)
pub fn insecure(self) -> Self {
Self {
insecure: true,
..self
}
}

/// Uses `localhost`, authenticates with [`AuthToken`].
pub fn new_with_params(insecure: bool) -> Result<Self, ServiceError> {
pub fn authenticated(self) -> Result<Self, ServiceError> {
Ok(Self {
client: Self::authenticated_client(insecure)?,
..Default::default()
client: Self::authenticated_client(self.insecure)?,
..self
})
}

pub fn unauthenticated(insecure: bool) -> Self {
let default_client = reqwest::Client::new();

Self {
/// Configures itself for connection(s) without authentication token
pub fn unauthenticated(self) -> Result<Self, ServiceError> {
Ok(Self {
client: reqwest::Client::builder()
.danger_accept_invalid_certs(insecure)
.danger_accept_invalid_certs(self.insecure)
.build()
.unwrap_or(default_client),
base_url: API_URL.to_owned(),
}
.map_err(anyhow::Error::new)?,
..self
})
}

fn authenticated_client(insecure: bool) -> Result<reqwest::Client, ServiceError> {
Expand Down
2 changes: 1 addition & 1 deletion rust/agama-lib/src/questions/http_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub struct HTTPClient {
impl HTTPClient {
pub fn new() -> Result<Self, ServiceError> {
Ok(Self {
client: BaseHTTPClient::new()?,
client: BaseHTTPClient::default().authenticated()?,
})
}

Expand Down

0 comments on commit 6b485e0

Please sign in to comment.