shrupl/src/sharry/api.rs

73 lines
1.5 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};
2025-06-04 22:01:17 +00:00
#[derive(Serialize, Deserialize, Debug, Hash)]
2025-05-26 23:56:31 +00:00
pub struct Uri {
2025-05-22 17:34:44 +00:00
protocol: String,
base_url: String,
}
2025-05-26 23:56:31 +00:00
impl Uri {
2025-05-24 00:22:24 +00:00
pub fn with_protocol(protocol: impl Into<String>, base_url: impl Into<String>) -> Self {
2025-05-22 17:34:44 +00:00
Self {
2025-05-22 21:07:49 +00:00
protocol: protocol.into(),
2025-05-22 17:34:44 +00:00
base_url: base_url.into(),
}
}
pub fn endpoint(&self, endpoint: impl fmt::Display) -> String {
let uri = format!("{self}/{endpoint}");
2025-06-14 00:07:43 +00:00
trace!("endpoint: {uri:?}");
uri
}
2025-05-24 00:22:24 +00:00
}
2025-05-22 17:34:44 +00:00
2025-06-08 00:40:29 +00:00
impl fmt::Display for Uri {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2025-06-04 22:01:17 +00:00
write!(f, "{}://{}/api/v2", self.protocol, self.base_url)
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)]
2025-05-23 22:13:17 +00:00
pub(super) 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)]
2025-05-23 22:13:17 +00:00
pub(super) struct NotifyShareResponse {
2025-05-22 21:07:49 +00:00
pub success: bool,
pub message: String,
}