shrupl/src/sharry/api.rs

88 lines
2 KiB
Rust
Raw Normal View History

2025-06-08 00:40:29 +00:00
use std::fmt;
2025-06-04 22:01:17 +00:00
2025-06-14 00:07:43 +00:00
use log::trace;
2025-05-22 17:34:44 +00:00
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
pub struct Uri(String);
2025-05-22 17:34:44 +00:00
impl fmt::Display for Uri {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl AsRef<[u8]> for Uri {
fn as_ref(&self) -> &[u8] {
self.0.as_bytes()
}
}
2025-05-26 23:56:31 +00:00
impl Uri {
pub fn new(protocol: impl fmt::Display, base_url: impl fmt::Display) -> Self {
Self(format!("{}://{}", protocol, base_url))
2025-05-22 17:34:44 +00:00
}
fn endpoint(&self, path: fmt::Arguments) -> String {
let uri = format!("{}/api/v2/{path}", self.0);
2025-06-14 00:07:43 +00:00
trace!("endpoint: {uri:?}");
uri
}
2025-05-22 17:34:44 +00:00
pub fn share_create(&self) -> String {
self.endpoint(format_args!("alias/upload/new"))
}
pub fn share_notify(&self, share_id: &str) -> String {
self.endpoint(format_args!("alias/mail/notify/{share_id}"))
}
pub fn file_create(&self, share_id: &str) -> String {
self.endpoint(format_args!("alias/upload/{share_id}/files/tus"))
}
pub fn file_patch(&self, share_id: &str, file_id: &str) -> String {
self.endpoint(format_args!("alias/upload/{share_id}/files/tus/{file_id}"))
2025-05-22 17:34:44 +00:00
}
}
#[derive(Serialize, Debug)]
2025-05-22 17:34:44 +00:00
#[allow(non_snake_case)]
2025-05-23 01:11:11 +00:00
pub struct NewShareRequest {
2025-05-27 14:01:09 +00:00
name: String,
2025-05-22 17:34:44 +00:00
validity: u32,
2025-05-27 14:01:09 +00:00
description: Option<String>,
2025-05-22 17:34:44 +00:00
maxViews: u32,
2025-05-23 01:11:11 +00:00
password: Option<String>,
2025-05-22 17:34:44 +00:00
}
2025-05-23 01:11:11 +00:00
impl NewShareRequest {
pub fn new(
2025-05-27 14:01:09 +00:00
name: impl Into<String>,
description: Option<impl Into<String>>,
2025-05-23 01:11:11 +00:00
max_views: u32,
) -> Self {
2025-05-22 17:34:44 +00:00
Self {
2025-05-27 14:01:09 +00:00
name: name.into(),
2025-05-22 17:34:44 +00:00
validity: 0,
2025-05-27 14:01:09 +00:00
description: description.map(Into::into),
2025-05-22 17:34:44 +00:00
maxViews: max_views,
password: None,
}
}
}
#[derive(Deserialize, Debug)]
pub struct NewShareResponse {
2025-05-22 17:34:44 +00:00
pub success: bool,
pub message: String,
pub id: String,
}
2025-05-22 21:07:49 +00:00
#[derive(Deserialize, Debug)]
#[allow(dead_code)]
pub struct NotifyShareResponse {
2025-05-22 21:07:49 +00:00
pub success: bool,
pub message: String,
}