sigstore 0.14.0

An experimental crate to interact with sigstore
Documentation
/*
 * Rekor
 *
 * Rekor is a cryptographically secure, immutable transparency log for signed software releases.
 *
 * The version of the OpenAPI document: 0.0.1
 *
 * Generated by: https://openapi-generator.tech
 */

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

/// struct for typed errors of method [`get_log_info`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum GetLogInfoError {
    DefaultResponse(crate::rekor::models::Error),
    UnknownValue(serde_json::Value),
    ConversionError(String),
}

/// struct for typed errors of method [`get_log_proof`]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum GetLogProofError {
    Status400(crate::rekor::models::Error),
    DefaultResponse(crate::rekor::models::Error),
    UnknownValue(serde_json::Value),
    ConversionError(String),
}

/// Fetches the current state of the Rekor transparency log.
///
/// It queries the Rekor API for the latest log information, and returns a
/// [`LogInfo`](crate::rekor::models::LogInfo).
///
/// Returns an error if the HTTP request fails, the response cannot be parsed, or the log info
/// cannot be converted from the raw API format.
pub async fn get_log_info(
    configuration: &configuration::Configuration,
) -> Result<crate::rekor::models::LogInfo, Error<GetLogInfoError>> {
    let local_var_configuration = configuration;

    let local_var_client = &local_var_configuration.client;

    let local_var_uri_str = format!("{}/api/v1/log", local_var_configuration.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_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());
    }

    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() {
        let raw: crate::rekor::models::RekorLogInfo =
            serde_json::from_str(&local_var_content).map_err(Error::from)?;
        let proof = crate::rekor::models::LogInfo::try_from(raw).map_err(|e| {
            Error::ResponseError(ResponseContent {
                status: local_var_status,
                content: local_var_content.clone(),
                entity: Some(GetLogInfoError::ConversionError(e.to_string())),
            })
        })?;
        Ok(proof)
    } else {
        let local_var_entity: Option<GetLogInfoError> =
            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))
    }
}

/// Fetches a Merkle consistency proof between two tree sizes from the Rekor transparency log.
///
/// It queries the Rekor API for a consistency proof, returned in a
/// [`ConsistencyProof`](crate::rekor::models::ConsistencyProof).
///
/// # Arguments
/// * `configuration` - Rekor API client configuration.
/// * `last_size` - The size of the newer tree (as an integer).
/// * `first_size` - The size of the older tree (as a string, optional).
/// * `tree_id` - The tree ID to query (optional).
///
/// Returns an error if the HTTP request fails, the response cannot be parsed, or the proof
/// cannot be converted from the raw API format.
pub async fn get_log_proof(
    configuration: &configuration::Configuration,
    last_size: i32,
    first_size: Option<&str>,
    tree_id: Option<&str>,
) -> Result<crate::rekor::models::ConsistencyProof, Error<GetLogProofError>> {
    let local_var_configuration = configuration;

    let local_var_client = &local_var_configuration.client;

    let local_var_uri_str = format!("{}/api/v1/log/proof", local_var_configuration.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) = first_size {
        local_var_req_builder =
            local_var_req_builder.query(&[("firstSize", &local_var_str.to_string())]);
    }
    local_var_req_builder = local_var_req_builder.query(&[("lastSize", &last_size.to_string())]);
    if let Some(ref local_var_str) = tree_id {
        local_var_req_builder =
            local_var_req_builder.query(&[("treeID", &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());
    }

    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() {
        let raw: crate::rekor::models::RekorConsistencyProof =
            serde_json::from_str(&local_var_content).map_err(Error::from)?;
        let proof = crate::rekor::models::ConsistencyProof::try_from(raw).map_err(|e| {
            Error::ResponseError(ResponseContent {
                status: local_var_status,
                content: local_var_content.clone(),
                entity: Some(GetLogProofError::ConversionError(e.to_string())),
            })
        })?;
        Ok(proof)
    } else {
        let local_var_entity: Option<GetLogProofError> =
            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))
    }
}