Skip to main content

pocketbase_sdk/
client.rs

1use crate::{collections::CollectionsManager, httpc::Httpc};
2use crate::{logs::LogsManager, records::RecordsManager};
3use anyhow::{anyhow, Result};
4use serde::Deserialize;
5use serde_json::json;
6
7#[derive(Debug, Deserialize)]
8struct AuthSuccessResponse {
9    token: String,
10}
11
12#[derive(Debug, Clone)]
13pub struct NoAuth;
14
15#[derive(Debug, Clone)]
16pub struct Auth;
17
18#[derive(Debug, Clone)]
19pub struct Client<State = NoAuth> {
20    pub base_url: String,
21    pub auth_token: Option<String>,
22    pub state: State,
23}
24
25#[derive(Debug, Clone, Deserialize)]
26pub struct HealthCheckResponse {
27    pub code: i32,
28    pub message: String,
29}
30
31impl Client<Auth> {
32    pub fn collections(&self) -> CollectionsManager {
33        CollectionsManager { client: self }
34    }
35
36    pub fn health_check(&self) -> Result<HealthCheckResponse> {
37        let url = format!("{}/api/health", self.base_url);
38        match Httpc::get(self, &url, None) {
39            Ok(response) => Ok(response.into_json::<HealthCheckResponse>()?),
40            Err(e) => Err(anyhow!("{}", e))
41        }
42    }
43
44    pub fn logs(&self) -> LogsManager {
45        LogsManager { client: self }
46    }
47
48    pub fn records(&self, record_name: &'static str) -> RecordsManager {
49        RecordsManager {
50            client: self,
51            name: record_name,
52        }
53    }
54}
55
56impl Client<NoAuth> {
57    pub fn new(base_url: &str) -> Self {
58        Self {
59            base_url: base_url.to_string(),
60            auth_token: None,
61            state: NoAuth,
62        }
63    }
64
65    pub fn health_check(&self) -> Result<HealthCheckResponse> {
66        let url = format!("{}/api/health", self.base_url);
67        match Httpc::get(self, &url, None) {
68            Ok(response) => Ok(response.into_json::<HealthCheckResponse>()?),
69            Err(e) => Err(anyhow!("{}", e))
70        }
71    }
72
73    pub fn auth_with_password(&self, collection: &str, identifier: &str, secret: &str) -> Result<Client<Auth>> {
74        let url = format!(
75            "{}/api/collections/{}/auth-with-password",
76            self.base_url, collection
77        );
78
79        let auth_payload = json!({
80            "identity": identifier,
81            "password": secret
82        });
83
84        match Httpc::post(self, &url, auth_payload.to_string()) {
85            Ok(response) => {
86                let raw_response = response.into_json::<AuthSuccessResponse>();
87                match raw_response {
88                    Ok(AuthSuccessResponse { token }) => Ok(Client {
89                        base_url: self.base_url.clone(),
90                        state: Auth,
91                        auth_token: Some(token),
92                    }),
93                    Err(e) => Err(anyhow!("{}", e)),
94                }
95            }
96            Err(e) => Err(anyhow!("{}", e)),
97        }
98    }
99}