#![allow(dead_code)]
use crate::error::Error;
use crate::shared::path_segments::STREAM_PATH;
use std::path::Path;
use futures_util::{Stream, StreamExt};
use std::pin::Pin;
use super::*;
const AUDIO_ISOLATION_PATH: &str = "v1/audio-isolation";
#[derive(Clone, Debug)]
pub struct AudioIsolation {
pub audio_file: String,
}
impl AudioIsolation {
pub fn new<T: Into<String> >(audio_file: T) -> Self {
Self { audio_file: audio_file.into() }
}
}
impl Endpoint for AudioIsolation {
type ResponseBody = Bytes;
fn method(&self) -> Method {
Method::POST
}
fn request_body(&self) -> Result<RequestBody> {
Ok(RequestBody::Multipart(to_form(&self.audio_file)?))
}
async fn response_body(self, resp: Response) -> Result<Self::ResponseBody> {
Ok(resp.bytes().await?)
}
fn url(&self) -> Url {
let mut url = BASE_URL.parse::<Url>().unwrap();
url.set_path(AUDIO_ISOLATION_PATH);
url
}
}
#[derive(Clone, Debug)]
pub struct AudioIsolationStream {
pub audio_file: String,
}
impl AudioIsolationStream {
pub fn new<T: Into<String> >(audio_file: T) -> Self {
Self { audio_file: audio_file.into() }
}
}
type AudioIsolationStreamResponse = Pin<Box<dyn Stream<Item = Result<Bytes>> + Send>>;
impl Endpoint for AudioIsolationStream {
type ResponseBody = AudioIsolationStreamResponse;
fn method(&self) -> Method {
Method::POST
}
fn request_body(&self) -> Result<RequestBody> {
Ok(RequestBody::Multipart(to_form(&self.audio_file)?))
}
async fn response_body(self, resp: Response) -> Result<Self::ResponseBody> {
let stream = resp.bytes_stream();
let stream = stream.map(|r| r.map_err(Into::into));
Ok(Box::pin(stream))
}
fn url(&self) -> Url {
let mut url = BASE_URL.parse::<Url>().unwrap();
url.set_path(&format!("{}{}", AUDIO_ISOLATION_PATH, STREAM_PATH));
url
}
}
fn to_form(audio_file: &str) -> Result<Form> {
let mut form = Form::new();
let path = Path::new(audio_file);
let audio_bytes = std::fs::read(audio_file)?;
let mut part = Part::bytes(audio_bytes);
let file_path_str = path.to_str().ok_or(Box::new(Error::PathNotValidUTF8))?;
part = part.file_name(file_path_str.to_string());
let mime_subtype = path
.extension()
.ok_or(Box::new(Error::FileExtensionNotFound))?
.to_str()
.ok_or(Box::new(Error::FileExtensionNotValidUTF8))?;
let mime = format!("audio/{}", mime_subtype);
part = part.mime_str(&mime)?;
form = form.part("audio", part);
Ok(form)
}