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
use std::fmt;
use reqwest::header::{HeaderMap, HeaderValue};
use reqwest::{Response, Url};
use serde::de::DeserializeOwned;
use serde::Deserialize;
use uuid::Uuid;
use crate::meeting_token::MeetingToken;
use crate::recording::RecordingObject;
use crate::room::Room;
use crate::{Error, Result};
const BASE_URL: &str = "https://api.daily.co/v1/";
/// A `Client` to make `Daily` API requests with.
#[derive(Debug, Clone)]
pub struct Client {
pub(crate) client: reqwest::Client,
pub(crate) base_url: Url,
}
impl Client {
/// Creates a [Client](crate::Client) from an API key.
///
/// # Errors
///
/// If the given API key does not contain only ASCII characters, an
/// error variant will be returned.
///
/// # Examples
///
/// ```
/// # use dailyco::{Client, Result};
/// # fn main_fn() -> Result<Client> {
/// let client = Client::new("test-api-key")?;
/// Ok(client)
/// # }
/// ```
pub fn new<T: fmt::Display>(key: T) -> Result<Self> {
// We should be guaranteed this parsing will not fail
let base_url = Url::parse(BASE_URL).unwrap();
Self::with_endpoint(key, base_url)
}
/// Creates a [Client](crate::Client) with a custom endpoint. This is primarily
/// intended for testing purposes - for example pointing API requests to a [wiremock server](https://github.com/LukeMathWalker/wiremock-rs).
///
/// # Examples
///
/// ```
/// # use dailyco::{Client, Result};
/// # fn main_fn() -> Result<Client> {
/// let mock_server_addr = "http://localhost:8080";
/// let client = Client::with_endpoint(
/// "test-api-key",
/// reqwest::Url::parse(mock_server_addr).unwrap(),
/// )?;
/// Ok(client)
/// # }
/// ```
pub fn with_endpoint<T: fmt::Display>(key: T, endpoint: Url) -> Result<Self> {
let mut header_val = HeaderValue::try_from(format!("Bearer {}", key))
.map_err(|_| Error::BadAPIKey("API key must include only ASCII characters"))?;
header_val.set_sensitive(true);
let mut headers = HeaderMap::new();
headers.insert(reqwest::header::AUTHORIZATION, header_val);
let client = reqwest::Client::builder()
.default_headers(headers)
.build()?;
Ok(Self {
client,
base_url: endpoint,
})
}
}
impl Client {
/// Retrieve the `Daily` room corresponding to this name.
///
/// # Examples
///
/// ```no_run
/// # use dailyco::Client;
/// # use dailyco::Result;
/// # use dailyco::room::Room;
/// #
/// # async fn run() -> Result<Room> {
/// let client = Client::new("test-api-key")?;
/// let room = client.get_room("room-we-just-made").await?;
/// # Ok(room)
/// # }
/// ```
pub async fn get_room(&self, room_name: &str) -> Result<Room> {
let url = self.get_room_url_with_name(room_name);
let resp = self.client.get(url).send().await?;
parse_dailyco_response(resp).await
}
/// Validate and retrieve configuration information for the provided meeting token.
///
/// # Examples
///
/// ```no_run
/// # use dailyco::{Client, Result};
/// # use dailyco::room::Room;
/// # use dailyco::meeting_token::{CreateMeetingToken};
/// #
/// # async fn run() -> Result<()> {
/// let client = Client::new("test-api-key")?;
/// let token = CreateMeetingToken::new()
/// .room_name("room-which-exists")
/// .send(&client)
/// .await?;
/// let validated = client.get_meeting_token(&token).await?;
/// assert_eq!(validated.room_name, Some("room-which-exists".to_string()));
/// # Ok(())
/// # }
/// ```
pub async fn get_meeting_token(&self, token: &str) -> Result<MeetingToken> {
let url = self
.base_url
.join("meeting-tokens/")
.unwrap()
.join(token)
.unwrap();
let resp = self.client.get(url).send().await?;
parse_dailyco_response(resp).await
}
/// Retrieve all `Daily` rooms for the account.
///
/// Pagination is currently unimplemented, so queries returning
/// more than `100` rooms will return a `crate::Error::RequiresPagination`.
///
/// # Examples
///
/// ```no_run
/// # use dailyco::{Client, Result};
/// # use dailyco::room::Room;
/// # async fn run() -> Result<Vec<Room>> {
/// let client = Client::new("test-api-key")?;
/// let rooms = client.get_rooms().await?;
/// # Ok(rooms)
/// # }
/// ```
pub async fn get_rooms(&self) -> Result<Vec<Room>> {
#[derive(Debug, Deserialize)]
struct GetRoomsResponse {
total_count: usize,
data: Vec<Room>,
}
let url = self.base_url.join("rooms/").unwrap();
let resp = self.client.get(url).send().await?;
let data: GetRoomsResponse = parse_dailyco_response(resp).await?;
if data.total_count >= 100 {
Err(Error::RequiresPagination)
} else {
Ok(data.data)
}
}
/// Get information about a specific recording.
///
/// <https://docs.daily.co/reference/rest-api/recordings/get-recording-information>
pub async fn get_recording(&self, id: Uuid) -> Result<RecordingObject> {
let url = format!("{}/recordings/{id}", self.base_url);
let resp = self.client.get(url).send().await?;
let data: RecordingObject = parse_dailyco_response(resp).await?;
Ok(data)
}
/// Delete a specific recording
///
/// <https://docs.daily.co/reference/rest-api/recordings/delete-recording>
pub async fn delete_recording(&self, id: Uuid) -> Result<()> {
let url = format!("{}/recordings/{id}", self.base_url);
let resp = self.client.delete(url).send().await?;
if resp.status().is_success() {
Ok(())
} else {
Err(Error::from_failed_daily_request(resp).await)
}
}
/// Delete the `Daily` room with this name.
///
/// Will result in an error if the room does not exist.
///
/// # Examples
///
/// ```no_run
/// # use dailyco::{Client, Result};
/// # async fn run() -> Result<()> {
/// let client = Client::new("test-api-key")?;
/// client.delete_room("room-that-exists").await?;
/// # Ok(())
/// # }
/// ```
pub async fn delete_room(&self, room_name: &str) -> Result<()> {
let url = self.get_room_url_with_name(room_name);
let resp = self.client.delete(url).send().await?;
if resp.status().is_success() {
Ok(())
} else {
Err(Error::from_failed_daily_request(resp).await)
}
}
fn get_room_url_with_name(&self, room_name: &str) -> Url {
// Neither of these unwraps should be able to fail
self.base_url
.join("rooms/")
.unwrap()
.join(room_name)
.unwrap()
}
}
pub async fn parse_dailyco_response<T: DeserializeOwned>(resp: Response) -> Result<T> {
if resp.status().is_success() {
Ok(resp.json().await?)
} else {
Err(Error::from_failed_daily_request(resp).await)
}
}