aboutsummaryrefslogtreecommitdiff
path: root/client/src/lib.rs
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2025-04-30 11:24:14 +0200
committermetamuffin <metamuffin@disroot.org>2025-04-30 11:24:14 +0200
commitd6a039a10ac3c81d410beb9b648d29524ca1e278 (patch)
treefa521277e3ea94f796e21071f4d2b372c5b9830c /client/src/lib.rs
parenta2ef3f6ec4c830611fde1a2e935588ccbbc61c03 (diff)
downloadjellything-d6a039a10ac3c81d410beb9b648d29524ca1e278.tar
jellything-d6a039a10ac3c81d410beb9b648d29524ca1e278.tar.bz2
jellything-d6a039a10ac3c81d410beb9b648d29524ca1e278.tar.zst
smaller fixes and remove useless deps
Diffstat (limited to 'client/src/lib.rs')
-rw-r--r--client/src/lib.rs196
1 files changed, 0 insertions, 196 deletions
diff --git a/client/src/lib.rs b/client/src/lib.rs
deleted file mode 100644
index d3172fd..0000000
--- a/client/src/lib.rs
+++ /dev/null
@@ -1,196 +0,0 @@
-/*
- This file is part of jellything (https://codeberg.org/metamuffin/jellything)
- which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
- Copyright (C) 2025 metamuffin <metamuffin.org>
-*/
-use anyhow::Result;
-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,
-};
-use stream::StreamSpec;
-use tokio::io::AsyncWriteExt;
-
-pub use jellycommon::*;
-
-#[derive(Debug, Clone)]
-pub struct Instance {
- pub host: String,
- pub tls: bool,
-}
-
-impl Instance {
- pub fn new(host: String, tls: bool) -> Self {
- Self { host, tls }
- }
- pub fn base(&self) -> String {
- format!(
- "{}://{}",
- if self.tls { "https" } else { "http" },
- self.host
- )
- }
- pub async fn login(self, data: CreateSessionParams) -> anyhow::Result<Session> {
- let session_token = Client::builder()
- .build()?
- .post(format!("{}/api/create_session", self.base()))
- .json(&data)
- .send()
- .await?
- .json()
- .await?;
-
- let mut headers = HeaderMap::new();
- headers.insert(
- "Cookie",
- HeaderValue::from_str(&format!("session={session_token}")).unwrap(),
- );
- headers.insert("Accept", HeaderValue::from_static("application/json"));
-
- Ok(Session {
- instance: self,
- session_token,
- client: Client::builder().default_headers(headers).build().unwrap(),
- })
- }
-}
-
-pub struct Session {
- client: Client,
- instance: Instance,
- session_token: String,
-}
-
-pub trait UnpinWrite: tokio::io::AsyncWrite + std::marker::Unpin {}
-impl<T: tokio::io::AsyncWrite + std::marker::Unpin> UnpinWrite for T {}
-
-impl Session {
- fn session_param(&self) -> String {
- format!("session={}", self.session_token)
- }
-
- 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!("{}/home", self.instance.base(),))
- .send()
- .await?
- .error_for_status()?
- .json()
- .await?)
- }
-
- pub async fn node_thumbnail(
- &self,
- writer: impl UnpinWrite,
- id: NodeIDOrSlug,
- width: usize,
- time: f64,
- ) -> Result<()> {
- debug!("downloading thumbnail for {id} at {time}s");
- self.download_url(
- writer,
- format!(
- "{}/n/{id}/thumbnail?t={time}&width={width}",
- self.instance.base(),
- ),
- )
- .await
- }
-
- pub async fn asset(&self, writer: impl UnpinWrite, token: &str, width: usize) -> Result<()> {
- debug!("downloading asset {token:?} (w={width})");
- self.download_url(
- writer,
- format!("{}/asset/{token}?width={width}", self.instance.base()),
- )
- .await
- }
-
- pub async fn stream(
- &self,
- writer: impl UnpinWrite,
- id: NodeIDOrSlug,
- stream_spec: &StreamSpec,
- ) -> Result<()> {
- self.download_url(writer, self.stream_url(id, stream_spec))
- .await
- }
-
- pub fn stream_url(&self, id: NodeIDOrSlug, stream_spec: &StreamSpec) -> String {
- format!(
- "{}/n/{}/stream{}&{}",
- self.instance.base(),
- id,
- stream_spec.to_query(),
- self.session_param()
- )
- }
-
- pub async fn download_url(&self, mut writer: impl UnpinWrite, url: String) -> Result<()> {
- let mut r = self.client.get(url).send().await?.error_for_status()?;
- while let Some(chunk) = r.chunk().await? {
- writer.write_all(&chunk).await?;
- }
- Ok(())
- }
-
- pub async fn reimport(&self, incremental: bool) -> Result<()> {
- info!("reimport");
- self.client
- .post(format!(
- "{}/admin/import?incremental={incremental}",
- self.instance.base()
- ))
- .send()
- .await?;
- info!("done");
- Ok(())
- }
-}