This commit is contained in:
mc_fdc
2023-04-07 23:10:25 +00:00
parent d05235ae89
commit 47eba2e866
5 changed files with 71 additions and 20 deletions

22
src/audio_query.rs Normal file
View File

@ -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<Bytes> {
let data = self.restapi.synthesis(&self.audio_query, speaker).await?;
Ok(data)
}
}

View File

@ -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),
}
}
}
pub async fn create_audio_query(
&self,
text: &str,
core_version: Option<&str>,
) -> Result<AudioQuery> {
let data: AudioQueryType = self.restapi.create_audio_query(text, core_version).await?;
let audio_query = AudioQuery::new(self.restapi.clone(), data);
Ok(audio_query)
}
}

View File

@ -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();
}
}

View File

@ -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<AudioQueryType> {
let mut params = vec![("text", text)];
pub async fn create_audio_query(
&self, speaker: i32,
text: &str,
core_version: Option<&str>,
) -> Result<AudioQueryType> {
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(&params)
let data: AudioQueryType = self
.request("POST", "/audio_query")
.query(&params)
.send()
.await?
.json()
.await?
.await?;
Ok(data)
}
pub async fn synthesis(&self, audio_query: &AudioQueryType) -> Result<Bytes> {
self.request("POST", "/synthesis")
pub async fn synthesis(&self, audio_query: &AudioQueryType, speaker: i32) -> Result<Bytes> {
let data = self
.request("POST", "/synthesis")
.json(audio_query)
.query(&[("speaker", speaker)])
.send()
.await?
.bytes()
.await?
.await?;
Ok(data)
}
}
}

View File

@ -1 +1 @@
mod audio_query;
pub mod audio_query;