aboutsummaryrefslogtreecommitdiffstats
path: root/src/local/api_client.rs
diff options
context:
space:
mode:
authorEllie Huxtable <e@elm.sh>2021-04-20 17:07:11 +0100
committerGitHub <noreply@github.com>2021-04-20 16:07:11 +0000
commit34888827f8a06de835cbe5833a06914f28cce514 (patch)
tree8b56f20e50065cd2c222d5e8e067ec55cf1947a1 /src/local/api_client.rs
parentOptimise docker (#34) (diff)
downloadatuin-34888827f8a06de835cbe5833a06914f28cce514.zip
Switch to Warp + SQLx, use async, switch to Rust stable (#36)
* Switch to warp + sql, use async and stable rust * Update CI to use stable
Diffstat (limited to 'src/local/api_client.rs')
-rw-r--r--src/local/api_client.rs87
1 files changed, 44 insertions, 43 deletions
diff --git a/src/local/api_client.rs b/src/local/api_client.rs
index 434c07ba..1b64a295 100644
--- a/src/local/api_client.rs
+++ b/src/local/api_client.rs
@@ -1,93 +1,94 @@
use chrono::Utc;
use eyre::Result;
-use reqwest::header::AUTHORIZATION;
+use reqwest::header::{HeaderMap, AUTHORIZATION};
+use reqwest::Url;
+use sodiumoxide::crypto::secretbox;
-use crate::api::{AddHistoryRequest, CountResponse, ListHistoryResponse};
-use crate::local::encryption::{decrypt, load_key};
+use crate::api::{AddHistoryRequest, CountResponse, SyncHistoryResponse};
+use crate::local::encryption::decrypt;
use crate::local::history::History;
-use crate::settings::Settings;
use crate::utils::hash_str;
pub struct Client<'a> {
- settings: &'a Settings,
+ sync_addr: &'a str,
+ token: &'a str,
+ key: secretbox::Key,
+ client: reqwest::Client,
}
impl<'a> Client<'a> {
- pub const fn new(settings: &'a Settings) -> Self {
- Client { settings }
+ pub fn new(sync_addr: &'a str, token: &'a str, key: secretbox::Key) -> Self {
+ Client {
+ sync_addr,
+ token,
+ key,
+ client: reqwest::Client::new(),
+ }
}
- pub fn count(&self) -> Result<i64> {
- let url = format!("{}/sync/count", self.settings.local.sync_address);
- let client = reqwest::blocking::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 resp = client
- .get(url)
- .header(
- AUTHORIZATION,
- format!("Token {}", self.settings.local.session_token),
- )
- .send()?;
+ let mut headers = HeaderMap::new();
+ headers.insert(AUTHORIZATION, token);
+
+ let resp = self.client.get(url).headers(headers).send().await?;
- let count = resp.json::<CountResponse>()?;
+ let count = resp.json::<CountResponse>().await?;
Ok(count.count)
}
- pub fn get_history(
+ pub async fn get_history(
&self,
sync_ts: chrono::DateTime<Utc>,
history_ts: chrono::DateTime<Utc>,
host: Option<String>,
) -> Result<Vec<History>> {
- let key = load_key(self.settings)?;
-
let host = match host {
None => hash_str(&format!("{}:{}", whoami::hostname(), whoami::username())),
Some(h) => h,
};
- // this allows for syncing between users on the same machine
let url = format!(
"{}/sync/history?sync_ts={}&history_ts={}&host={}",
- self.settings.local.sync_address,
- sync_ts.to_rfc3339(),
- history_ts.to_rfc3339(),
+ self.sync_addr,
+ urlencoding::encode(sync_ts.to_rfc3339().as_str()),
+ urlencoding::encode(history_ts.to_rfc3339().as_str()),
host,
);
- let client = reqwest::blocking::Client::new();
- let resp = client
+ let resp = self
+ .client
.get(url)
- .header(
- AUTHORIZATION,
- format!("Token {}", self.settings.local.session_token),
- )
- .send()?;
+ .header(AUTHORIZATION, format!("Token {}", self.token))
+ .send()
+ .await?;
- let history = resp.json::<ListHistoryResponse>()?;
+ 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, &key).expect("failed to decrypt history! check your key"))
+ .map(|h| decrypt(&h, &self.key).expect("failed to decrypt history! check your key"))
.collect();
Ok(history)
}
- pub fn post_history(&self, history: &[AddHistoryRequest]) -> Result<()> {
- let client = reqwest::blocking::Client::new();
+ pub async fn post_history(&self, history: &[AddHistoryRequest]) -> Result<()> {
+ let url = format!("{}/history", self.sync_addr);
+ let url = Url::parse(url.as_str())?;
- let url = format!("{}/history", self.settings.local.sync_address);
- client
+ self.client
.post(url)
.json(history)
- .header(
- AUTHORIZATION,
- format!("Token {}", self.settings.local.session_token),
- )
- .send()?;
+ .header(AUTHORIZATION, format!("Token {}", self.token))
+ .send()
+ .await?;
Ok(())
}