/*
 * Mithril
 *
 * Mithril Compute API
 *
 * The version of the OpenAPI document: 1.0.0
 *
 * Generated by: https://openapi-generator.tech
 */

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

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

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

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

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

/// Create a new Kubernetes cluster
pub async fn create_kubernetes_cluster_v2_kubernetes_clusters_post(
    configuration: &configuration::Configuration,
    create_kubernetes_cluster_request: models::CreateKubernetesClusterRequest,
) -> Result<
    models::KubernetesClusterModel,
    Error<CreateKubernetesClusterV2KubernetesClustersPostError>,
> {
    // add a prefix to parameters to efficiently prevent name collisions
    let p_body_create_kubernetes_cluster_request = create_kubernetes_cluster_request;

    let uri_str = format!("{}/v2/kubernetes/clusters", 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());
    };
    req_builder = req_builder.json(&p_body_create_kubernetes_cluster_request);

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

/// Delete a Kubernetes cluster
pub async fn delete_kubernetes_cluster_v2_kubernetes_clusters_cluster_fid_delete(
    configuration: &configuration::Configuration,
    cluster_fid: &str,
) -> Result<(), Error<DeleteKubernetesClusterV2KubernetesClustersClusterFidDeleteError>> {
    // add a prefix to parameters to efficiently prevent name collisions
    let p_path_cluster_fid = cluster_fid;

    let uri_str = format!(
        "{}/v2/kubernetes/clusters/{cluster_fid}",
        configuration.base_path,
        cluster_fid = crate::apis::urlencode(p_path_cluster_fid)
    );
    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());
    }
    if let Some(ref token) = configuration.bearer_access_token {
        req_builder = req_builder.bearer_auth(token.to_owned());
    };

    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<DeleteKubernetesClusterV2KubernetesClustersClusterFidDeleteError> =
            serde_json::from_str(&content).ok();
        Err(Error::ResponseError(ResponseContent {
            status,
            content,
            entity,
        }))
    }
}

/// Get a specific Kubernetes cluster
pub async fn get_kubernetes_cluster_v2_kubernetes_clusters_cluster_fid_get(
    configuration: &configuration::Configuration,
    cluster_fid: &str,
) -> Result<
    models::KubernetesClusterModel,
    Error<GetKubernetesClusterV2KubernetesClustersClusterFidGetError>,
> {
    // add a prefix to parameters to efficiently prevent name collisions
    let p_path_cluster_fid = cluster_fid;

    let uri_str = format!(
        "{}/v2/kubernetes/clusters/{cluster_fid}",
        configuration.base_path,
        cluster_fid = crate::apis::urlencode(p_path_cluster_fid)
    );
    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());
    }
    if let Some(ref token) = configuration.bearer_access_token {
        req_builder = req_builder.bearer_auth(token.to_owned());
    };

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

/// Get all Kubernetes clusters for a project
pub async fn get_kubernetes_clusters_v2_kubernetes_clusters_get(
    configuration: &configuration::Configuration,
    project: &str,
) -> Result<
    Vec<models::KubernetesClusterModel>,
    Error<GetKubernetesClustersV2KubernetesClustersGetError>,
> {
    // add a prefix to parameters to efficiently prevent name collisions
    let p_query_project = project;

    let uri_str = format!("{}/v2/kubernetes/clusters", configuration.base_path);
    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);

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