From 34888827f8a06de835cbe5833a06914f28cce514 Mon Sep 17 00:00:00 2001 From: Ellie Huxtable Date: Tue, 20 Apr 2021 17:07:11 +0100 Subject: 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 --- src/local/api_client.rs | 87 +++++++++++++++++++++++++------------------------ 1 file changed, 44 insertions(+), 43 deletions(-) (limited to 'src/local/api_client.rs') 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 { - let url = format!("{}/sync/count", self.settings.local.sync_address); - let client = reqwest::blocking::Client::new(); + pub async fn count(&self) -> Result { + 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::()?; + let count = resp.json::().await?; Ok(count.count) } - pub fn get_history( + pub async fn get_history( &self, sync_ts: chrono::DateTime, history_ts: chrono::DateTime, host: Option, ) -> Result> { - 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::()?; + let history = resp.json::().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(()) } -- cgit v1.3.1