1 pub mod cli;
2 pub mod handler;
3
4 use anyhow::{anyhow, Result};
5 use log::debug;
6 use reqwest::Client;
7 use smbpndk_model::{AuthApp, AuthAppCreate};
8 use smbpndk_networking::{constants::BASE_URL, get_token};
9
10 async fn get_auth_apps() -> Result<Vec<AuthApp>> {
11 // Get current token
12 let token = get_token().await?;
13
14 let response = Client::new()
15 .get([BASE_URL, "v1/auth_apps"].join(""))
16 .header("Authorization", token)
17 .send()
18 .await?;
19
20 match response.status() {
21 reqwest::StatusCode::OK => {
22 let auth_apps: Vec<AuthApp> = response.json().await?;
23 Ok(auth_apps)
24 }
25 _ => Err(anyhow!("Failed to fetch auth apps.")),
26 }
27 }
28
29 async fn get_auth_app(id: &str) -> Result<AuthApp> {
30 // Get current token
31 let token = get_token().await?;
32
33 let response = Client::new()
34 .get([BASE_URL, "v1/auth_apps/", id].join(""))
35 .header("Authorization", token)
36 .send()
37 .await?;
38
39 match response.status() {
40 reqwest::StatusCode::OK => {
41 let auth_app: AuthApp = response.json().await?;
42 println!("Auth app requested: {auth_app:#?}");
43 Ok(auth_app)
44 }
45 _ => Err(anyhow!(format!(
46 "Failed to find an auth app with id: {id}."
47 ))),
48 }
49 }
50
51 async fn delete_auth_app(id: String) -> Result<()> {
52 // Get current token
53 let token = get_token().await?;
54
55 let response = Client::new()
56 .delete([BASE_URL, "v1/auth_apps/", &id].join(""))
57 .header("Authorization", token)
58 .send()
59 .await?;
60
61 match response.status() {
62 reqwest::StatusCode::OK => {
63 debug!("Project deleted.");
64 Ok(())
65 }
66 _ => Err(anyhow!("Failed to delete an auth app.")),
67 }
68 }
69
70 async fn create_auth_app(auth_app: AuthAppCreate) -> Result<AuthApp> {
71 // Get current token
72 let token = get_token().await?;
73
74 let response = Client::new()
75 .post([BASE_URL, "v1/auth_apps"].join(""))
76 .json(&auth_app)
77 .header("Authorization", token)
78 .send()
79 .await?;
80
81 match response.status() {
82 reqwest::StatusCode::CREATED => {
83 let auth_app: AuthApp = response.json().await?;
84 println!("Project created: {auth_app:#?}");
85 Ok(auth_app)
86 }
87 _ => Err(anyhow!("Failed to create an auth app.")),
88 }
89 }