From 0bba75241d59c64375cfece494eb9dcdbaa664fc Mon Sep 17 00:00:00 2001 From: metamuffin Date: Fri, 7 Feb 2025 16:19:20 +0100 Subject: update client --- client/src/lib.rs | 59 ++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 52 insertions(+), 7 deletions(-) (limited to 'client/src') diff --git a/client/src/lib.rs b/client/src/lib.rs index e0ab440..96c39b6 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -4,8 +4,12 @@ Copyright (C) 2025 metamuffin */ use anyhow::Result; -use jellycommon::user::CreateSessionParams; +use jellycommon::{ + api::{ApiHomeResponse, ApiNodeResponse, ApiSearchResponse}, + user::CreateSessionParams, +}; use log::{debug, info}; +use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC}; use reqwest::{ header::{HeaderMap, HeaderValue}, Client, @@ -71,11 +75,50 @@ impl Session { format!("session={}", self.session_token) } - pub async fn node(&self, id: NodeIDOrSlug) -> Result { + pub async fn node( + &self, + id: NodeIDOrSlug, + children: bool, + parents: bool, + ) -> Result { debug!("downloading node {id}"); + let params = match (children, parents) { + (true, true) => "?children&parents", + (true, false) => "?children", + (false, true) => "?parents", + (false, false) => "", + }; + Ok(self + .client + .get(format!("{}/n/{id}{params}", self.instance.base(),)) + .send() + .await? + .error_for_status()? + .json() + .await?) + } + + pub async fn search(&self, query: &str, page: usize) -> Result { + debug!("searching for {query:?}"); + Ok(self + .client + .get(format!( + "{}/search?query={}&page={page}", + utf8_percent_encode(query, NON_ALPHANUMERIC), + self.instance.base(), + )) + .send() + .await? + .error_for_status()? + .json() + .await?) + } + + pub async fn home(&self) -> Result { + debug!("home page"); Ok(self .client - .get(format!("{}/n/{id}", self.instance.base())) + .get(format!("{}/home", self.instance.base(),)) .send() .await? .error_for_status()? @@ -105,7 +148,7 @@ impl Session { debug!("downloading asset {token:?} (w={width})"); self.download_url( writer, - format!("{}/asset/{token}?width={width}", self.instance.base(),), + format!("{}/asset/{token}?width={width}", self.instance.base()), ) .await } @@ -138,11 +181,13 @@ impl Session { Ok(()) } - pub async fn reimport(&self) -> Result<()> { - // TODO error handling + pub async fn reimport(&self, incremental: bool) -> Result<()> { info!("reimport"); self.client - .post(format!("{}/admin/import", self.instance.base())) + .post(format!( + "{}/admin/import?incremental={incremental}", + self.instance.base() + )) .send() .await?; info!("done"); -- cgit v1.2.3-70-g09d2