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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
//! List the STT and TTS models available to you or to a project.
//!
//! See the [Deepgram Model Metadata guide][docs] for more info.
//!
//! [docs]: https://developers.deepgram.com/guides/fundamentals/model-metadata
use reqwest::RequestBuilder;
use url::Url;
use crate::{send_and_translate_response, Deepgram};
use response::{Model, ModelsResponse};
pub mod response;
/// List the models available to you or to a project.
///
/// Constructed using [`Deepgram::models`].
///
/// See the [Deepgram Model Metadata guide][docs] for more info.
///
/// [docs]: https://developers.deepgram.com/guides/fundamentals/model-metadata
#[derive(Debug, Clone)]
pub struct Models<'a>(&'a Deepgram);
impl Deepgram {
/// Construct a new [`Models`] from a [`Deepgram`].
pub fn models(&self) -> Models<'_> {
self.into()
}
}
impl<'a> From<&'a Deepgram> for Models<'a> {
/// Construct a new [`Models`] from a [`Deepgram`].
fn from(deepgram: &'a Deepgram) -> Self {
Self(deepgram)
}
}
impl Models<'_> {
/// List metadata on all the latest public models.
///
/// To also include non-latest (outdated) versions, use
/// [`Models::get_models_including_outdated`].
///
/// See the [Deepgram API Reference][api] for more info.
///
/// [api]: https://developers.deepgram.com/reference/manage/models/list
pub async fn get_models(&self) -> crate::Result<ModelsResponse> {
send_and_translate_response(self.get_models_request(false)).await
}
/// List metadata on all public models, including non-latest (outdated)
/// versions.
///
/// Sends `include_outdated=true`. For the latest versions only, use
/// [`Models::get_models`].
///
/// See the [Deepgram API Reference][api] for more info.
///
/// [api]: https://developers.deepgram.com/reference/manage/models/list
pub async fn get_models_including_outdated(&self) -> crate::Result<ModelsResponse> {
send_and_translate_response(self.get_models_request(true)).await
}
/// Get metadata on a specific public model by its UUID.
///
/// See the [Deepgram API Reference][api] for more info.
///
/// [api]: https://developers.deepgram.com/reference/manage/models/get
pub async fn get_model(&self, model_id: &str) -> crate::Result<Model> {
send_and_translate_response(self.get_model_request(model_id)).await
}
/// List metadata on all the latest models a project has access to,
/// including non-public (custom) models.
///
/// To also include non-latest (outdated) versions, use
/// [`Models::get_project_models_including_outdated`].
///
/// See the [Deepgram API Reference][api] for more info.
///
/// [api]: https://developers.deepgram.com/reference/manage/projects/models/list
pub async fn get_project_models(&self, project_id: &str) -> crate::Result<ModelsResponse> {
send_and_translate_response(self.get_project_models_request(project_id, false)).await
}
/// List metadata on all the models a project has access to, including
/// non-public (custom) models and non-latest (outdated) versions.
///
/// Sends `include_outdated=true`. For the latest versions only, use
/// [`Models::get_project_models`].
///
/// See the [Deepgram API Reference][api] for more info.
///
/// [api]: https://developers.deepgram.com/reference/manage/projects/models/list
pub async fn get_project_models_including_outdated(
&self,
project_id: &str,
) -> crate::Result<ModelsResponse> {
send_and_translate_response(self.get_project_models_request(project_id, true)).await
}
/// Get metadata for a specific model a project has access to.
///
/// See the [Deepgram API Reference][api] for more info.
///
/// [api]: https://developers.deepgram.com/reference/manage/projects/models/get
pub async fn get_project_model(
&self,
project_id: &str,
model_id: &str,
) -> crate::Result<Model> {
send_and_translate_response(self.get_project_model_request(project_id, model_id)).await
}
/// `include_outdated` is optional on the endpoint and defaults to the
/// latest versions only, so it is sent only when it is `true`.
fn get_models_request(&self, include_outdated: bool) -> RequestBuilder {
let request = self.0.client.get(self.models_url());
if include_outdated {
request.query(&[("include_outdated", "true")])
} else {
request
}
}
fn get_model_request(&self, model_id: &str) -> RequestBuilder {
self.0.client.get(self.model_url(model_id))
}
/// `include_outdated` is sent only when `true`, as in
/// `get_models_request`.
fn get_project_models_request(
&self,
project_id: &str,
include_outdated: bool,
) -> RequestBuilder {
let request = self.0.client.get(self.project_models_url(project_id));
if include_outdated {
request.query(&[("include_outdated", "true")])
} else {
request
}
}
fn get_project_model_request(&self, project_id: &str, model_id: &str) -> RequestBuilder {
self.0
.client
.get(self.project_model_url(project_id, model_id))
}
fn models_url(&self) -> Url {
self.join("v1/models")
}
fn model_url(&self, model_id: &str) -> Url {
self.join(&format!("v1/models/{model_id}"))
}
fn project_models_url(&self, project_id: &str) -> Url {
self.join(&format!("v1/projects/{project_id}/models"))
}
fn project_model_url(&self, project_id: &str, model_id: &str) -> Url {
self.join(&format!("v1/projects/{project_id}/models/{model_id}"))
}
fn join(&self, path: &str) -> Url {
self.0
.base_url
.join(path)
.expect("base_url is checked to be a valid base_url when constructing Deepgram client")
}
}
#[cfg(test)]
mod tests {
use crate::Deepgram;
/// Pin each of the four model request URLs. A path typo would otherwise
/// only surface against the live API.
#[test]
fn request_urls() {
let dg = Deepgram::new("token").unwrap();
let models = dg.models();
let url = |request: reqwest::RequestBuilder| request.build().unwrap().url().to_string();
// `include_outdated` is optional on both listing endpoints and the
// endpoint default is the latest versions only, so `get_models` and
// `get_project_models` omit the parameter entirely and only the
// `_including_outdated` variants send it.
assert_eq!(
url(models.get_models_request(false)),
"https://api.deepgram.com/v1/models"
);
assert_eq!(
url(models.get_models_request(true)),
"https://api.deepgram.com/v1/models?include_outdated=true"
);
assert_eq!(
url(models.get_model_request("4899aa60-f731-4d2b-b1fd-fa5d6a2dc98b")),
"https://api.deepgram.com/v1/models/4899aa60-f731-4d2b-b1fd-fa5d6a2dc98b"
);
assert_eq!(
url(models.get_project_models_request("proj-1", false)),
"https://api.deepgram.com/v1/projects/proj-1/models"
);
assert_eq!(
url(models.get_project_models_request("proj-1", true)),
"https://api.deepgram.com/v1/projects/proj-1/models?include_outdated=true"
);
assert_eq!(
url(models.get_project_model_request("proj-1", "model-2")),
"https://api.deepgram.com/v1/projects/proj-1/models/model-2"
);
}
#[test]
fn request_methods_are_get() {
let dg = Deepgram::new("token").unwrap();
let models = dg.models();
for request in [
models.get_models_request(false),
models.get_model_request("model-1"),
models.get_project_models_request("proj-1", false),
models.get_project_model_request("proj-1", "model-1"),
] {
assert_eq!(request.build().unwrap().method(), reqwest::Method::GET);
}
}
/// A custom base URL must reach the management endpoints too: before this,
/// every `manage` module hardcoded `https://api.deepgram.com` and silently
/// ignored `with_base_url`.
#[test]
fn request_urls_honor_a_custom_base_url() {
let dg =
Deepgram::with_base_url_and_api_key("http://localhost:8888/abc/", "token").unwrap();
let models = dg.models();
let url = |request: reqwest::RequestBuilder| request.build().unwrap().url().to_string();
assert_eq!(
url(models.get_models_request(false)),
"http://localhost:8888/abc/v1/models"
);
assert_eq!(
url(models.get_model_request("model-2")),
"http://localhost:8888/abc/v1/models/model-2"
);
assert_eq!(
url(models.get_project_models_request("proj-1", true)),
"http://localhost:8888/abc/v1/projects/proj-1/models?include_outdated=true"
);
assert_eq!(
url(models.get_project_model_request("proj-1", "model-2")),
"http://localhost:8888/abc/v1/projects/proj-1/models/model-2"
);
}
/// A base URL that carries a path prefix has to end in `/` for the prefix
/// to survive: `Url::join` treats a base whose path does not end in `/` as
/// naming a file and replaces its last segment, so `.../abc` drops `abc`.
/// That is standard `Url::join` behavior and is how every surface in this
/// crate builds its URLs, not something specific to `models`. A bare host
/// (`http://localhost:8888`) is unaffected, because its path is already
/// `/`.
#[test]
fn a_base_url_path_prefix_needs_a_trailing_slash() {
let url = |request: reqwest::RequestBuilder| request.build().unwrap().url().to_string();
let no_prefix =
Deepgram::with_base_url_and_api_key("http://localhost:8888", "token").unwrap();
assert_eq!(
url(no_prefix.models().get_models_request(false)),
"http://localhost:8888/v1/models"
);
let unterminated_prefix =
Deepgram::with_base_url_and_api_key("http://localhost:8888/abc", "token").unwrap();
assert_eq!(
url(unterminated_prefix.models().get_models_request(false)),
"http://localhost:8888/v1/models"
);
}
}