/*
 * Text Video Agent Api
 *
 * 文本生成视频API服务
 *
 * The version of the OpenAPI document: 1.0.6
 * 
 * Generated by: https://openapi-generator.tech
 */


use reqwest;
use serde::{Deserialize, Serialize, de::Error as _};
use crate::{apis::ResponseContent, models};
use super::{Error, configuration, ContentType};


/// struct for typed errors of method [`query_img_recognition_status_api_ark_omnihuman_task_status_get`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum QueryImgRecognitionStatusApiArkOmnihumanTaskStatusGetError {
    Status422(models::HttpValidationError),
    UnknownValue(serde_json::Value),
}

/// struct for typed errors of method [`submit_img_recognition_api_ark_omnihuman_img_recognition_post`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum SubmitImgRecognitionApiArkOmnihumanImgRecognitionPostError {
    Status422(models::HttpValidationError),
    UnknownValue(serde_json::Value),
}

/// struct for typed errors of method [`submit_video_generate_task_api_ark_omnihuman_video_submit_task_post`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum SubmitVideoGenerateTaskApiArkOmnihumanVideoSubmitTaskPostError {
    Status422(models::HttpValidationError),
    UnknownValue(serde_json::Value),
}


/// Args:      task_type: face_detect: 人脸检测结果, video_generate: 视频生成结果 Returns:
pub async fn query_img_recognition_status_api_ark_omnihuman_task_status_get(configuration: &configuration::Configuration, task_id: &str, task_type: Option<&str>) -> Result<serde_json::Value, Error<QueryImgRecognitionStatusApiArkOmnihumanTaskStatusGetError>> {
    // add a prefix to parameters to efficiently prevent name collisions
    let p_task_id = task_id;
    let p_task_type = task_type;

    let uri_str = format!("{}/api/ark/omnihuman/task/status", configuration.base_path);
    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);

    req_builder = req_builder.query(&[("task_id", &p_task_id.to_string())]);
    if let Some(ref param_value) = p_task_type {
        req_builder = req_builder.query(&[("task_type", &param_value.to_string())]);
    }
    if let Some(ref user_agent) = configuration.user_agent {
        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
    }

    let req = req_builder.build()?;
    let resp = configuration.client.execute(req).await?;

    let status = resp.status();
    let content_type = resp
        .headers()
        .get("content-type")
        .and_then(|v| v.to_str().ok())
        .unwrap_or("application/octet-stream");
    let content_type = super::ContentType::from(content_type);

    if !status.is_client_error() && !status.is_server_error() {
        let content = resp.text().await?;
        match content_type {
            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `serde_json::Value`"))),
            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `serde_json::Value`")))),
        }
    } else {
        let content = resp.text().await?;
        let entity: Option<QueryImgRecognitionStatusApiArkOmnihumanTaskStatusGetError> = serde_json::from_str(&content).ok();
        Err(Error::ResponseError(ResponseContent { status, content, entity }))
    }
}

pub async fn submit_img_recognition_api_ark_omnihuman_img_recognition_post(configuration: &configuration::Configuration, img_url: Option<&str>, img_file: Option<std::path::PathBuf>) -> Result<serde_json::Value, Error<SubmitImgRecognitionApiArkOmnihumanImgRecognitionPostError>> {
    // add a prefix to parameters to efficiently prevent name collisions
    let p_img_url = img_url;
    let p_img_file = img_file;

    let uri_str = format!("{}/api/ark/omnihuman/img/recognition", configuration.base_path);
    let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);

    if let Some(ref user_agent) = configuration.user_agent {
        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
    }
    let mut multipart_form = reqwest::multipart::Form::new();
    if let Some(param_value) = p_img_url {
        multipart_form = multipart_form.text("img_url", param_value.to_string());
    }
    // TODO: support file upload for 'img_file' parameter
    req_builder = req_builder.multipart(multipart_form);

    let req = req_builder.build()?;
    let resp = configuration.client.execute(req).await?;

    let status = resp.status();
    let content_type = resp
        .headers()
        .get("content-type")
        .and_then(|v| v.to_str().ok())
        .unwrap_or("application/octet-stream");
    let content_type = super::ContentType::from(content_type);

    if !status.is_client_error() && !status.is_server_error() {
        let content = resp.text().await?;
        match content_type {
            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `serde_json::Value`"))),
            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `serde_json::Value`")))),
        }
    } else {
        let content = resp.text().await?;
        let entity: Option<SubmitImgRecognitionApiArkOmnihumanImgRecognitionPostError> = serde_json::from_str(&content).ok();
        Err(Error::ResponseError(ResponseContent { status, content, entity }))
    }
}

/// Args:      img_input: 输入的图片, 支持链接 + 文件流      audio_input: 输入的音频, 支持链接 + 文件流  Returns: {\"status\": True, 'data': '任务id', 'msg': '信息'}
pub async fn submit_video_generate_task_api_ark_omnihuman_video_submit_task_post(configuration: &configuration::Configuration, img_url: Option<&str>, img_file: Option<std::path::PathBuf>, audio_url: Option<&str>, audio_file: Option<std::path::PathBuf>) -> Result<serde_json::Value, Error<SubmitVideoGenerateTaskApiArkOmnihumanVideoSubmitTaskPostError>> {
    // add a prefix to parameters to efficiently prevent name collisions
    let p_img_url = img_url;
    let p_img_file = img_file;
    let p_audio_url = audio_url;
    let p_audio_file = audio_file;

    let uri_str = format!("{}/api/ark/omnihuman/video/submit/task", configuration.base_path);
    let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);

    if let Some(ref user_agent) = configuration.user_agent {
        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
    }
    let mut multipart_form = reqwest::multipart::Form::new();
    if let Some(param_value) = p_img_url {
        multipart_form = multipart_form.text("img_url", param_value.to_string());
    }
    // TODO: support file upload for 'img_file' parameter
    if let Some(param_value) = p_audio_url {
        multipart_form = multipart_form.text("audio_url", param_value.to_string());
    }
    // TODO: support file upload for 'audio_file' parameter
    req_builder = req_builder.multipart(multipart_form);

    let req = req_builder.build()?;
    let resp = configuration.client.execute(req).await?;

    let status = resp.status();
    let content_type = resp
        .headers()
        .get("content-type")
        .and_then(|v| v.to_str().ok())
        .unwrap_or("application/octet-stream");
    let content_type = super::ContentType::from(content_type);

    if !status.is_client_error() && !status.is_server_error() {
        let content = resp.text().await?;
        match content_type {
            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `serde_json::Value`"))),
            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `serde_json::Value`")))),
        }
    } else {
        let content = resp.text().await?;
        let entity: Option<SubmitVideoGenerateTaskApiArkOmnihumanVideoSubmitTaskPostError> = serde_json::from_str(&content).ok();
        Err(Error::ResponseError(ResponseContent { status, content, entity }))
    }
}

