1use anyhow::{bail, Context, Result};
2use serde::{Deserialize, Serialize};
3
4pub struct Workspaces(pub String, pub String);
5
6#[derive(Deserialize, Debug, Serialize)]
7pub struct Workspace {
8 pub workspace_id: String,
9 pub name: String,
10 pub connect_partner_name: String,
11 pub is_sandbox: bool,
12}
13
14impl Workspaces {
15 pub fn get(self) -> Result<Workspace> {
16 let url = format!("{}/workspaces/get", self.1);
17 let header = format!("Bearer {}", self.0);
18
19 let req = reqwest::blocking::Client::new()
20 .get(url)
21 .header("Authorization", header)
22 .send()
23 .context("Failed to send get request")?;
24
25 if req.status() == reqwest::StatusCode::NOT_FOUND {
26 bail!("workspace not found");
27 } else if req.status() != reqwest::StatusCode::OK {
28 bail!("{}", req.text().context("Really bad API failure")?);
29 }
30
31 let json: crate::Response = req.json().context("Failed to deserialize JSON")?;
32 Ok(json.workspace.unwrap())
33 }
34
35 pub fn reset_sandbox(self) -> Result<()> {
36 let url = format!("{}/workspaces/reset_sandbox", self.1);
37 let header = format!("Bearer {}", self.0);
38 let req = reqwest::blocking::Client::new()
39 .post(url)
40 .header("Authorization", header)
41 .send()
42 .context("Failed to send request")?;
43
44 if req.status() != reqwest::StatusCode::OK {
45 bail!("Reset failed");
46 }
47 Ok(())
48 }
49
50 pub fn list(self, workspace: Option<String>) -> Result<Vec<Workspace>> {
52 let workspace_id = match workspace {
53 Some(a) => a,
54 None => "None".to_string(),
55 };
56
57 let url = format!("{}/workspaces/list?workspace_id={:?}", self.1, workspace_id);
58 let header = format!("Bearer {}", self.0);
59
60 let req = reqwest::blocking::Client::new()
61 .get(url)
62 .header("Authorization", header)
63 .send()
64 .context("Failed to send request")?;
65
66 if req.status() == reqwest::StatusCode::NOT_FOUND {
67 bail!("workspaces not found");
68 } else if req.status() != reqwest::StatusCode::OK {
69 bail!("request failed");
70 }
71
72 let json: crate::Response = req.json().context("Failed to deserialize JSON")?;
73 Ok(json.workspaces.unwrap())
74 }
75}