mirror of
https://github.com/mii443/rust.git
synced 2025-08-22 16:25:37 +00:00
oh
This commit is contained in:
22
src/audio_query.rs
Normal file
22
src/audio_query.rs
Normal 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)
|
||||
}
|
||||
}
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
16
src/lib.rs
16
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();
|
||||
}
|
||||
}
|
||||
|
@ -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(¶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<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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1 +1 @@
|
||||
mod audio_query;
|
||||
pub mod audio_query;
|
||||
|
Reference in New Issue
Block a user