diff options
| author | Ellie Huxtable <e@elm.sh> | 2021-04-20 21:53:07 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-04-20 20:53:07 +0000 |
| commit | a21737e2b7f8d1e426726bdd7536033f299d476a (patch) | |
| tree | e940afdff9c145d25d9a2895fd44a77d70719a2e /src/local/api_client.rs | |
| parent | Switch to Warp + SQLx, use async, switch to Rust stable (#36) (diff) | |
| download | atuin-a21737e2b7f8d1e426726bdd7536033f299d476a.zip | |
Use cargo workspaces (#37)
* Switch to Cargo workspaces
Breaking things into "client", "server" and "common" makes managing the
codebase much easier!
client - anything running on a user's machine for adding history
server - handles storing/syncing history and running a HTTP server
common - request/response API definitions, common utils, etc
* Update dockerfile
Diffstat (limited to 'src/local/api_client.rs')
| -rw-r--r-- | src/local/api_client.rs | 95 |
1 files changed, 0 insertions, 95 deletions
diff --git a/src/local/api_client.rs b/src/local/api_client.rs deleted file mode 100644 index 1b64a295..00000000 --- a/src/local/api_client.rs +++ /dev/null @@ -1,95 +0,0 @@ -use chrono::Utc; -use eyre::Result; -use reqwest::header::{HeaderMap, AUTHORIZATION}; -use reqwest::Url; -use sodiumoxide::crypto::secretbox; - -use crate::api::{AddHistoryRequest, CountResponse, SyncHistoryResponse}; -use crate::local::encryption::decrypt; -use crate::local::history::History; -use crate::utils::hash_str; - -pub struct Client<'a> { - sync_addr: &'a str, - token: &'a str, - key: secretbox::Key, - client: reqwest::Client, -} - -impl<'a> Client<'a> { - pub fn new(sync_addr: &'a str, token: &'a str, key: secretbox::Key) -> Self { - Client { - sync_addr, - token, - key, - client: reqwest::Client::new(), - } - } - - 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).headers(headers).send().await?; - - let count = resp.json::<CountResponse>().await?; - - Ok(count.count) - } - - pub async fn get_history( - &self, - sync_ts: chrono::DateTime<Utc>, - history_ts: chrono::DateTime<Utc>, - host: Option<String>, - ) -> Result<Vec<History>> { - let host = match host { - None => hash_str(&format!("{}:{}", whoami::hostname(), whoami::username())), - Some(h) => h, - }; - - let url = format!( - "{}/sync/history?sync_ts={}&history_ts={}&host={}", - self.sync_addr, - urlencoding::encode(sync_ts.to_rfc3339().as_str()), - urlencoding::encode(history_ts.to_rfc3339().as_str()), - host, - ); - - let resp = self - .client - .get(url) - .header(AUTHORIZATION, format!("Token {}", self.token)) - .send() - .await?; - - let history = resp.json::<SyncHistoryResponse>().await?; - let history = history - .history - .iter() - .map(|h| serde_json::from_str(h).expect("invalid base64")) - .map(|h| decrypt(&h, &self.key).expect("failed to decrypt history! check your key")) - .collect(); - - Ok(history) - } - - pub async fn post_history(&self, history: &[AddHistoryRequest]) -> 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)) - .send() - .await?; - - Ok(()) - } -} |
