diff options
Diffstat (limited to 'atuin-client/src/api_client.rs')
-rw-r--r-- | atuin-client/src/api_client.rs | 90 |
1 files changed, 24 insertions, 66 deletions
diff --git a/atuin-client/src/api_client.rs b/atuin-client/src/api_client.rs index a8ce7b27..4b0c98c7 100644 --- a/atuin-client/src/api_client.rs +++ b/atuin-client/src/api_client.rs @@ -7,21 +7,21 @@ use reqwest::{StatusCode, Url}; use sodiumoxide::crypto::secretbox; use atuin_common::api::{ - AddHistoryRequest, CountResponse, LoginResponse, RegisterResponse, SyncHistoryResponse, + AddHistoryRequest, CountResponse, LoginRequest, LoginResponse, RegisterResponse, + SyncHistoryResponse, }; use atuin_common::utils::hash_str; -use crate::encryption::{decode_key, decrypt}; +use crate::encryption::{decode_key, decrypt, EncryptedHistory}; use crate::history::History; -const VERSION: &str = env!("CARGO_PKG_VERSION"); +static APP_USER_AGENT: &str = concat!("atuin/", env!("CARGO_PKG_VERSION"),); // TODO: remove all references to the encryption key from this // It should be handled *elsewhere* pub struct Client<'a> { sync_addr: &'a str, - token: &'a str, key: secretbox::Key, client: reqwest::Client, } @@ -31,7 +31,7 @@ pub fn register( username: &str, email: &str, password: &str, -) -> Result<RegisterResponse> { +) -> Result<RegisterResponse<'static>> { let mut map = HashMap::new(); map.insert("username", username); map.insert("email", email); @@ -48,7 +48,7 @@ pub fn register( let client = reqwest::blocking::Client::new(); let resp = client .post(url) - .header(USER_AGENT, format!("atuin/{}", VERSION)) + .header(USER_AGENT, APP_USER_AGENT) .json(&map) .send()?; @@ -60,18 +60,14 @@ pub fn register( Ok(session) } -pub fn login(address: &str, username: &str, password: &str) -> Result<LoginResponse> { - let mut map = HashMap::new(); - map.insert("username", username); - map.insert("password", password); - +pub fn login(address: &str, req: LoginRequest) -> Result<LoginResponse<'static>> { let url = format!("{}/login", address); let client = reqwest::blocking::Client::new(); let resp = client .post(url) - .header(USER_AGENT, format!("atuin/{}", VERSION)) - .json(&map) + .header(USER_AGENT, APP_USER_AGENT) + .json(&req) .send()?; if resp.status() != reqwest::StatusCode::OK { @@ -83,31 +79,25 @@ pub fn login(address: &str, username: &str, password: &str) -> Result<LoginRespo } impl<'a> Client<'a> { - pub fn new(sync_addr: &'a str, token: &'a str, key: String) -> Result<Self> { + pub fn new(sync_addr: &'a str, session_token: &'a str, key: String) -> Result<Self> { + let mut headers = HeaderMap::new(); + headers.insert(AUTHORIZATION, format!("Token {}", session_token).parse()?); + Ok(Client { sync_addr, - token, key: decode_key(key)?, - client: reqwest::Client::new(), + client: reqwest::Client::builder() + .user_agent(APP_USER_AGENT) + .default_headers(headers) + .build()?, }) } pub async fn count(&self) -> Result<i64> { let url = format!("{}/sync/count", self.sync_addr); let url = Url::parse(url.as_str())?; - let token = format!("Token {}", self.token); - let token = token.parse()?; - let mut headers = HeaderMap::new(); - headers.insert(AUTHORIZATION, token); - - let resp = self - .client - .get(url) - .header(USER_AGENT, format!("atuin/{}", VERSION)) - .headers(headers) - .send() - .await?; + let resp = self.client.get(url).send().await?; if resp.status() != StatusCode::OK { return Err(eyre!("failed to get count (are you logged in?)")); @@ -137,13 +127,7 @@ impl<'a> Client<'a> { host, ); - let resp = self - .client - .get(url) - .header(AUTHORIZATION, format!("Token {}", self.token)) - .header(USER_AGENT, format!("atuin/{}", VERSION)) - .send() - .await?; + let resp = self.client.get(url).send().await?; let history = resp.json::<SyncHistoryResponse>().await?; let history = history @@ -156,41 +140,15 @@ impl<'a> Client<'a> { Ok(history) } - pub async fn post_history(&self, history: &[AddHistoryRequest]) -> Result<()> { + pub async fn post_history( + &self, + history: &[AddHistoryRequest<'_, EncryptedHistory>], + ) -> Result<()> { let url = format!("{}/history", self.sync_addr); let url = Url::parse(url.as_str())?; - self.client - .post(url) - .json(history) - .header(AUTHORIZATION, format!("Token {}", self.token)) - .header(USER_AGENT, format!("atuin/{}", VERSION)) - .send() - .await?; + self.client.post(url).json(history).send().await?; Ok(()) } - - pub async fn login(&self, username: &str, password: &str) -> Result<LoginResponse> { - let mut map = HashMap::new(); - map.insert("username", username); - map.insert("password", password); - - let url = format!("{}/login", self.sync_addr); - let resp = self - .client - .post(url) - .json(&map) - .header(USER_AGENT, format!("atuin/{}", VERSION)) - .send() - .await?; - - if resp.status() != reqwest::StatusCode::OK { - return Err(eyre!("invalid login details")); - } - - let session = resp.json::<LoginResponse>().await?; - - Ok(session) - } } |