seven_client/
subaccounts.rs

1use crate::{client::Client};
2use ureq::{Error};
3use serde::{Deserialize};
4
5const ENDPOINT: &str = "subaccounts";
6
7#[derive(Deserialize)]
8pub struct AutoTopUp {
9    pub amount: Option<f32>,
10    pub threshold: Option<f32>,
11}
12
13#[derive(Deserialize)]
14pub struct Contact {
15    pub name: String,
16    pub email: String,
17}
18
19#[derive(Deserialize)]
20pub struct Subaccount {
21    pub auto_topup: AutoTopUp,
22    pub balance: f32,
23    pub company: Option<String>,
24    pub contact: Contact,
25    pub id: u32,
26    pub total_usage: f32,
27    pub username: Option<String>,
28}
29
30#[derive(Deserialize)]
31pub struct CreateResponse {
32    pub error: Option<String>,
33    pub subaccount: Option<Subaccount>,
34    pub success: bool,
35}
36
37#[derive(Deserialize)]
38pub struct TransferCreditsResponse {
39    pub error: Option<String>,
40    pub success: bool,
41}
42
43#[derive(Deserialize)]
44pub struct AutoChargeResponse {
45    pub error: Option<String>,
46    pub success: bool,
47}
48
49#[derive(Deserialize)]
50pub struct DeleteResponse {
51    pub error: Option<String>,
52    pub success: bool,
53}
54
55#[derive(Deserialize)]
56pub struct CreateParams {
57    pub email: String,
58    pub name: String,
59}
60
61pub struct DeleteParams {
62    pub id: u32,
63}
64
65pub struct TransferCreditsParams {
66    pub id: u32,
67    pub amount: f32,
68}
69
70pub struct AutoChargeParams {
71    pub id: u32,
72    pub amount: f32,
73    pub threshold: f32,
74}
75
76pub struct Subaccounts {
77    client: Client,
78}
79
80impl Subaccounts {
81    pub fn new(client: Client) -> Self {
82        Subaccounts {
83            client,
84        }
85    }
86
87    pub fn read(&self) -> Result<Vec<Subaccount>, Error> {
88        let res = self.client.request("GET", ENDPOINT)
89            .clone()
90            .query("action", "read")
91            .call()?
92            .into_json::<Vec<Subaccount>>()?;
93        Ok(res)
94    }
95
96    pub fn create(&self, params: CreateParams) -> Result<CreateResponse, Error> {
97        let res = self.client.request("POST", ENDPOINT)
98            .send_form(&[
99                ("action", "create"),
100                ("email", &*params.email),
101                ("name", &*params.name),
102            ])?
103            .into_json::<CreateResponse>()?;
104        Ok(res)
105    }
106
107    pub fn transfer_credits(&self, params: TransferCreditsParams) -> Result<TransferCreditsResponse, Error> {
108        let res = self.client.request("POST", ENDPOINT)
109            .send_form(&[
110                ("action", "transfer_credits"),
111                ("amount", &params.amount.to_string()),
112                ("id", &*params.id.to_string()),
113            ])?
114            .into_json::<TransferCreditsResponse>()?;
115        Ok(res)
116    }
117
118    pub fn auto_charge(&self, params: AutoChargeParams) -> Result<AutoChargeResponse, Error> {
119        let res = self.client.request("POST", ENDPOINT)
120            .send_form(&[
121                ("action", "update"),
122                ("amount", &*params.amount.to_string()),
123                ("id", &*params.id.to_string()),
124                ("threshold", &*params.threshold.to_string()),
125            ])?
126            .into_json::<AutoChargeResponse>()?;
127        Ok(res)
128    }
129
130    pub fn delete(&self, params: DeleteParams) -> Result<DeleteResponse, Error> {
131        let res = self.client.request("POST", ENDPOINT)
132            .send_form(&[
133                ("action", "delete"),
134                ("id", &*params.id.to_string()),
135            ])?
136            .into_json::<DeleteResponse>()?;
137        Ok(res)
138    }
139}