aboutsummaryrefslogtreecommitdiff
path: root/client/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'client/src/lib.rs')
-rw-r--r--client/src/lib.rs59
1 files changed, 52 insertions, 7 deletions
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 <metamuffin.org>
*/
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<Node> {
+ pub async fn node(
+ &self,
+ id: NodeIDOrSlug,
+ children: bool,
+ parents: bool,
+ ) -> Result<ApiNodeResponse> {
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<ApiSearchResponse> {
+ 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<ApiHomeResponse> {
+ 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");