don't create messages on zero-length or non-200

This commit is contained in:
Brandon Thomas
2020-09-12 04:47:41 -04:00
parent 518a716c59
commit 1126803cab
+15 -4
View File
@@ -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)
}