roctogen 0.50.0

Github API and models generated from the official swagger OpenAPI specification
Documentation
//! Method, error and parameter types for the Credentials endpoint.
#![allow(
    clippy::all
)]
/* 
 * GitHub v3 REST API
 *
 * GitHub's v3 REST API.
 *
 * OpenAPI spec version: 1.1.4
 * 
 * Generated by: https://github.com/swagger-api/swagger-codegen.git
 */

use serde::Deserialize;

use roctokit::adapters::{AdapterError, Client, GitHubRequest, GitHubResponseExt};
use crate::models::*;

use super::PerPage;

use std::collections::HashMap;
use serde_json::value::Value;

pub struct Credentials<'api, C: Client> where AdapterError: From<<C as Client>::Err> {
    client: &'api C
}

pub fn new<C: Client>(client: &C) -> Credentials<C> where AdapterError: From<<C as Client>::Err> {
    Credentials { client }
}

/// Errors for the [Revoke a list of credentials](Credentials::revoke_async()) endpoint.
#[derive(Debug, thiserror::Error)]
pub enum CredentialsRevokeError {
    #[error("Validation failed, or the endpoint has been spammed.")]
    Status422(ValidationErrorSimple),
    #[error("Internal Error")]
    Status500(BasicError),
    #[error("Status code: {}", code)]
    Generic { code: u16 },
}

impl From<CredentialsRevokeError> for AdapterError {
    fn from(err: CredentialsRevokeError) -> Self {
        let (description, status_code) = match err {
            CredentialsRevokeError::Status422(_) => (String::from("Validation failed, or the endpoint has been spammed."), 422),
            CredentialsRevokeError::Status500(_) => (String::from("Internal Error"), 500),
            CredentialsRevokeError::Generic { code } => (String::from("Generic"), code)
        };

        Self::Endpoint {
            description,
            status_code,
            source: Some(Box::new(err))
        }
    }
}



impl<'api, C: Client> Credentials<'api, C> where AdapterError: From<<C as Client>::Err> {
    /// ---
    ///
    /// # Revoke a list of credentials
    ///
    /// Submit a list of credentials to be revoked. This endpoint is intended to revoke credentials the caller does not own and may have found exposed on GitHub.com or elsewhere. It can also be used for credentials associated with an old user account that you no longer have access to. Credential owners will be notified of the revocation.
    /// 
    /// This endpoint currently accepts the following credential types:
    /// - Personal access tokens (classic)
    /// - Fine-grained personal access tokens
    /// 
    /// Revoked credentials may impact users on GitHub Free, Pro, & Team and GitHub Enterprise Cloud, and GitHub Enterprise Cloud with Enterprise Managed Users.
    /// GitHub cannot reactivate any credentials that have been revoked; new credentials will need to be generated.
    /// 
    /// To prevent abuse, this API is limited to only 60 unauthenticated requests per hour and a max of 1000 tokens per API request.
    /// 
    /// > [!NOTE]
    /// > Any authenticated requests will return a 403.
    ///
    /// [GitHub API docs for revoke](https://docs.github.com/rest/credentials/revoke#revoke-a-list-of-credentials)
    ///
    /// ---
    pub async fn revoke_async(&self, body: PostCredentialsRevoke) -> Result<HashMap<String, Value>, AdapterError> {

        let request_uri = format!("{}/credentials/revoke", super::GITHUB_BASE_API_URL);


        let req = GitHubRequest {
            uri: request_uri,
            body: Some(C::from_json::<PostCredentialsRevoke>(body)?),
            method: "POST",
            headers: vec![]
        };

        let request = self.client.build(req)?;

        // --

        let github_response = self.client.fetch_async(request).await?;

        // --

        if github_response.is_success() {
            Ok(github_response.to_json_async().await?)
        } else {
            match github_response.status_code() {
                422 => Err(CredentialsRevokeError::Status422(github_response.to_json_async().await?).into()),
                500 => Err(CredentialsRevokeError::Status500(github_response.to_json_async().await?).into()),
                code => Err(CredentialsRevokeError::Generic { code }.into()),
            }
        }
    }

    /// ---
    ///
    /// # Revoke a list of credentials
    ///
    /// Submit a list of credentials to be revoked. This endpoint is intended to revoke credentials the caller does not own and may have found exposed on GitHub.com or elsewhere. It can also be used for credentials associated with an old user account that you no longer have access to. Credential owners will be notified of the revocation.
    /// 
    /// This endpoint currently accepts the following credential types:
    /// - Personal access tokens (classic)
    /// - Fine-grained personal access tokens
    /// 
    /// Revoked credentials may impact users on GitHub Free, Pro, & Team and GitHub Enterprise Cloud, and GitHub Enterprise Cloud with Enterprise Managed Users.
    /// GitHub cannot reactivate any credentials that have been revoked; new credentials will need to be generated.
    /// 
    /// To prevent abuse, this API is limited to only 60 unauthenticated requests per hour and a max of 1000 tokens per API request.
    /// 
    /// > [!NOTE]
    /// > Any authenticated requests will return a 403.
    ///
    /// [GitHub API docs for revoke](https://docs.github.com/rest/credentials/revoke#revoke-a-list-of-credentials)
    ///
    /// ---
    #[cfg(not(target_arch = "wasm32"))]
    pub fn revoke(&self, body: PostCredentialsRevoke) -> Result<HashMap<String, Value>, AdapterError> {

        let request_uri = format!("{}/credentials/revoke", super::GITHUB_BASE_API_URL);


        let req = GitHubRequest {
            uri: request_uri,
            body: Some(C::from_json::<PostCredentialsRevoke>(body)?),
            method: "POST",
            headers: vec![]
        };

        let request = self.client.build(req)?;

        // --

        let github_response = self.client.fetch(request)?;

        // --

        if github_response.is_success() {
            Ok(github_response.to_json()?)
        } else {
            match github_response.status_code() {
                422 => Err(CredentialsRevokeError::Status422(github_response.to_json()?).into()),
                500 => Err(CredentialsRevokeError::Status500(github_response.to_json()?).into()),
                code => Err(CredentialsRevokeError::Generic { code }.into()),
            }
        }
    }

}