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 {
|
pub struct Client {
|
||||||
restapi: RestAPI,
|
restapi: RestAPI,
|
||||||
@ -10,4 +11,14 @@ impl Client {
|
|||||||
restapi: RestAPI::new(base_path),
|
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;
|
mod types;
|
||||||
|
|
||||||
pub fn add(left: usize, right: usize) -> usize {
|
pub use client::Client;
|
||||||
left + right
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
@ -10,7 +11,12 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn it_works() {
|
fn it_works() {
|
||||||
let result = add(2, 2);
|
let client = Client::new("http://localhost:50021");
|
||||||
assert_eq!(result, 4);
|
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 crate::types::audio_query::AudioQueryType;
|
||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
|
use reqwest::{Client, RequestBuilder, Result};
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
pub struct RestAPI {
|
pub struct RestAPI {
|
||||||
base_path: String,
|
base_path: String,
|
||||||
client: Client,
|
client: Client,
|
||||||
@ -16,29 +17,40 @@ impl RestAPI {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn request(&self, method: &str, path: &str) -> RequestBuilder {
|
pub fn request(&self, method: &str, path: &str) -> RequestBuilder {
|
||||||
self.client
|
self.client.request(
|
||||||
.request(method.parse().unwrap(), &format!("{}{}", self.base_path, path))
|
method.parse().unwrap(),
|
||||||
|
format!("{}{}", self.base_path, path),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn create_audio_query(&self, text: &str, core_version: Option<&str>) -> Result<AudioQueryType> {
|
pub async fn create_audio_query(
|
||||||
let mut params = vec![("text", text)];
|
&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 {
|
if let Some(core_version) = core_version {
|
||||||
params.push(("core_version", core_version))
|
params.push(("core_version", core_version))
|
||||||
}
|
}
|
||||||
self.request("POST", "/audio_query")
|
let data: AudioQueryType = self
|
||||||
.param(¶ms)
|
.request("POST", "/audio_query")
|
||||||
|
.query(¶ms)
|
||||||
.send()
|
.send()
|
||||||
.await?
|
.await?
|
||||||
.json()
|
.json()
|
||||||
.await?
|
.await?;
|
||||||
|
Ok(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn synthesis(&self, audio_query: &AudioQueryType) -> Result<Bytes> {
|
pub async fn synthesis(&self, audio_query: &AudioQueryType, speaker: i32) -> Result<Bytes> {
|
||||||
self.request("POST", "/synthesis")
|
let data = self
|
||||||
|
.request("POST", "/synthesis")
|
||||||
.json(audio_query)
|
.json(audio_query)
|
||||||
|
.query(&[("speaker", speaker)])
|
||||||
.send()
|
.send()
|
||||||
.await?
|
.await?
|
||||||
.bytes()
|
.bytes()
|
||||||
.await?
|
.await?;
|
||||||
|
Ok(data)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1 +1 @@
|
|||||||
mod audio_query;
|
pub mod audio_query;
|
||||||
|
Reference in New Issue
Block a user