add: default header

This commit is contained in:
mc_fdc
2023-04-08 18:47:03 +00:00
parent 10c077a1dd
commit 126f4b7d7b
4 changed files with 14 additions and 9 deletions

View File

@ -9,7 +9,7 @@ use std::{io::Write, fs::File};
#[tokio::main] #[tokio::main]
async fn main() -> Result<()> { async fn main() -> Result<()> {
let client = Client::new("http://localhost:50021".to_string()); let client = Client::new("http://localhost:50021".to_string(), None);
let audio_query = client let audio_query = client
.create_audio_query("こんにちは", 1, None) .create_audio_query("こんにちは", 1, None)
.await?; .await?;

View File

@ -1,10 +1,10 @@
use voicevox_client::Client; use voicevox_client::Client;
use reqwest::Result; use reqwest::{Result, header::HeaderMap};
use std::{io::Write, fs::File}; use std::{io::Write, fs::File};
#[tokio::main] #[tokio::main]
async fn main() -> Result<()> { async fn main() -> Result<()> {
let client = Client::new("http://localhost:50021".to_string()); let client = Client::new("http://localhost:50021".to_string(), None);
let audio_query = client let audio_query = client
.create_audio_query("こんにちは", 1, None) .create_audio_query("こんにちは", 1, None)
.await?; .await?;

View File

@ -1,14 +1,14 @@
use crate::{audio_query::AudioQuery, restapi::RestAPI, types::audio_query::AudioQueryType}; use crate::{audio_query::AudioQuery, restapi::RestAPI, types::audio_query::AudioQueryType};
use reqwest::Result; use reqwest::{Result, header::HeaderMap};
pub struct Client { pub struct Client {
restapi: RestAPI, restapi: RestAPI,
} }
impl Client { impl Client {
pub fn new(base_path: String) -> Self { pub fn new(base_path: String, headers: Option<HeaderMap>) -> Self {
Self { Self {
restapi: RestAPI::new(base_path), restapi: RestAPI::new(base_path, headers),
} }
} }

View File

@ -1,6 +1,6 @@
use crate::types::audio_query::AudioQueryType; use crate::types::audio_query::AudioQueryType;
use bytes::Bytes; use bytes::Bytes;
use reqwest::{Client, RequestBuilder, Result}; use reqwest::{Client, RequestBuilder, Result, header::HeaderMap};
#[derive(Clone)] #[derive(Clone)]
pub struct RestAPI { pub struct RestAPI {
@ -9,10 +9,15 @@ pub struct RestAPI {
} }
impl RestAPI { impl RestAPI {
pub fn new(base_path: String) -> Self { pub fn new(base_path: String, headers: Option<HeaderMap>) -> Self {
let mut client_builder = Client::builder();
if let Some(headers) = headers {
client_builder = client_builder.default_headers(headers);
}
let client = client_builder.build().unwrap();
Self { Self {
base_path, base_path,
client: Client::new(), client,
} }
} }