From c40ba8390be1e4bec2f474a4caf5007c4daba3cb Mon Sep 17 00:00:00 2001 From: nazeh Date: Sat, 27 Jul 2024 19:31:26 +0300 Subject: [PATCH] feat(pubky): enable get and put methods --- pubky/src/client.rs | 2 +- pubky/src/client/public.rs | 32 ++++++++------------------------ 2 files changed, 9 insertions(+), 25 deletions(-) diff --git a/pubky/src/client.rs b/pubky/src/client.rs index 2839110..d114a72 100644 --- a/pubky/src/client.rs +++ b/pubky/src/client.rs @@ -1,6 +1,6 @@ mod auth; mod pkarr; -// mod public; +mod public; use std::{collections::HashMap, fmt::format, time::Duration}; diff --git a/pubky/src/client/public.rs b/pubky/src/client/public.rs index e2d396f..2c1b39f 100644 --- a/pubky/src/client/public.rs +++ b/pubky/src/client/public.rs @@ -5,41 +5,31 @@ use pkarr::PublicKey; use super::{PubkyClient, Result}; impl PubkyClient { - pub fn put(&self, pubky: &PublicKey, path: &str, content: &[u8]) -> Result<()> { + pub async fn put(&self, pubky: &PublicKey, path: &str, content: &[u8]) -> Result<()> { let path = normalize_path(path); - let (_, mut url) = self.resolve_pubky_homeserver(pubky)?; + let (_, mut url) = self.resolve_pubky_homeserver(pubky).await?; url.set_path(&format!("/{pubky}/{path}")); - self.request(super::HttpMethod::Put, &url) - .send_bytes(content)?; + self.http.put(url).body(content.to_owned()).send().await?; Ok(()) } - pub fn get(&self, pubky: &PublicKey, path: &str) -> Result { + pub async fn get(&self, pubky: &PublicKey, path: &str) -> Result { let path = normalize_path(path); - let (_, mut url) = self.resolve_pubky_homeserver(pubky)?; + let (_, mut url) = self.resolve_pubky_homeserver(pubky).await?; url.set_path(&format!("/{pubky}/{path}")); - let response = self.request(super::HttpMethod::Get, &url).call()?; - - let len = response - .header("Content-Length") - .and_then(|s| s.parse::().ok()) - // TODO: return an error in case content-length header is missing - .unwrap_or(0); + let response = self.http.get(url).send().await?; // TODO: bail on too large files. + let bytes = response.bytes().await?; - let mut bytes = vec![0; len as usize]; - - response.into_reader().read_exact(&mut bytes); - - Ok(bytes.into()) + Ok(bytes) } } @@ -86,12 +76,6 @@ mod tests { .put(&keypair.public_key(), "/pub/foo.txt", &[0, 1, 2, 3, 4]) .await; - if let Err(Error::Ureq(ureqerror)) = response { - if let Some(r) = ureqerror.into_response() { - dbg!(r.into_string()); - } - } - let response = client .get(&keypair.public_key(), "/pub/foo.txt") .await