elevenlabs_rs 0.3.2

A lib crate for ElevenLabs
Documentation
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
//! The history endpoints
#![allow(dead_code)]

use super::*;
use crate::endpoints::voice::VoiceSettings;

const HISTORY_PATH: &str = "/v1/history";
const AUDIO_PATH: &str = "/audio";
const PAGE_SIZE_QUERY: &str = "page_size";
const HISTORY_ITEM_IDS: &str = "history_item_ids";
const START_AFTER_HISTORY_ITEM_ID_QUERY: &str = "start_after_history_item_id";
const VOICE_ID_QUERY: &str = "voice_id";

#[derive(Clone, Debug)]
pub struct DeleteHistoryItem(HistoryItemID);

impl DeleteHistoryItem {
    pub fn new<T: Into<String>>(history_item_id: T) -> Self {
        Self(HistoryItemID(history_item_id.into()))
    }
}

impl Endpoint for DeleteHistoryItem {
    type ResponseBody = StatusResponseBody;
    fn method(&self) -> Method {
        Method::DELETE
    }
    async fn response_body(self, resp: Response) -> Result<Self::ResponseBody> {
        Ok(resp.json().await?)
    }
    fn url(&self) -> Url {
        let mut url = BASE_URL.parse::<Url>().unwrap();
        url.set_path(&format!("{}/{}", HISTORY_PATH, self.0 .0));
        url
    }
}

/// Download one or more history items.
/// If one history item ID is provided, we will return a single audio file.
/// If more than one history item IDs are provided, we will provide the history items packed into a .zip file.
///
/// # Example
/// ```no_run
/// use elevenlabs_rs::*;
/// use elevenlabs_rs::utils::save;
///
/// #[tokio::main]
/// async fn main() -> Result<()> {
///     let client = ElevenLabsClient::default()?;
///     let history_item_ids = client
///         .hit(GetGeneratedItems::new(HistoryQuery::default()))
///         .await?
///         .history()
///         .iter()
///         .map(|i| i.history_item_id().to_string())
///         .collect::<Vec<String>>();
///     let body = DownloadBody::new(history_item_ids);
///     let downloaded_items = client.hit(DownloadHistoryItems::new(body)).await?;
///     save("last_100_items.zip", downloaded_items)?;
///     Ok(())
/// }
/// ```
#[derive(Clone, Debug)]
pub struct DownloadHistoryItems(DownloadBody);

impl DownloadHistoryItems {
    pub fn new(body: DownloadBody) -> Self {
        Self(body)
    }
}

impl Endpoint for DownloadHistoryItems {
    type ResponseBody = Bytes;
    fn method(&self) -> Method {
        Method::POST
    }
    fn request_body(&self) -> Result<RequestBody> {
        Ok(RequestBody::Json(serde_json::to_value(&self.0)?))
    }

    async fn response_body(self, resp: Response) -> Result<Self::ResponseBody> {
        Ok(resp.bytes().await?)
    }
    fn url(&self) -> Url {
        let mut url = BASE_URL.parse::<Url>().unwrap();
        url.set_path(&format!("{}{}", HISTORY_PATH, DOWNLOAD_PATH));
        url
    }
}
#[derive(Clone, Debug, Serialize)]
pub struct DownloadBody {
    pub history_item_ids: Vec<String>,
    /// Output format to transcode the audio file, can be wav or default (Mp3).
    #[serde(skip_serializing_if = "DownloadOutputFormat::is_mp3")]
    pub output_format: DownloadOutputFormat,
}

impl DownloadBody {
    pub fn new(history_item_ids: Vec<String>) -> Self {
        Self  {
            history_item_ids,
            output_format: DownloadOutputFormat::default(),
        }
    }
    pub fn with_output_format(mut self, output_format: DownloadOutputFormat) -> Self {
        self.output_format = output_format;
        self
    }
}

#[derive(Clone, Debug, Default, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum DownloadOutputFormat {
    #[default]
    Mp3,
    Wav,
}

impl DownloadOutputFormat {
    fn is_mp3(&self) -> bool {
        match self {
            DownloadOutputFormat::Mp3 => true,
            DownloadOutputFormat::Wav => false,
        }
    }
}

