mistral-openapi-client 0.1.0

Our Chat Completion and Embeddings APIs specification. Create your account on [La Plateforme](https://console.mistral.ai) to get access and read the [docs](https://docs.mistral.ai) to learn how to use it.
Documentation
/*
 * Mistral AI API
 *
 * Our Chat Completion and Embeddings APIs specification. Create your account on [La Plateforme](https://console.mistral.ai) to get access and read the [docs](https://docs.mistral.ai) to learn how to use it.
 *
 * The version of the OpenAPI document: 1.0.0
 * 
 * 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 [`audio_api_v1_transcriptions_post`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum AudioApiV1TranscriptionsPostError {
    UnknownValue(serde_json::Value),
}

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


pub async fn audio_api_v1_transcriptions_post(configuration: &configuration::Configuration, model: &str, file: Option<std::path::PathBuf>, file_url: Option<&str>, file_id: Option<&str>, language: Option<&str>, temperature: Option<f64>, stream: Option<bool>, diarize: Option<bool>, context_bias: Option<Vec<String>>, timestamp_granularities: Option<Vec<models::TimestampGranularity>>) -> Result<models::TranscriptionResponse, Error<AudioApiV1TranscriptionsPostError>> {
    // add a prefix to parameters to efficiently prevent name collisions
    let p_form_model = model;
    let p_form_file = file;
    let p_form_file_url = file_url;
    let p_form_file_id = file_id;
    let p_form_language = language;
    let p_form_temperature = temperature;
    let p_form_stream = stream;
    let p_form_diarize = diarize;
    let p_form_context_bias = context_bias;
    let p_form_timestamp_granularities = timestamp_granularities;

    let uri_str = format!("{}/v1/audio/transcriptions", 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());
    }
    if let Some(ref token) = configuration.bearer_access_token {
        req_builder = req_builder.bearer_auth(token.to_owned());
    };
    let mut multipart_form = reqwest::multipart::Form::new();
    multipart_form = multipart_form.text("model", p_form_model.to_string());
    if let Some(param_value) = p_form_file {
        multipart_form = multipart_form.text("file", serde_json::to_string(&param_value)?);
    }
    if let Some(param_value) = p_form_file_url {
        multipart_form = multipart_form.text("file_url", param_value.to_string());
    }
    if let Some(param_value) = p_form_file_id {
        multipart_form = multipart_form.text("file_id", param_value.to_string());
    }
    if let Some(param_value) = p_form_language {
        multipart_form = multipart_form.text("language", param_value.to_string());
    }
    if let Some(param_value) = p_form_temperature {
        multipart_form = multipart_form.text("temperature", param_value.to_string());
    }
    if let Some(param_value) = p_form_stream {
        multipart_form = multipart_form.text("stream", param_value.to_string());
    }
    if let Some(param_value) = p_form_diarize {
        multipart_form = multipart_form.text("diarize", param_value.to_string());
    }
    if let Some(param_value) = p_form_context_bias {
        multipart_form = multipart_form.text("context_bias", serde_json::to_string(&param_value)?);
    }
    if let Some(param_value) = p_form_timestamp_granularities {
        multipart_form = multipart_form.text("timestamp_granularities", serde_json::to_string(&param_value)?);
    }
    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 `models::TranscriptionResponse`"))),
            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::TranscriptionResponse`")))),
        }
    } else {
        let content = resp.text().await?;
        let entity: Option<AudioApiV1TranscriptionsPostError> = serde_json::from_str(&content).ok();
        Err(Error::ResponseError(ResponseContent { status, content, entity }))
    }
}

pub async fn audio_api_v1_transcriptions_post_stream(configuration: &configuration::Configuration, model: &str, file: Option<std::path::PathBuf>, file_url: Option<&str>, file_id: Option<&str>, language: Option<&str>, temperature: Option<f64>, stream: Option<bool>, diarize: Option<bool>, context_bias: Option<Vec<String>>, timestamp_granularities: Option<Vec<models::TimestampGranularity>>) -> Result<models::TranscriptionStreamEvents, Error<AudioApiV1TranscriptionsPostStreamError>> {
    // add a prefix to parameters to efficiently prevent name collisions
    let p_form_model = model;
    let p_form_file = file;
    let p_form_file_url = file_url;
    let p_form_file_id = file_id;
    let p_form_language = language;
    let p_form_temperature = temperature;
    let p_form_stream = stream;
    let p_form_diarize = diarize;
    let p_form_context_bias = context_bias;
    let p_form_timestamp_granularities = timestamp_granularities;

    let uri_str = format!("{}/v1/audio/transcriptions#stream", 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());
    }
    if let Some(ref token) = configuration.bearer_access_token {
        req_builder = req_builder.bearer_auth(token.to_owned());
    };
    let mut multipart_form = reqwest::multipart::Form::new();
    multipart_form = multipart_form.text("model", p_form_model.to_string());
    if let Some(param_value) = p_form_file {
        multipart_form = multipart_form.text("file", serde_json::to_string(&param_value)?);
    }
    if let Some(param_value) = p_form_file_url {
        multipart_form = multipart_form.text("file_url", param_value.to_string());
    }
    if let Some(param_value) = p_form_file_id {
        multipart_form = multipart_form.text("file_id", param_value.to_string());
    }
    if let Some(param_value) = p_form_language {
        multipart_form = multipart_form.text("language", param_value.to_string());
    }
    if let Some(param_value) = p_form_temperature {
        multipart_form = multipart_form.text("temperature", param_value.to_string());
    }
    if let Some(param_value) = p_form_stream {
        multipart_form = multipart_form.text("stream", param_value.to_string());
    }
    if let Some(param_value) = p_form_diarize {
        multipart_form = multipart_form.text("diarize", param_value.to_string());
    }
    if let Some(param_value) = p_form_context_bias {
        multipart_form = multipart_form.text("context_bias", serde_json::to_string(&param_value)?);
    }
    if let Some(param_value) = p_form_timestamp_granularities {
        multipart_form = multipart_form.text("timestamp_granularities", serde_json::to_string(&param_value)?);
    }
    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 `models::TranscriptionStreamEvents`"))),
            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::TranscriptionStreamEvents`")))),
        }
    } else {
        let content = resp.text().await?;
        let entity: Option<AudioApiV1TranscriptionsPostStreamError> = serde_json::from_str(&content).ok();
        Err(Error::ResponseError(ResponseContent { status, content, entity }))
    }
}