1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
//! Get the outstanding balances for a Deepgram Project.
//!
//! See the [Deepgram API Reference][api] for more info.
//!
//! [api]: https://developers.deepgram.com/api-reference/#billing
use url::Url;
use crate::{
manage::billing::response::{Balance, Balances},
send_and_translate_response, Deepgram,
};
pub mod response;
/// Get the outstanding balances for a Deepgram Project.
///
/// Constructed using [`Deepgram::billing`].
///
/// See the [Deepgram API Reference][api] for more info.
///
/// [api]: https://developers.deepgram.com/api-reference/#billing
#[derive(Debug, Clone)]
pub struct Billing<'a>(&'a Deepgram);
impl Deepgram {
/// Construct a new [`Billing`] from a [`Deepgram`].
pub fn billing(&self) -> Billing<'_> {
self.into()
}
}
impl<'a> From<&'a Deepgram> for Billing<'a> {
/// Construct a new [`Billing`] from a [`Deepgram`].
fn from(deepgram: &'a Deepgram) -> Self {
Self(deepgram)
}
}
impl Billing<'_> {
/// Get the outstanding balances for the specified project.
///
/// See the [Deepgram API Reference][api] for more info.
///
/// [api]: https://developers.deepgram.com/api-reference/#billing-all
///
/// # Examples
///
/// ```no_run
/// # use deepgram::{Deepgram, DeepgramError};
/// # use std::env;
/// #
/// # #[tokio::main]
/// # async fn main() -> Result<(), DeepgramError> {
/// # let deepgram_api_key =
/// # env::var("DEEPGRAM_API_KEY").expect("DEEPGRAM_API_KEY environmental variable");
/// #
/// # let project_id =
/// # env::var("DEEPGRAM_PROJECT_ID").expect("DEEPGRAM_PROJECT_ID environmental variable");
/// #
/// let dg_client = Deepgram::new(&deepgram_api_key)?;
///
/// let balances = dg_client
/// .billing()
/// .list_balance(&project_id)
/// .await?;
/// #
/// # Ok(())
/// # }
/// ```
pub async fn list_balance(&self, project_id: &str) -> crate::Result<Balances> {
let url = self.balances_url(project_id)?;
send_and_translate_response(self.0.client.get(url)).await
}
/// Get the details of a specific balance.
///
/// See the [Deepgram API Reference][api] for more info.
///
/// [api]: https://developers.deepgram.com/api-reference/#billing-get
///
/// # Examples
///
/// ```no_run
/// # use deepgram::{Deepgram, DeepgramError};
/// # use std::env;
/// #
/// # #[tokio::main]
/// # async fn main() -> Result<(), DeepgramError> {
/// # let deepgram_api_key =
/// # env::var("DEEPGRAM_API_KEY").expect("DEEPGRAM_API_KEY environmental variable");
/// #
/// # let project_id =
/// # env::var("DEEPGRAM_PROJECT_ID").expect("DEEPGRAM_PROJECT_ID environmental variable");
/// #
/// # let balance_id =
/// # env::var("DEEPGRAM_BALANCE_ID").expect("DEEPGRAM_BALANCE_ID environmental variable");
/// #
/// let dg_client = Deepgram::new(&deepgram_api_key)?;
///
/// let balance = dg_client
/// .billing()
/// .get_balance(&project_id, &balance_id)
/// .await?;
///
/// assert_eq!(balance_id, balance.balance_id.to_string());
/// #
/// # Ok(())
/// # }
/// ```
pub async fn get_balance(&self, project_id: &str, balance_id: &str) -> crate::Result<Balance> {
let url = self.balance_url(project_id, balance_id)?;
send_and_translate_response(self.0.client.get(url)).await
}
fn balances_url(&self, project_id: &str) -> crate::Result<Url> {
self.0
.api_url(&format!("v1/projects/{project_id}/balances"))
}
fn balance_url(&self, project_id: &str, balance_id: &str) -> crate::Result<Url> {
self.0
.api_url(&format!("v1/projects/{project_id}/balances/{balance_id}"))
}
}
#[cfg(test)]
mod tests {
use crate::manage::billing::response::{Balance, BillingUnits};
use crate::Deepgram;
#[test]
fn urls_default_base() {
let dg = Deepgram::new("token").unwrap();
assert_eq!(
dg.billing().balances_url("proj").unwrap().as_str(),
"https://api.deepgram.com/v1/projects/proj/balances"
);
assert_eq!(
dg.billing().balance_url("proj", "bal").unwrap().as_str(),
"https://api.deepgram.com/v1/projects/proj/balances/bal"
);
}
#[test]
fn urls_custom_base() {
let dg = Deepgram::with_base_url("http://deepgram.internal").unwrap();
assert_eq!(
dg.billing().balances_url("proj").unwrap().as_str(),
"http://deepgram.internal/v1/projects/proj/balances"
);
assert_eq!(
dg.billing().balance_url("proj", "bal").unwrap().as_str(),
"http://deepgram.internal/v1/projects/proj/balances/bal"
);
}
#[test]
fn test() {
assert_eq!(
serde_json::from_str::<Balance>(
"{\"balance_id\":\"a1a2a3a4-b1b2-c1c2-d1d2-d3d4d5d6d7d8\",\"amount\":1,\"units\":\"usd\",\"purchase_order_id\":\"a1a2a3a4-b1b2-c1c2-d1d2-d3d4d5d6d7d8\"}",
).unwrap().units,
BillingUnits::Usd
);
}
}