/// Get the audio file of a history item.
///
/// # Example
/// ```no_run
/// use elevenlabs_rs::*;
/// use elevenlabs_rs::utils::play;
///
/// #[tokio::main]
/// async fn main() -> Result<()> {
///     let client = ElevenLabsClient::default()?;
///     let query = HistoryQuery::default().with_page_size(1);
///     let resp = client.hit(GetGeneratedItems::new(query))
///         .await?;
///     let item_id = resp
///         .history()
///         .first().unwrap()
///         .history_item_id();
///     let audio = client.hit(GetAudio::new(item_id)).await?;
///     play(audio)?;
///     Ok(())
/// }
/// ```
#[derive(Clone, Debug)]
pub struct GetAudio(HistoryItemID);

impl GetAudio {
    pub fn new<T: Into<String>>(history_item_id: T) -> Self {
        Self(HistoryItemID(history_item_id.into()))
    }
}

impl Endpoint for GetAudio {
    type ResponseBody = Bytes;
    fn method(&self) -> Method {
        Method::GET
    }
    async fn response_body(self, resp: Response) -> Result<Self::ResponseBody> {
        Ok(resp.bytes().await?)
    }
    fn url(&self) -> Url {
        let mut url = BASE_URL.parse::<Url>().unwrap();
        url.set_path(&format!("{}/{}{}", HISTORY_PATH, self.0 .0, AUDIO_PATH));
        url
    }
}

/// Get the generated items endpoint.
///
/// # Example
/// ```no_run
/// use elevenlabs_rs::*;
///
/// #[tokio::main]
/// async fn main() -> Result<()> {
///     let c = ElevenLabsClient::default()?;
///     let query = HistoryQuery::default()
///         .with_page_size(10)
///         .with_voice_id(PreMadeVoiceID::Alice);
///     let endpoint = GetGeneratedItems::new(query);
///     let resp = c.hit(endpoint).await?;
///     println!("{:#?}", resp);
///     Ok(())
/// }
/// ```
#[derive(Clone, Debug)]
pub struct GetGeneratedItems(HistoryQuery);

impl GetGeneratedItems {
    pub fn new(query: HistoryQuery) -> Self {
        Self(query)
    }
}

impl Endpoint for GetGeneratedItems {
    type ResponseBody = GeneratedItems;
    fn method(&self) -> Method {
        Method::GET
    }
    async fn response_body(self, resp: Response) -> Result<Self::ResponseBody> {
        Ok(resp.json().await?)
    }
    fn url(&self) -> Url {
        let mut ego = self.clone();
        let mut url = BASE_URL.parse::<Url>().unwrap();
        url.set_path(HISTORY_PATH);
        url.set_query(ego.0.join().as_deref());
        url
    }
}

#[derive(Clone, Debug)]
pub struct GetHistoryItem(HistoryItemID);

impl GetHistoryItem {
    pub fn new<T: Into<String>>(history_item_id: T) -> Self {
        Self(HistoryItemID(history_item_id.into()))
    }
}

impl Endpoint for GetHistoryItem {
    type ResponseBody = HistoryItem;
    fn method(&self) -> Method {
        Method::GET
    }
    async fn response_body(self, resp: Response) -> Result<Self::ResponseBody> {
        Ok(resp.json().await?)
    }
    fn url(&self) -> Url {
        let mut url = BASE_URL.parse::<Url>().unwrap();
        url.set_path(&format!("{}/{}", HISTORY_PATH, self.0 .0));
        url
    }
}

#[derive(Clone, Debug, Default)]
pub struct HistoryQuery {
    pub page_size: Option<String>,
    pub start_after_history_item_id: Option<String>,
    pub voice_id: Option<String>,
}

impl HistoryQuery {
    pub fn with_page_size(mut self, page_size: u16) -> Self {
        self.page_size = Some(format!("{}={}", PAGE_SIZE_QUERY, page_size));
        self
    }
    pub fn with_start_after_history_item_id(mut self, start_after_history_item_id: &str) -> Self {
        self.start_after_history_item_id = Some(format!(
            "{}={}",
            START_AFTER_HISTORY_ITEM_ID_QUERY, start_after_history_item_id
        ));
        self
    }

