From cd544e71e3b169282bed179417c2e1c736ba3dd4 Mon Sep 17 00:00:00 2001 From: Matthias Puchstein Date: Mon, 24 Mar 2025 22:24:46 +0100 Subject: [PATCH] alhp_api general should be working --- crates/alhp_api/src/lib.rs | 322 ++++++++++++++++++++++--------------- src/main.rs | 19 ++- 2 files changed, 203 insertions(+), 138 deletions(-) diff --git a/crates/alhp_api/src/lib.rs b/crates/alhp_api/src/lib.rs index d7c530a..01c9ff0 100644 --- a/crates/alhp_api/src/lib.rs +++ b/crates/alhp_api/src/lib.rs @@ -1,143 +1,203 @@ -use log::{error, info}; -use reqwest::StatusCode; -use reqwest::blocking::Client; -use serde::{Deserialize, Serialize}; -use std::error::Error; -use std::fmt; - const API_BASE_URL: &str = "https://api.alhp.dev"; const API_PACKAGES_EXT: &str = "/packages?"; const API_GENERAL_EXT: &str = "/stats?"; -#[derive(Debug, Clone, Copy, Serialize, Deserialize, Eq, PartialEq)] -#[serde(rename_all = "lowercase")] -pub enum PackageStatus { - Latest, - Failed, - Built, - Skipped, - Delayed, - Building, - Signing, - Unknown, - Queued, -} +pub mod packages { + use crate::{API_BASE_URL, API_PACKAGES_EXT}; + use log::{error, info}; + use reqwest::StatusCode; + use serde::{Deserialize, Serialize}; + use std::error::Error; + use std::fmt; -impl fmt::Display for PackageStatus { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!( - f, - "{}", - match self { - PackageStatus::Latest => "latest", - PackageStatus::Failed => "failed", - PackageStatus::Built => "build", - PackageStatus::Skipped => "skipped", - PackageStatus::Delayed => "delayed", - PackageStatus::Building => "building", - PackageStatus::Signing => "signing", - PackageStatus::Unknown => "unknown", - PackageStatus::Queued => "queued", + #[derive(Debug, Clone, Copy, Serialize, Deserialize, Eq, PartialEq)] + #[serde(rename_all = "lowercase")] + pub enum PackageStatus { + Latest, + Failed, + Built, + Skipped, + Delayed, + Building, + Signing, + Unknown, + Queued, + } + + impl fmt::Display for PackageStatus { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!( + f, + "{}", + match self { + PackageStatus::Latest => "latest", + PackageStatus::Failed => "failed", + PackageStatus::Built => "build", + PackageStatus::Skipped => "skipped", + PackageStatus::Delayed => "delayed", + PackageStatus::Building => "building", + PackageStatus::Signing => "signing", + PackageStatus::Unknown => "unknown", + PackageStatus::Queued => "queued", + } + ) + } + } + + #[derive(Debug, Clone, Serialize, Deserialize)] + pub struct Package { + pkgbase: String, + repo: String, + split_packages: Vec, + status: PackageStatus, + skip_reason: Option, + lto: String, + debug_symbols: String, + arch_version: String, + repo_version: String, + build_date: Option, + peak_mem: Option, + } + + #[derive(Debug, Clone, Serialize, Deserialize)] + pub struct PackageResponse { + pub total: usize, + pub offset: usize, + pub limit: usize, + pub packages: Vec, + } + #[derive(Debug, Serialize, Default)] + pub struct PackageRequest { + pub status: Vec, + pub pkgbase: Option, + pub exact: bool, + pub repo: Option, + pub offset: usize, + pub limit: usize, + } + + impl PackageRequest { + fn query_string(&self) -> String { + let mut params = Vec::new(); + for status in &self.status { + params.push(format!("status={}", status)); } - ) - } -} - -#[derive(Debug, Clone, Serialize, Deserialize)] -pub struct Package { - pkgbase: String, - repo: String, - split_packages: Vec, - status: PackageStatus, - skip_reason: Option, - lto: String, - debug_symbols: String, - arch_version: String, - repo_version: String, - build_date: Option, - peak_mem: Option, -} - -#[derive(Debug, Clone, Serialize, Deserialize)] -pub struct PackageResponse { - pub total: usize, - pub offset: usize, - pub limit: usize, - pub packages: Vec, -} -#[derive(Debug, Serialize, Default)] -pub struct PackageRequest { - pub status: Vec, - pub pkgbase: Option, - pub exact: bool, - pub repo: Option, - pub offset: usize, - pub limit: usize, -} - -impl PackageRequest { - fn query_string(&self) -> String { - let mut params = Vec::new(); - for status in &self.status { - params.push(format!("status={}", status)); + if let Some(pkgbase) = &self.pkgbase { + params.push(format!("pkgbase={}", pkgbase)); + } + if self.exact { + params.push("exact".to_string()); + } + if let Some(repo) = &self.repo { + params.push(format!("repo={}", repo)); + } + params.push(format!("offset={}", self.offset)); + params.push(format!("limit={}", self.limit)); + params.join("&") } - if let Some(pkgbase) = &self.pkgbase { - params.push(format!("pkgbase={}", pkgbase)); - } - if self.exact { - params.push("exact".to_string()); - } - if let Some(repo) = &self.repo { - params.push(format!("repo={}", repo)); - } - params.push(format!("offset={}", self.offset)); - params.push(format!("limit={}", self.limit)); - params.join("&") - } - pub fn response(&self) -> Result> { - let query_url = format!( - "{}{}{}", - API_BASE_URL, - API_PACKAGES_EXT, - self.query_string() - ); - println!("{}", query_url); - let client = Client::new(); - info!("Fetching URL: {}", query_url); - let response = client.get(query_url).send()?; - match response.status() { - StatusCode::OK => { - let response = response.text()?; - println!("{}", response); - match serde_json::from_str(&response) { - Ok(json) => Ok(json), - Err(e) => { - error!("Failed to deserialize JSON: {}", e); - error!("Response body: {}", response); - Err(Box::new(e)) + pub fn response(&self) -> Result> { + let query_url = format!( + "{}{}{}", + API_BASE_URL, + API_PACKAGES_EXT, + self.query_string() + ); + info!("Fetching URL: {}", query_url); + let response = reqwest::blocking::get(query_url)?; + match response.status() { + StatusCode::OK => { + let response = response.text()?; + match serde_json::from_str(&response) { + Ok(json) => Ok(json), + Err(e) => { + error!("Failed to deserialize JSON: {}", e); + error!("Response body: {}", response); + Err(Box::new(e)) + } } } - } - StatusCode::NOT_FOUND => { - info!("No packages found"); - Ok(PackageResponse{ - total: 0, - offset: 0, - limit: 0, - packages: vec![], - }) - } - StatusCode::INTERNAL_SERVER_ERROR => { - panic!("Internal Server Error"); - } - _ => { - let query_url = format!( - "{}{}{}", - API_BASE_URL, - API_PACKAGES_EXT, - self.query_string() - ); - panic!("Unexpected server response: {:?} for query url: {}", response, query_url); + StatusCode::NOT_FOUND => { + info!("No packages found"); + Ok(PackageResponse { + total: 0, + offset: 0, + limit: 0, + packages: vec![], + }) + } + StatusCode::INTERNAL_SERVER_ERROR => { + panic!("Internal Server Error"); + } + _ => { + let query_url = format!( + "{}{}{}", + API_BASE_URL, + API_PACKAGES_EXT, + self.query_string() + ); + panic!( + "Unexpected server response: {:?} for query url: {}", + response, query_url + ); + } + } + } + } +} + +pub mod general { + use crate::{API_BASE_URL, API_GENERAL_EXT}; + use log::error; + use reqwest::StatusCode; + use serde::{Deserialize, Serialize}; + use std::error::Error; + + #[derive(Debug, Clone, Serialize, Deserialize)] + pub struct LtoStats { + pub enabled: usize, + pub disabled: usize, + pub unknown: usize, + } + + #[derive(Debug, Clone, Serialize, Deserialize)] + pub struct GeneralResponse { + pub failed: usize, + pub skipped: usize, + pub latest: usize, + pub queued: usize, + pub lto: LtoStats, + } + + #[derive(Debug, Clone, Serialize, Deserialize)] + pub struct GeneralRequest { + //No params yet. + } + impl GeneralRequest { + pub fn response(&self) -> Result> { + let query_url = format!("{}{}", API_BASE_URL, API_GENERAL_EXT); + let response = reqwest::blocking::get(query_url)?; + match response.status() { + StatusCode::OK => { + let response = response.text()?; + match serde_json::from_str(&response) { + Ok(json) => Ok(json), + Err(e) => { + error!("Failed to deserialize JSON: {}", e); + error!("Response body: {}", response); + Err(Box::new(e)) + } + } + } + StatusCode::INTERNAL_SERVER_ERROR => { + panic!("Internal Server Error"); + } + _ => { + let query_url = format!("{}{}", API_BASE_URL, API_GENERAL_EXT,); + panic!( + "Unexpected server response: {:?} for query url: {}", + response, query_url + ); + } } } } diff --git a/src/main.rs b/src/main.rs index 3231c4b..c9619d3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,14 @@ use alhp_api; -use alpm::{Alpm}; -use alhp_api::{PackageRequest, PackageStatus}; +use alhp_api::general::GeneralRequest; +use alhp_api::packages::{PackageRequest, PackageStatus}; +use alpm::Alpm; fn query_installed_packages() { - let alpm = match Alpm::new("/", "/var/lib/pacman"){ + let alpm = match Alpm::new("/", "/var/lib/pacman") { Ok(alpm) => alpm, Err(_) => panic!("Error establishing ALPM handle."), }; - let local_db= alpm.localdb(); + let local_db = alpm.localdb(); let server = local_db.servers(); for s in server { println!("{}", s); @@ -15,15 +16,19 @@ fn query_installed_packages() { } fn main() { - let pkg = PackageRequest{ + let pkg = PackageRequest { status: vec![PackageStatus::Building, PackageStatus::Queued], pkgbase: None, - exact: true, - repo: Some("extra-x86-64-v3".to_string()), + exact: false, + // repo: Some("extra-x86-64-v3".to_string()), + repo: None, offset: 0, limit: 0, }; let pkg = pkg.response().unwrap(); println!("{:#?}", pkg); + let stats = GeneralRequest {}; + let stats = stats.response().unwrap(); + println!("{:#?}", stats); query_installed_packages(); }