From d0df13ce08baaef2cd26f98b10947afd7edabd7e Mon Sep 17 00:00:00 2001 From: Cyborus Date: Sun, 26 Nov 2023 15:50:02 -0500 Subject: [PATCH] add `miscellaneous` methods --- src/lib.rs | 41 ++++++++++++++ src/misc.rs | 156 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 197 insertions(+) create mode 100644 src/misc.rs diff --git a/src/lib.rs b/src/lib.rs index df34694..81d7bca 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,11 +8,13 @@ pub struct Forgejo { client: Client, } +mod misc; mod organization; mod issue; mod repository; mod user; +pub use misc::*; pub use organization::*; pub use issue::*; pub use repository::*; @@ -86,6 +88,12 @@ impl Forgejo { self.execute_opt(request).await } + async fn get_str(&self, path: &str) -> Result { + let url = self.url.join("api/v1/").unwrap().join(path).unwrap(); + let request = self.client.get(url).build()?; + self.execute_str(request).await + } + async fn post( &self, path: &str, @@ -106,6 +114,26 @@ impl Forgejo { self.execute(request).await } + async fn post_str_out( + &self, + path: &str, + body: &T, + ) -> Result { + let url = self.url.join("api/v1/").unwrap().join(path).unwrap(); + let request = self.client.post(url).json(body).build()?; + self.execute_str(request).await + } + + async fn post_raw( + &self, + path: &str, + body: String, + ) -> Result { + let url = self.url.join("api/v1/").unwrap().join(path).unwrap(); + let request = self.client.post(url).body(body).build()?; + self.execute_str(request).await + } + async fn delete(&self, path: &str) -> Result<(), ForgejoError> { let url = self.url.join("api/v1/").unwrap().join(path).unwrap(); let request = self.client.delete(url).build()?; @@ -134,6 +162,19 @@ impl Forgejo { } } + /// Like `execute`, but returns a `String`. + async fn execute_str(&self, request: Request) -> Result { + let response = self.client.execute(request).await?; + match response.status() { + status if status.is_success() => Ok(response.text().await?), + status if status.is_client_error() => Err(ForgejoError::ApiError( + status, + response.json::().await?.message, + )), + status => Err(ForgejoError::UnexpectedStatusCode(status)), + } + } + /// Like `execute`, but returns `Ok(None)` on 404. async fn execute_opt( &self, diff --git a/src/misc.rs b/src/misc.rs new file mode 100644 index 0000000..e2aeb5a --- /dev/null +++ b/src/misc.rs @@ -0,0 +1,156 @@ +use super::*; + +impl Forgejo { + pub async fn get_gitignore_templates(&self) -> Result, ForgejoError> { + self.get("gitignore/templates").await + } + + pub async fn get_gitignore_template(&self, name: &str) -> Result, ForgejoError> { + self.get_opt(&format!("gitignore/templates/{name}")).await + } + + pub async fn get_label_templates(&self) -> Result, ForgejoError> { + self.get("label/templates").await + } + + pub async fn get_label_template(&self, name: &str) -> Result, ForgejoError> { + self.get(&format!("label/templates/{name}")).await + } + + pub async fn get_licenses(&self) -> Result, ForgejoError> { + self.get("licenses").await + } + + pub async fn get_license(&self, name: &str) -> Result, ForgejoError> { + self.get_opt(&format!("license/{name}")).await + } + + pub async fn render_markdown(&self, opt: MarkdownOption) -> Result { + self.post_str_out("markdown", &opt).await + } + + pub async fn render_markdown_raw(&self, body: String) -> Result { + self.post_raw("markdown/raw", body).await + } + + pub async fn render_markup(&self, opt: MarkupOption) -> Result { + self.post_str_out("markup", &opt).await + } + + pub async fn nodeinfo(&self) -> Result { + self.get("nodeinfo").await + } + + pub async fn signing_key(&self) -> Result { + self.get_str("signing-key.gpg").await + } + + pub async fn version(&self) -> Result { + self.get("version").await + } +} + +#[derive(serde::Deserialize, Debug, PartialEq)] +pub struct GitignoreTemplateInfo { + pub name: String, + pub source: String, +} + +#[derive(serde::Deserialize, Debug, PartialEq)] +pub struct LabelTemplate { + pub color: String, + pub description: String, + pub exclusive: bool, + pub name: String, +} + +#[derive(serde::Deserialize, Debug, PartialEq)] +pub struct LicenseTemplateListEntry { + pub key: String, + pub name: String, + pub url: Url, +} + +#[derive(serde::Deserialize, Debug, PartialEq)] +pub struct LicenseTemplateInfo { + pub body: String, + pub implementation: String, + pub key: String, + pub name: String, + pub url: Url, +} + +#[derive(serde::Serialize, Debug, PartialEq, Default)] +pub struct MarkdownOption { + #[serde(rename = "Context")] + pub context: String, + #[serde(rename = "Mode")] + pub mode: String, + #[serde(rename = "Text")] + pub text: String, + #[serde(rename = "Wiki")] + pub wiki: String, +} + +#[derive(serde::Serialize, Debug, PartialEq, Default)] +pub struct MarkupOption { + #[serde(rename = "Context")] + pub context: String, + #[serde(rename = "FilePath")] + pub file_path: String, + #[serde(rename = "Mode")] + pub mode: String, + #[serde(rename = "Text")] + pub text: String, + #[serde(rename = "Wiki")] + pub wiki: String, +} + +#[derive(serde::Deserialize, Debug, PartialEq)] +pub struct NodeInfo { + pub metadata: std::collections::BTreeMap, + #[serde(rename = "openRegistrations")] + pub open_registrations: bool, + pub protocols: Vec, + pub services: NodeInfoServices, + pub software: NodeInfoSoftware, + pub usage: NodeInfoUsage, + pub version: String, +} + +#[derive(serde::Deserialize, Debug, PartialEq)] +pub struct NodeInfoServices { + pub inbound: Vec, + pub outbound: Vec, +} + +#[derive(serde::Deserialize, Debug, PartialEq)] +pub struct NodeInfoSoftware { + pub homepage: Url, + pub name: String, + pub repository: Url, + pub version: String, +} + +#[derive(serde::Deserialize, Debug, PartialEq)] +pub struct NodeInfoUsage { + #[serde(rename = "localComments")] + pub local_comments: u64, + #[serde(rename = "localPosts")] + pub local_posts: u64, + pub users: NodeInfoUsageUsers, +} + +#[derive(serde::Deserialize, Debug, PartialEq)] +pub struct NodeInfoUsageUsers { + #[serde(rename = "activeHalfYear")] + pub active_half_year: u64, + #[serde(rename = "activeMonth")] + pub active_month: u64, + pub total: u64, +} + +#[derive(serde::Deserialize, Debug, PartialEq)] +pub struct ServerVersion { + pub version: String, +}