    pub fn with_voice_id<T: Into<String>>(mut self, voice_id: T) -> Self {
        self.voice_id = Some(format!("{}={}", VOICE_ID_QUERY, voice_id.into()));
        self
    }
    pub fn join(&mut self) -> Option<String> {
        let mut result = String::new();

        if let Some(value) = self.page_size.take() {
            result.push_str(&value);
        }
        if let Some(value) = self.start_after_history_item_id.take() {
            if !result.is_empty() {
                result.push('&');
            }
            result.push_str(&value);
        }
        if let Some(value) = self.voice_id.take() {
            if !result.is_empty() {
                result.push('&');
            }
            result.push_str(&value);
        }
        if result.is_empty() {
            None
        } else {
            Some(result)
        }
    }
}

#[derive(Clone, Debug, Deserialize)]
pub struct GeneratedItems {
    history: Vec<HistoryItem>,
    last_history_item_id: String,
    has_more: bool,
}

impl GeneratedItems {
    pub fn history(&self) -> &Vec<HistoryItem> {
        &self.history
    }
    pub fn last_history_item_id(&self) -> &str {
        &self.last_history_item_id
    }
    pub fn has_more(&self) -> bool {
        self.has_more
    }
}

#[derive(Clone, Debug, Deserialize)]
pub struct HistoryItem {
    history_item_id: String,
    request_id: String,
    voice_id: String,
    voice_name: String,
    voice_category: Option<String>,
    model_id: Option<String>,
    text: String,
    date_unix: u64,
    character_count_change_from: u64,
    character_count_change_to: u64,
    content_type: String,
    state: String,
    settings: VoiceSettings,
    feedback: Option<Feedback>,
    share_link_id: Option<String>,
    source: Option<String>,
}
impl HistoryItem {
    pub fn history_item_id(&self) -> &str {
        &self.history_item_id
    }
    pub fn request_id(&self) -> &str {
        &self.request_id
    }
    pub fn voice_id(&self) -> &str {
        &self.voice_id
    }
    pub fn voice_name(&self) -> &str {
        &self.voice_name
    }
    pub fn voice_category(&self) -> Option<&str> {
        self.voice_category.as_deref()
    }
    pub fn model_id(&self) -> Option<&str> {
        self.model_id.as_deref()
    }
    pub fn text(&self) -> &str {
        &self.text
    }
    pub fn date_unix(&self) -> u64 {
        self.date_unix
    }
    pub fn character_count_change_from(&self) -> u64 {
        self.character_count_change_from
    }
    pub fn character_count_change_to(&self) -> u64 {
        self.character_count_change_to
    }
    pub fn content_type(&self) -> &str {
        &self.content_type
    }
    pub fn state(&self) -> &str {
        &self.state
    }
    pub fn settings(&self) -> &VoiceSettings {
        &self.settings
    }
    pub fn feedback(&self) -> Option<&Feedback> {
        self.feedback.as_ref()
    }
    pub fn share_link_id(&self) -> Option<&str> {
        self.share_link_id.as_deref()
    }
    pub fn source(&self) -> Option<&str> {
        self.source.as_deref()
    }
}

#[derive(Clone, Debug, Deserialize)]
pub struct Feedback {
    thumbs_up: bool,
    feedback: String,
    emotions: bool,
    inaccurate_clone: bool,
    glitches: bool,
    audio_quality: bool,
    review_status: String,
    other: bool,
}
impl Feedback {
    pub fn thumbs_up(&self) -> bool {
        self.thumbs_up
    }
    pub fn feedback(&self) -> &str {
        &self.feedback
    }
    pub fn emotions(&self) -> bool {
        self.emotions
    }
    pub fn inaccurate_clone(&self) -> bool {
        self.inaccurate_clone
    }
    pub fn glitches(&self) -> bool {
        self.glitches
    }
    pub fn audio_quality(&self) -> bool {
        self.audio_quality
    }
    pub fn review_status(&self) -> &str {
        &self.review_status
    }
    pub fn other(&self) -> bool {
        self.other
    }
}