vrchatapi 1.20.8-nightly.11

VRChat API Client for Rust
Documentation
use super::{configuration, ContentType, Error};
use crate::{apis::ResponseContent, models};
use reqwest;
use serde::{de::Error as _, Deserialize, Serialize};
use tokio::fs::File as TokioFile;
use tokio_util::codec::{BytesCodec, FramedRead};

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

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

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

/// struct for typed errors of method [`get_user_prints`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum GetUserPrintsError {
    Status401(models::Error),
    Status403(models::Error),
    UnknownValue(serde_json::Value),
}

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

/// Returns a print.
pub async fn delete_print(
    configuration: &configuration::Configuration,
    print_id: &str,
) -> Result<(), Error<DeletePrintError>> {
    // add a prefix to parameters to efficiently prevent name collisions
    let p_path_print_id = print_id;

    let uri_str = format!(
        "{}/prints/{printId}",
        configuration.base_path,
        printId = crate::apis::urlencode(p_path_print_id)
    );
    let mut req_builder = configuration
        .client
        .request(reqwest::Method::DELETE, &uri_str);

    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();

    if !status.is_client_error() && !status.is_server_error() {
        Ok(())
    } else {
        let content = resp.text().await?;
        let entity: Option<DeletePrintError> = serde_json::from_str(&content).ok();
        Err(Error::ResponseError(ResponseContent {
            status,
            content,
            entity,
        }))
    }
}

/// Edits a print.
pub async fn edit_print(
    configuration: &configuration::Configuration,
    print_id: &str,
    image: crate::patches::better_file_upload::File<'_>,
    note: Option<&str>,
) -> Result<models::Print, Error<EditPrintError>> {
    // add a prefix to parameters to efficiently prevent name collisions
    let p_path_print_id = print_id;
    let p_form_image = image;
    let p_form_note = note;

    let uri_str = format!(
        "{}/prints/{printId}",
        configuration.base_path,
        printId = crate::apis::urlencode(p_path_print_id)
    );
    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();
    multipart_form = multipart_form.part("image", p_form_image.get_multipart().await?);
    if let Some(param_value) = p_form_note {
        multipart_form = multipart_form.text("note", param_value.to_string());
    }
    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::Print`"))),
            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::Print`")))),
        }
    } else {
        let content = resp.text().await?;
        let entity: Option<EditPrintError> = serde_json::from_str(&content).ok();
        Err(Error::ResponseError(ResponseContent {
            status,
            content,
            entity,
        }))
    }
}

/// Returns a print.
pub async fn get_print(
    configuration: &configuration::Configuration,
    print_id: &str,
) -> Result<models::Print, Error<GetPrintError>> {
    // add a prefix to parameters to efficiently prevent name collisions
    let p_path_print_id = print_id;

    let uri_str = format!(
        "{}/prints/{printId}",
        configuration.base_path,
        printId = crate::apis::urlencode(p_path_print_id)
    );
    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);

    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 `models::Print`"))),
            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::Print`")))),
        }
    } else {
        let content = resp.text().await?;
        let entity: Option<GetPrintError> = serde_json::from_str(&content).ok();
        Err(Error::ResponseError(ResponseContent {
            status,
            content,
            entity,
        }))
    }
}

/// Returns a list of all prints of the user. User id has to be your own userId, as you can't request other user's prints.
pub async fn get_user_prints(
    configuration: &configuration::Configuration,
    user_id: &str,
) -> Result<Vec<models::Print>, Error<GetUserPrintsError>> {
    // add a prefix to parameters to efficiently prevent name collisions
    let p_path_user_id = user_id;

    let uri_str = format!(
        "{}/prints/user/{userId}",
        configuration.base_path,
        userId = crate::apis::urlencode(p_path_user_id)
    );
    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);

    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 `Vec&lt;models::Print&gt;`"))),
            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `Vec&lt;models::Print&gt;`")))),
        }
    } else {
        let content = resp.text().await?;
        let entity: Option<GetUserPrintsError> = serde_json::from_str(&content).ok();
        Err(Error::ResponseError(ResponseContent {
            status,
            content,
            entity,
        }))
    }
}

/// Uploads and creates a print.
pub async fn upload_print(
    configuration: &configuration::Configuration,
    image: crate::patches::better_file_upload::File<'_>,
    timestamp: String,
    note: Option<&str>,
    world_id: Option<&str>,
    world_name: Option<&str>,
) -> Result<models::Print, Error<UploadPrintError>> {
    // add a prefix to parameters to efficiently prevent name collisions
    let p_form_image = image;
    let p_form_timestamp = timestamp;
    let p_form_note = note;
    let p_form_world_id = world_id;
    let p_form_world_name = world_name;

    let uri_str = format!("{}/prints", 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();
    multipart_form = multipart_form.part("image", p_form_image.get_multipart().await?);
    if let Some(param_value) = p_form_note {
        multipart_form = multipart_form.text("note", param_value.to_string());
    }
    macro_rules! serialize {
        ($form:ident, data, $data:ident) => {
            $form = $form.text("data", serde_json::to_string(&$data)?);
        };
        ($form:ident, $tag:ident, $data:ident) => {
            $form = $form.text(stringify!($tag), $data.to_string());
        };
    }
    serialize!(multipart_form, timestamp, p_form_timestamp);
    if let Some(param_value) = p_form_world_id {
        multipart_form = multipart_form.text("worldId", param_value.to_string());
    }
    if let Some(param_value) = p_form_world_name {
        multipart_form = multipart_form.text("worldName", param_value.to_string());
    }
    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::Print`"))),
            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::Print`")))),
        }
    } else {
        let content = resp.text().await?;
        let entity: Option<UploadPrintError> = serde_json::from_str(&content).ok();
        Err(Error::ResponseError(ResponseContent {
            status,
            content,
            entity,
        }))
    }
}