diff --git a/src/audio_query.rs b/src/audio_query.rs new file mode 100644 index 0000000..765a9d9 --- /dev/null +++ b/src/audio_query.rs @@ -0,0 +1,22 @@ +use crate::{restapi::RestAPI, types::audio_query::AudioQueryType}; +use bytes::Bytes; +use reqwest::Result; + +pub struct AudioQuery { + restapi: RestAPI, + audio_query: AudioQueryType, +} + +impl AudioQuery { + pub fn new(restapi: RestAPI, audio_query: AudioQueryType) -> Self { + Self { + restapi, + audio_query, + } + } + + pub async fn synthesis(&self, speaker: i32) -> Result { + let data = self.restapi.synthesis(&self.audio_query, speaker).await?; + Ok(data) + } +} diff --git a/src/client.rs b/src/client.rs index 6b7af9e..27cbe77 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1,4 +1,5 @@ -use crate::restapi::RestAPI; +use crate::{audio_query::AudioQuery, restapi::RestAPI, types::audio_query::AudioQueryType}; +use reqwest::Result; pub struct Client { restapi: RestAPI, @@ -10,4 +11,14 @@ impl Client { restapi: RestAPI::new(base_path), } } -} \ No newline at end of file + + pub async fn create_audio_query( + &self, + text: &str, + core_version: Option<&str>, + ) -> Result { + let data: AudioQueryType = self.restapi.create_audio_query(text, core_version).await?; + let audio_query = AudioQuery::new(self.restapi.clone(), data); + Ok(audio_query) + } +} diff --git a/src/lib.rs b/src/lib.rs index 281d2af..c0e78cf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,8 +1,9 @@ +mod audio_query; +mod client; +mod restapi; mod types; -pub fn add(left: usize, right: usize) -> usize { - left + right -} +pub use client::Client; #[cfg(test)] mod tests { @@ -10,7 +11,12 @@ mod tests { #[test] fn it_works() { - let result = add(2, 2); - assert_eq!(result, 4); + let client = Client::new("http://localhost:50021"); + let audio_query = client.create_audio_query("こんにちは", 1) + .await + .unwrap(); + audio_query.synthesis(1) + .await + .unwrap(); } } diff --git a/src/restapi.rs b/src/restapi.rs index 481d336..e1bf152 100644 --- a/src/restapi.rs +++ b/src/restapi.rs @@ -1,7 +1,8 @@ -use reqwest::{Client, RequestBuilder, Result}; use crate::types::audio_query::AudioQueryType; use bytes::Bytes; +use reqwest::{Client, RequestBuilder, Result}; +#[derive(Clone)] pub struct RestAPI { base_path: String, client: Client, @@ -16,29 +17,40 @@ impl RestAPI { } pub fn request(&self, method: &str, path: &str) -> RequestBuilder { - self.client - .request(method.parse().unwrap(), &format!("{}{}", self.base_path, path)) + self.client.request( + method.parse().unwrap(), + format!("{}{}", self.base_path, path), + ) } - pub async fn create_audio_query(&self, text: &str, core_version: Option<&str>) -> Result { - let mut params = vec![("text", text)]; + pub async fn create_audio_query( + &self, speaker: i32, + text: &str, + core_version: Option<&str>, + ) -> Result { + let mut params = vec![("text", text), ("speaker", speaker)]; if let Some(core_version) = core_version { params.push(("core_version", core_version)) } - self.request("POST", "/audio_query") - .param(¶ms) + let data: AudioQueryType = self + .request("POST", "/audio_query") + .query(¶ms) .send() .await? .json() - .await? + .await?; + Ok(data) } - pub async fn synthesis(&self, audio_query: &AudioQueryType) -> Result { - self.request("POST", "/synthesis") + pub async fn synthesis(&self, audio_query: &AudioQueryType, speaker: i32) -> Result { + let data = self + .request("POST", "/synthesis") .json(audio_query) + .query(&[("speaker", speaker)]) .send() .await? .bytes() - .await? + .await?; + Ok(data) } -} \ No newline at end of file +} diff --git a/src/types/mod.rs b/src/types/mod.rs index f938474..6fa1a3a 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -1 +1 @@ -mod audio_query; +pub mod audio_query;