mirror of
https://github.com/storytold/discord-vocodes.git
synced 2026-10-19 07:43:06 +00:00
don't create messages on zero-length or non-200
This commit is contained in:
+15
-4
@@ -1,14 +1,17 @@
|
||||
use anyhow::Result as AnyhowResult;
|
||||
use anyhow::bail;
|
||||
use hyper::Body;
|
||||
use hyper::Client as HyperClient;
|
||||
use hyper::Request;
|
||||
use hyper_tls::HttpsConnector;
|
||||
use log::{error, debug, info, warn};
|
||||
|
||||
const VOCODES_SPEAK_ENDPOINT : &'static str = "http://mumble.stream/speak";
|
||||
|
||||
pub async fn fetch(text: &str, speaker: &str) -> Result<Vec<u8>, Box<dyn std::error::Error + Send + Sync>> {
|
||||
pub async fn fetch(text: &str, speaker: &str) -> AnyhowResult<Vec<u8>> {
|
||||
let mut request = format!("{{\"speaker\": \"{}\", \"text\": \"{}\"}}", speaker, text);
|
||||
|
||||
println!("Req: {}", request);
|
||||
debug!("Req: {}", request);
|
||||
|
||||
let https = HttpsConnector::new();
|
||||
let client = HyperClient::builder()
|
||||
@@ -25,12 +28,20 @@ pub async fn fetch(text: &str, speaker: &str) -> Result<Vec<u8>, Box<dyn std::er
|
||||
|
||||
let resp = client.request(req).await?;
|
||||
|
||||
println!("Status: {}", resp.status());
|
||||
info!("Vocodes Response Status: {}", resp.status());
|
||||
|
||||
if resp.status() != 200 {
|
||||
bail!("The response code wasn't 200.");
|
||||
}
|
||||
|
||||
let bytes = hyper::body::to_bytes(resp.into_body()).await?;
|
||||
let result = bytes.to_vec();
|
||||
|
||||
println!("Length: {}", result.len());
|
||||
if result.len() == 0 {
|
||||
bail!("The response body has zero length.");
|
||||
}
|
||||
|
||||
info!("Vocodes Response Length: {}", result.len());
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user