alhp_api general should be working
This commit is contained in:
@@ -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_BASE_URL: &str = "https://api.alhp.dev";
|
||||||
const API_PACKAGES_EXT: &str = "/packages?";
|
const API_PACKAGES_EXT: &str = "/packages?";
|
||||||
const API_GENERAL_EXT: &str = "/stats?";
|
const API_GENERAL_EXT: &str = "/stats?";
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Eq, PartialEq)]
|
pub mod packages {
|
||||||
#[serde(rename_all = "lowercase")]
|
use crate::{API_BASE_URL, API_PACKAGES_EXT};
|
||||||
pub enum PackageStatus {
|
use log::{error, info};
|
||||||
Latest,
|
use reqwest::StatusCode;
|
||||||
Failed,
|
use serde::{Deserialize, Serialize};
|
||||||
Built,
|
use std::error::Error;
|
||||||
Skipped,
|
use std::fmt;
|
||||||
Delayed,
|
|
||||||
Building,
|
|
||||||
Signing,
|
|
||||||
Unknown,
|
|
||||||
Queued,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl fmt::Display for PackageStatus {
|
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Eq, PartialEq)]
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
#[serde(rename_all = "lowercase")]
|
||||||
write!(
|
pub enum PackageStatus {
|
||||||
f,
|
Latest,
|
||||||
"{}",
|
Failed,
|
||||||
match self {
|
Built,
|
||||||
PackageStatus::Latest => "latest",
|
Skipped,
|
||||||
PackageStatus::Failed => "failed",
|
Delayed,
|
||||||
PackageStatus::Built => "build",
|
Building,
|
||||||
PackageStatus::Skipped => "skipped",
|
Signing,
|
||||||
PackageStatus::Delayed => "delayed",
|
Unknown,
|
||||||
PackageStatus::Building => "building",
|
Queued,
|
||||||
PackageStatus::Signing => "signing",
|
}
|
||||||
PackageStatus::Unknown => "unknown",
|
|
||||||
PackageStatus::Queued => "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<String>,
|
||||||
|
status: PackageStatus,
|
||||||
|
skip_reason: Option<String>,
|
||||||
|
lto: String,
|
||||||
|
debug_symbols: String,
|
||||||
|
arch_version: String,
|
||||||
|
repo_version: String,
|
||||||
|
build_date: Option<String>,
|
||||||
|
peak_mem: Option<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 {
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
params.push("exact".to_string());
|
||||||
pub struct Package {
|
}
|
||||||
pkgbase: String,
|
if let Some(repo) = &self.repo {
|
||||||
repo: String,
|
params.push(format!("repo={}", repo));
|
||||||
split_packages: Vec<String>,
|
}
|
||||||
status: PackageStatus,
|
params.push(format!("offset={}", self.offset));
|
||||||
skip_reason: Option<String>,
|
params.push(format!("limit={}", self.limit));
|
||||||
lto: String,
|
params.join("&")
|
||||||
debug_symbols: String,
|
|
||||||
arch_version: String,
|
|
||||||
repo_version: String,
|
|
||||||
build_date: Option<String>,
|
|
||||||
peak_mem: Option<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 {
|
pub fn response(&self) -> Result<PackageResponse, Box<dyn Error>> {
|
||||||
params.push(format!("pkgbase={}", pkgbase));
|
let query_url = format!(
|
||||||
}
|
"{}{}{}",
|
||||||
if self.exact {
|
API_BASE_URL,
|
||||||
params.push("exact".to_string());
|
API_PACKAGES_EXT,
|
||||||
}
|
self.query_string()
|
||||||
if let Some(repo) = &self.repo {
|
);
|
||||||
params.push(format!("repo={}", repo));
|
info!("Fetching URL: {}", query_url);
|
||||||
}
|
let response = reqwest::blocking::get(query_url)?;
|
||||||
params.push(format!("offset={}", self.offset));
|
match response.status() {
|
||||||
params.push(format!("limit={}", self.limit));
|
StatusCode::OK => {
|
||||||
params.join("&")
|
let response = response.text()?;
|
||||||
}
|
match serde_json::from_str(&response) {
|
||||||
pub fn response(&self) -> Result<PackageResponse, Box<dyn Error>> {
|
Ok(json) => Ok(json),
|
||||||
let query_url = format!(
|
Err(e) => {
|
||||||
"{}{}{}",
|
error!("Failed to deserialize JSON: {}", e);
|
||||||
API_BASE_URL,
|
error!("Response body: {}", response);
|
||||||
API_PACKAGES_EXT,
|
Err(Box::new(e))
|
||||||
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))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
StatusCode::NOT_FOUND => {
|
||||||
StatusCode::NOT_FOUND => {
|
info!("No packages found");
|
||||||
info!("No packages found");
|
Ok(PackageResponse {
|
||||||
Ok(PackageResponse{
|
total: 0,
|
||||||
total: 0,
|
offset: 0,
|
||||||
offset: 0,
|
limit: 0,
|
||||||
limit: 0,
|
packages: vec![],
|
||||||
packages: vec![],
|
})
|
||||||
})
|
}
|
||||||
}
|
StatusCode::INTERNAL_SERVER_ERROR => {
|
||||||
StatusCode::INTERNAL_SERVER_ERROR => {
|
panic!("Internal Server Error");
|
||||||
panic!("Internal Server Error");
|
}
|
||||||
}
|
_ => {
|
||||||
_ => {
|
let query_url = format!(
|
||||||
let query_url = format!(
|
"{}{}{}",
|
||||||
"{}{}{}",
|
API_BASE_URL,
|
||||||
API_BASE_URL,
|
API_PACKAGES_EXT,
|
||||||
API_PACKAGES_EXT,
|
self.query_string()
|
||||||
self.query_string()
|
);
|
||||||
);
|
panic!(
|
||||||
panic!("Unexpected server response: {:?} for query url: {}", response, query_url);
|
"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<GeneralResponse, Box<dyn Error>> {
|
||||||
|
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
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
19
src/main.rs
19
src/main.rs
@@ -1,13 +1,14 @@
|
|||||||
use alhp_api;
|
use alhp_api;
|
||||||
use alpm::{Alpm};
|
use alhp_api::general::GeneralRequest;
|
||||||
use alhp_api::{PackageRequest, PackageStatus};
|
use alhp_api::packages::{PackageRequest, PackageStatus};
|
||||||
|
use alpm::Alpm;
|
||||||
|
|
||||||
fn query_installed_packages() {
|
fn query_installed_packages() {
|
||||||
let alpm = match Alpm::new("/", "/var/lib/pacman"){
|
let alpm = match Alpm::new("/", "/var/lib/pacman") {
|
||||||
Ok(alpm) => alpm,
|
Ok(alpm) => alpm,
|
||||||
Err(_) => panic!("Error establishing ALPM handle."),
|
Err(_) => panic!("Error establishing ALPM handle."),
|
||||||
};
|
};
|
||||||
let local_db= alpm.localdb();
|
let local_db = alpm.localdb();
|
||||||
let server = local_db.servers();
|
let server = local_db.servers();
|
||||||
for s in server {
|
for s in server {
|
||||||
println!("{}", s);
|
println!("{}", s);
|
||||||
@@ -15,15 +16,19 @@ fn query_installed_packages() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let pkg = PackageRequest{
|
let pkg = PackageRequest {
|
||||||
status: vec![PackageStatus::Building, PackageStatus::Queued],
|
status: vec![PackageStatus::Building, PackageStatus::Queued],
|
||||||
pkgbase: None,
|
pkgbase: None,
|
||||||
exact: true,
|
exact: false,
|
||||||
repo: Some("extra-x86-64-v3".to_string()),
|
// repo: Some("extra-x86-64-v3".to_string()),
|
||||||
|
repo: None,
|
||||||
offset: 0,
|
offset: 0,
|
||||||
limit: 0,
|
limit: 0,
|
||||||
};
|
};
|
||||||
let pkg = pkg.response().unwrap();
|
let pkg = pkg.response().unwrap();
|
||||||
println!("{:#?}", pkg);
|
println!("{:#?}", pkg);
|
||||||
|
let stats = GeneralRequest {};
|
||||||
|
let stats = stats.response().unwrap();
|
||||||
|
println!("{:#?}", stats);
|
||||||
query_installed_packages();
|
query_installed_packages();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user