From da0b7528c083318ad25d8cd9c2741fd6212edd26 Mon Sep 17 00:00:00 2001 From: Filipe Pina Date: Thu, 26 May 2022 16:16:39 +0100 Subject: support for client certificates --- src/actions.rs | 48 +++++++++++++++++------------------------------- 1 file changed, 17 insertions(+), 31 deletions(-) (limited to 'src/actions.rs') diff --git a/src/actions.rs b/src/actions.rs index f6cef56..df94c29 100644 --- a/src/actions.rs +++ b/src/actions.rs @@ -1,12 +1,17 @@ use crate::prelude::*; +fn api_client() -> Result<(crate::api::Client, crate::config::Config)> { + let config = crate::config::Config::load()?; + let client = + crate::api::Client::new(&config.base_url(), &config.identity_url(), &config.client_cert_path()); + Ok((client, config)) +} + pub async fn register( email: &str, apikey: crate::locked::ApiKey, ) -> Result<()> { - let config = crate::config::Config::load_async().await?; - let client = - crate::api::Client::new(&config.base_url(), &config.identity_url()); + let (client, config) = api_client()?; client .register(email, &crate::config::device_id(&config).await?, &apikey) @@ -21,10 +26,7 @@ pub async fn login( two_factor_token: Option<&str>, two_factor_provider: Option, ) -> Result<(String, String, u32, String)> { - let config = crate::config::Config::load_async().await?; - let client = - crate::api::Client::new(&config.base_url(), &config.identity_url()); - + let (client, config) = api_client()?; let iterations = client.prelogin(email).await?; let identity = crate::identity::Identity::new(email, &password, iterations)?; @@ -121,9 +123,7 @@ async fn sync_once( std::collections::HashMap, Vec, )> { - let config = crate::config::Config::load_async().await?; - let client = - crate::api::Client::new(&config.base_url(), &config.identity_url()); + let (client, _) = api_client()?; client.sync(access_token).await } @@ -147,9 +147,7 @@ fn add_once( notes: Option<&str>, folder_id: Option<&str>, ) -> Result<()> { - let config = crate::config::Config::load()?; - let client = - crate::api::Client::new(&config.base_url(), &config.identity_url()); + let (client, _) = api_client()?; client.add(access_token, name, data, notes, folder_id)?; Ok(()) } @@ -189,9 +187,7 @@ fn edit_once( folder_uuid: Option<&str>, history: &[crate::db::HistoryEntry], ) -> Result<()> { - let config = crate::config::Config::load()?; - let client = - crate::api::Client::new(&config.base_url(), &config.identity_url()); + let (client, _) = api_client()?; client.edit( access_token, id, @@ -216,9 +212,7 @@ pub fn remove( } fn remove_once(access_token: &str, id: &str) -> Result<()> { - let config = crate::config::Config::load()?; - let client = - crate::api::Client::new(&config.base_url(), &config.identity_url()); + let (client, _) = api_client()?; client.remove(access_token, id)?; Ok(()) } @@ -233,9 +227,7 @@ pub fn list_folders( } fn list_folders_once(access_token: &str) -> Result> { - let config = crate::config::Config::load()?; - let client = - crate::api::Client::new(&config.base_url(), &config.identity_url()); + let (client, _) = api_client()?; client.folders(access_token) } @@ -250,9 +242,7 @@ pub fn create_folder( } fn create_folder_once(access_token: &str, name: &str) -> Result { - let config = crate::config::Config::load()?; - let client = - crate::api::Client::new(&config.base_url(), &config.identity_url()); + let (client, _) = api_client()?; client.create_folder(access_token, name) } @@ -302,15 +292,11 @@ where } fn exchange_refresh_token(refresh_token: &str) -> Result { - let config = crate::config::Config::load()?; - let client = - crate::api::Client::new(&config.base_url(), &config.identity_url()); + let (client, _) = api_client()?; client.exchange_refresh_token(refresh_token) } async fn exchange_refresh_token_async(refresh_token: &str) -> Result { - let config = crate::config::Config::load_async().await?; - let client = - crate::api::Client::new(&config.base_url(), &config.identity_url()); + let (client, _) = api_client()?; client.exchange_refresh_token_async(refresh_token).await } -- cgit v1.2.3-54-g00ecf