hcloud 0.25.0

Unofficial Rust crate for accessing the Hetzner Cloud API
Documentation
/*
 * Hetzner Cloud API
 *
 * Copied from the official API documentation for the Public Hetzner Cloud.
 *
 * The version of the OpenAPI document: 0.28.0
 *
 * Generated by: https://openapi-generator.tech
 */

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

/// struct for passing parameters to the method [`get_iso`]
#[derive(Clone, Debug, Default)]
pub struct GetIsoParams {
    /// ID of the ISO.
    pub id: i64,
}

/// struct for passing parameters to the method [`list_isos`]
#[derive(Clone, Debug, Default)]
pub struct ListIsosParams {
    /// Filter resources by their name. The response will only contain the resources matching exactly the specified name.
    pub name: Option<String>,
    /// Filter resources by cpu architecture. The response will only contain the resources with the specified cpu architecture.
    pub architecture: Option<String>,
    /// Include Images with wildcard architecture (architecture is null). Works only if architecture filter is specified.
    pub include_architecture_wildcard: Option<bool>,
    /// Page number to return. For more information, see \"Pagination\".
    pub page: Option<i64>,
    /// Maximum number of entries returned per page. For more information, see \"Pagination\".
    pub per_page: Option<i64>,
}

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

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

/// Returns a specific ISO object.
pub async fn get_iso(
    configuration: &configuration::Configuration,
    params: GetIsoParams,
) -> Result<models::GetIsoResponse, Error<GetIsoError>> {
    let local_var_configuration = configuration;

    // unbox the parameters
    let id = params.id;

    let local_var_client = &local_var_configuration.client;

    let local_base_path = local_var_configuration.get_base_path("https://api.hetzner.cloud/v1");
    let local_var_uri_str = format!("{}/isos/{id}", local_base_path, id = id);
    let mut local_var_req_builder =
        local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());

    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
        local_var_req_builder =
            local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
    }
    if let Some(ref local_var_token) = local_var_configuration.bearer_access_token {
        local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
    };

    let local_var_req = local_var_req_builder.build()?;
    let local_var_resp = local_var_client.execute(local_var_req).await?;

    let local_var_status = local_var_resp.status();
    let local_var_content = local_var_resp.text().await?;

    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
        serde_json::from_str(&local_var_content).map_err(Error::from)
    } else {
        let local_var_entity: Option<GetIsoError> = serde_json::from_str(&local_var_content).ok();
        let local_var_error = ResponseContent {
            status: local_var_status,
            content: local_var_content,
            entity: local_var_entity,
        };
        Err(Error::ResponseError(local_var_error))
    }
}

/// Returns all available ISO objects.
pub async fn list_isos(
    configuration: &configuration::Configuration,
    params: ListIsosParams,
) -> Result<models::ListIsosResponse, Error<ListIsosError>> {
    let local_var_configuration = configuration;

    // unbox the parameters
    let name = params.name;
    let architecture = params.architecture;
    let include_architecture_wildcard = params.include_architecture_wildcard;
    let page = params.page;
    let per_page = params.per_page;

    let local_var_client = &local_var_configuration.client;

    let local_base_path = local_var_configuration.get_base_path("https://api.hetzner.cloud/v1");
    let local_var_uri_str = format!("{}/isos", local_base_path);
    let mut local_var_req_builder =
        local_var_client.request(reqwest::Method::GET, local_var_uri_str.as_str());

    if let Some(ref local_var_str) = name {
        local_var_req_builder =
            local_var_req_builder.query(&[("name", &local_var_str.to_string())]);
    }
    if let Some(ref local_var_str) = architecture {
        local_var_req_builder =
            local_var_req_builder.query(&[("architecture", &local_var_str.to_string())]);
    }
    if let Some(ref local_var_str) = include_architecture_wildcard {
        local_var_req_builder = local_var_req_builder
            .query(&[("include_architecture_wildcard", &local_var_str.to_string())]);
    }
    if let Some(ref local_var_str) = page {
        local_var_req_builder =
            local_var_req_builder.query(&[("page", &local_var_str.to_string())]);
    }
    if let Some(ref local_var_str) = per_page {
        local_var_req_builder =
            local_var_req_builder.query(&[("per_page", &local_var_str.to_string())]);
    }
    if let Some(ref local_var_user_agent) = local_var_configuration.user_agent {
        local_var_req_builder =
            local_var_req_builder.header(reqwest::header::USER_AGENT, local_var_user_agent.clone());
    }
    if let Some(ref local_var_token) = local_var_configuration.bearer_access_token {
        local_var_req_builder = local_var_req_builder.bearer_auth(local_var_token.to_owned());
    };

    let local_var_req = local_var_req_builder.build()?;
    let local_var_resp = local_var_client.execute(local_var_req).await?;

    let local_var_status = local_var_resp.status();
    let local_var_content = local_var_resp.text().await?;

    if !local_var_status.is_client_error() && !local_var_status.is_server_error() {
        serde_json::from_str(&local_var_content).map_err(Error::from)
    } else {
        let local_var_entity: Option<ListIsosError> = serde_json::from_str(&local_var_content).ok();
        let local_var_error = ResponseContent {
            status: local_var_status,
            content: local_var_content,
            entity: local_var_entity,
        };
        Err(Error::ResponseError(local_var_error))
    }
}