proxctl_bindings/
lib.rs

1// Proxmox API Bindings Library
2// <spiperac@denkei.org>
3//
4
5pub mod api;
6pub mod resources;
7
8use crate::api::http_client::Client;
9use serde::{Deserialize, Serialize};
10
11#[derive(Deserialize)]
12pub struct ProxmoxResponse<T> {
13    pub data: T,
14}
15
16#[derive(Debug, Serialize, Deserialize, Clone)]
17pub struct AuthResponse {
18    pub ticket: String,
19    #[serde(rename = "CSRFPreventionToken")]
20    pub csrf_prevention_token: String,
21    pub username: String,
22}
23
24#[derive(Debug, Serialize, Deserialize)]
25pub struct VersionInfo {
26    pub version: String,
27    pub release: String,
28}
29
30pub struct ProxmoxApi {
31    pub client: Client,
32}
33
34impl ProxmoxApi {
35    pub fn new(base_url: String) -> Self {
36        Self {
37            client: Client::new(base_url),
38        }
39    }
40
41    // Basic
42    pub async fn authenticate(
43        &mut self,
44        username: &str,
45        password: &str,
46        realm: &str,
47    ) -> Result<ProxmoxResponse<AuthResponse>, reqwest::Error> {
48        let auth_data = serde_json::json!({
49            "username": username,
50            "password": password,
51            "realm": realm,
52        });
53
54        let response = self
55            .client
56            .post("/api2/json/access/ticket", &auth_data)
57            .await?;
58
59        let auth_response: ProxmoxResponse<AuthResponse> = response.json().await?;
60
61        // Store the authentication token
62        self.client.set_tokens(
63            auth_response.data.ticket.clone(),
64            Some(auth_response.data.csrf_prevention_token.clone()),
65        );
66
67        Ok(auth_response)
68    }
69
70    pub async fn get_version(&self) -> Result<ProxmoxResponse<VersionInfo>, reqwest::Error> {
71        let response = self.client.get("/api2/json/version").await?;
72        let data: ProxmoxResponse<VersionInfo> = response.json().await?;
73        Ok(data)
74    }
75}