diff options
author | Ellie Huxtable <ellie@elliehuxtable.com> | 2023-03-20 09:26:54 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-20 09:26:54 +0000 |
commit | dcd77749dd1fdf6b0c8183bfbdf4f97bf238ebe4 (patch) | |
tree | 97c623911eeb52da65c2b3fd80092f2c86f3dd18 /atuin-client/src/api_client.rs | |
parent | edcd477153d00944c5dae16ec3ba69e339e1450c (diff) |
Add history deletion (#791)
* Drop events. I'd still like to do them, but differently
* Start adding delete api stuff
* Set mailmap
* Delete delete delete
* Fix tests
* Make clippy happy
Diffstat (limited to 'atuin-client/src/api_client.rs')
-rw-r--r-- | atuin-client/src/api_client.rs | 43 |
1 files changed, 41 insertions, 2 deletions
diff --git a/atuin-client/src/api_client.rs b/atuin-client/src/api_client.rs index 44375c06f..dee986137 100644 --- a/atuin-client/src/api_client.rs +++ b/atuin-client/src/api_client.rs @@ -1,4 +1,5 @@ use std::collections::HashMap; +use std::collections::HashSet; use chrono::Utc; use eyre::{bail, Result}; @@ -9,8 +10,8 @@ use reqwest::{ use sodiumoxide::crypto::secretbox; use atuin_common::api::{ - AddHistoryRequest, CountResponse, ErrorResponse, IndexResponse, LoginRequest, LoginResponse, - RegisterResponse, SyncHistoryResponse, + AddHistoryRequest, CountResponse, DeleteHistoryRequest, ErrorResponse, IndexResponse, + LoginRequest, LoginResponse, RegisterResponse, StatusResponse, SyncHistoryResponse, }; use semver::Version; @@ -138,11 +139,27 @@ impl<'a> Client<'a> { Ok(count.count) } + pub async fn status(&self) -> Result<StatusResponse> { + let url = format!("{}/sync/status", self.sync_addr); + let url = Url::parse(url.as_str())?; + + let resp = self.client.get(url).send().await?; + + if resp.status() != StatusCode::OK { + bail!("failed to get status (are you logged in?)"); + } + + let status = resp.json::<StatusResponse>().await?; + + Ok(status) + } + pub async fn get_history( &self, sync_ts: chrono::DateTime<Utc>, history_ts: chrono::DateTime<Utc>, host: Option<String>, + deleted: HashSet<String>, ) -> Result<Vec<History>> { let host = match host { None => hash_str(&format!("{}:{}", whoami::hostname(), whoami::username())), @@ -163,8 +180,17 @@ impl<'a> Client<'a> { let history = history .history .iter() + // TODO: handle deletion earlier in this chain .map(|h| serde_json::from_str(h).expect("invalid base64")) .map(|h| decrypt(&h, &self.key).expect("failed to decrypt history! check your key")) + .map(|mut h| { + if deleted.contains(&h.id) { + h.deleted_at = Some(chrono::Utc::now()); + h.command = String::from(""); + } + + h + }) .collect(); Ok(history) @@ -178,4 +204,17 @@ impl<'a> Client<'a> { Ok(()) } + + pub async fn delete_history(&self, h: History) -> Result<()> { + let url = format!("{}/history", self.sync_addr); + let url = Url::parse(url.as_str())?; + + self.client + .delete(url) + .json(&DeleteHistoryRequest { client_id: h.id }) + .send() + .await?; + + Ok(()) + } } |