ibm_watson/text-to-speech/mod.rs
1use reqwest::{
2 header::{HeaderMap, HeaderValue, AUTHORIZATION},
3 Client, ClientBuilder,
4};
5
6use crate::auth::IamAuthenticator;
7
8use self::voices::WatsonVoice;
9
10/// Manage custom Prompts, Words, Models
11pub mod customisations;
12/// Manage word pronunciation
13pub mod pronunciation;
14#[path = "speaker-models/mod.rs"]
15/// Manage speaker models
16pub mod speaker_models;
17/// Synthesise text to speech
18pub mod synthesis;
19#[path = "user-data/mod.rs"]
20/// Delete user data
21pub mod user_data;
22/// View information about Watson voices
23pub mod voices;
24
25/// Creates a client used to send requests to your Text To Speech endpoint
26pub struct TextToSpeech<'a> {
27 service_url: &'a str,
28 voice: WatsonVoice,
29 client: Client,
30}
31
32impl<'a> TextToSpeech<'a> {
33 /// Create a new Text To Speech instance. This instance will be used to make all the requests
34 /// to the text to speech service.
35 ///
36 /// # Parameters
37 /// * `authenticator` - The [`IamAuthenticator`] containing your IAM Access Token
38 /// * `service_url` - The endpoint for your text to speech instance. All Text To Speech
39 /// requests will be made to this endpoint
40 ///
41 /// # Examples
42 /// ``` no_run
43 /// # use ibm_watson::{
44 /// # auth::IamAuthenticator,
45 /// # tts::{voices::WatsonVoice, TextToSpeech},
46 /// # };
47 /// # async fn foo()-> Result<(), Box<dyn std::error::Error>> {
48 /// let auth = IamAuthenticator::new("api_key").await?;
49 /// let tts = TextToSpeech::new(&auth, "service_url");
50 /// let voice = tts.get_voice(WatsonVoice::EnGbCharlotteV3, None).await?;
51 /// # Ok(())
52 /// # }
53 /// ```
54 ///
55 /// [`IamAuthenticator`]: super::auth::IamAuthenticator
56 pub fn new(authenticator: &'a IamAuthenticator, service_url: &'a str) -> Self {
57 let client = ClientBuilder::new();
58 let default_headers = Self::default_headers(authenticator.token_response().access_token());
59 let client = client.default_headers(default_headers);
60
61 #[cfg(feature = "http2")]
62 let client = ClientBuilder::use_rustls_tls(client);
63
64 #[cfg(feature = "http2")]
65 let client = client.http2_prior_knowledge();
66
67 let client = client.build().unwrap();
68
69 Self {
70 service_url,
71 voice: WatsonVoice::default(),
72 client,
73 }
74 }
75
76 /// Change the default voice to use for Text To Speech requests
77 ///
78 /// # Parameters
79 ///
80 /// * `voice` - Use this [`voice`] in place of the [`default`] one
81 ///
82 /// # Examples
83 /// ``` no_run
84 /// # use ibm_watson::{
85 /// # auth::IamAuthenticator,
86 /// # tts::{voices::WatsonVoice, TextToSpeech},
87 /// # };
88 /// # async fn foo()-> Result<(), Box<dyn std::error::Error>> {
89 /// # let auth = IamAuthenticator::new("api_key").await?;
90 /// let mut tts = TextToSpeech::new(&auth, "service_url");
91 /// tts.set_voice(WatsonVoice::EnGbCharlotteV3);
92 /// # Ok(())
93 /// # }
94 /// ```
95 ///
96 /// [`voice`]: self::voices::WatsonVoice
97 /// [`default`]: self::voices::WatsonVoice::EnUsMichaelV3
98 pub fn set_voice(&mut self, voice: WatsonVoice) {
99 self.voice = voice;
100 }
101
102 pub(crate) fn get_client(&self) -> Client {
103 self.client.clone()
104 }
105
106 fn default_headers(token: &str) -> HeaderMap<HeaderValue> {
107 let mut headers = HeaderMap::new();
108 let mut auth_value = HeaderValue::from_str(&format!("Bearer {}", token)).unwrap();
109 auth_value.set_sensitive(true);
110 headers.insert(AUTHORIZATION, auth_value);
111 headers
112 }
113}