initial version
This commit is contained in:
83
crates/alhp_api/src/lib.rs
Normal file
83
crates/alhp_api/src/lib.rs
Normal file
@@ -0,0 +1,83 @@
|
||||
use std::fmt;
|
||||
use serde::{Serialize, Deserialize};
|
||||
use reqwest::blocking::Client;
|
||||
|
||||
#[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, "{}", serde_json::to_string(self).unwrap().trim_matches('"'))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct Package {
|
||||
pkgbase: String,
|
||||
repo: String,
|
||||
split_packages: Vec<String>,
|
||||
status: PackageStatus,
|
||||
lto: String,
|
||||
debug_symbols: String,
|
||||
arch_version: String,
|
||||
repo_version: String,
|
||||
build_date: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct PackageResponse {
|
||||
pub total: usize,
|
||||
pub offset: usize,
|
||||
pub limit: usize,
|
||||
pub packages: Vec<Package>
|
||||
}
|
||||
#[derive(Debug, Serialize, Default)]
|
||||
pub struct PackageRequest {
|
||||
pub status: Vec<PackageStatus>,
|
||||
pub pkgbase: Option<String>,
|
||||
pub exact: bool,
|
||||
pub repo: Option<String>,
|
||||
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("&")
|
||||
}
|
||||
pub fn response(&self) -> Result<PackageResponse, reqwest::Error> {
|
||||
let client = Client::new();
|
||||
let query_string = self.query_string();
|
||||
let url = format!("https://api.alhp.dev/packages?{}", query_string);
|
||||
println!("{}", url);
|
||||
let response = client.get(url).send()?.text()?;
|
||||
let response: PackageResponse = serde_json::from_str(&response).unwrap();
|
||||
Ok(response)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user