diff --git a/Cargo.toml b/Cargo.toml index fee3ea0..6cbd57e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,7 @@ edition = "2021" [dependencies] anyhow = "1.0.77" base64 = "0.21.5" -reqwest = { version = "0.11.23", features = ["json"] } +reqwest = "0.11.23" serde = { version = "1.0.193", features = ["derive", "std"] } serde_json = "1.0.108" thiserror = "1.0.52" diff --git a/src/client.rs b/src/client.rs index 6eb4de4..d9134dc 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1,8 +1,10 @@ +use std::io::Write; use anyhow::Result; use base64::Engine; use base64::engine::{GeneralPurpose}; use crate::post::{Post, Posts}; use reqwest::header::{AUTHORIZATION, HeaderMap, HeaderValue, USER_AGENT}; +use reqwest::Response; #[derive(Clone, Debug)] pub struct Client<'a> { @@ -38,31 +40,44 @@ impl<'a> Client<'a> { base64_engine.encode(format!("{username}:{apikey}")) } - pub async fn list_posts(&mut self, limit: Option, tags: Option>, page: Option) -> Result> { - let mut url = url::Url::parse(format!("{}/posts.json", self.host).as_str())?; + async fn list_posts_raw(&mut self, limit: Option, tags: Option>, page: Option) -> Result { + let mut url = url::Url::parse(format!("{}/posts.json", self.host).as_str())?; - let mut query_params = Vec::new(); - if let Some(limit) = limit { - query_params.push(format!("limit={limit}")); - } + let mut query_params = Vec::new(); + if let Some(limit) = limit { + query_params.push(format!("limit={limit}")); + } - if let Some(page) = page { - query_params.push(format!("page={page}")); - } + if let Some(page) = page { + query_params.push(format!("page={page}")); + } - if let Some(tags) = tags { - if tags.len() > 0 { - query_params.push(format!("tags={}", tags.join(","))); - } - } + if let Some(tags) = tags { + if tags.len() > 0 { + query_params.push(format!("tags={}", tags.join(","))); + } + } - url.set_query(Some(&query_params.join("&"))); + url.set_query(Some(&query_params.join("&"))); Ok(self.http_client.get(url.as_str()) .send() - .await? - .json::() - .await?.into()) + .await?) + } + + pub async fn list_posts(&mut self, limit: Option, tags: Option>, page: Option) -> Result> { + let res = self.list_posts_raw(limit, tags, page).await?; + let text = res.text().await?; + + /* + let mut debug_file = std::fs::OpenOptions::new() + .write(true) + .create(true) + .open("./debug.json")?; + debug_file.write_all(text.as_bytes())?; + */ + + Ok(serde_json::from_str(text.as_str())?) } }