roctogen 0.50.0

Github API and models generated from the official swagger OpenAPI specification
Documentation
//! Method, error and parameter types for the CodesOfConduct 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 CodesOfConduct<'api, C: Client> where AdapterError: From<<C as Client>::Err> {
    client: &'api C
}

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

/// Errors for the [Get all codes of conduct](CodesOfConduct::get_all_codes_of_conduct_async()) endpoint.
#[derive(Debug, thiserror::Error)]
pub enum CodesOfConductGetAllCodesOfConductError {
    #[error("Not modified")]
    Status304,
    #[error("Status code: {}", code)]
    Generic { code: u16 },
}

impl From<CodesOfConductGetAllCodesOfConductError> for AdapterError {
    fn from(err: CodesOfConductGetAllCodesOfConductError) -> Self {
        let (description, status_code) = match err {
            CodesOfConductGetAllCodesOfConductError::Status304 => (String::from("Not modified"), 304),
            CodesOfConductGetAllCodesOfConductError::Generic { code } => (String::from("Generic"), code)
        };

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

/// Errors for the [Get a code of conduct](CodesOfConduct::get_conduct_code_async()) endpoint.
#[derive(Debug, thiserror::Error)]
pub enum CodesOfConductGetConductCodeError {
    #[error("Resource not found")]
    Status404(BasicError),
    #[error("Not modified")]
    Status304,
    #[error("Status code: {}", code)]
    Generic { code: u16 },
}

impl From<CodesOfConductGetConductCodeError> for AdapterError {
    fn from(err: CodesOfConductGetConductCodeError) -> Self {
        let (description, status_code) = match err {
            CodesOfConductGetConductCodeError::Status404(_) => (String::from("Resource not found"), 404),
            CodesOfConductGetConductCodeError::Status304 => (String::from("Not modified"), 304),
            CodesOfConductGetConductCodeError::Generic { code } => (String::from("Generic"), code)
        };

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



impl<'api, C: Client> CodesOfConduct<'api, C> where AdapterError: From<<C as Client>::Err> {
    /// ---
    ///
    /// # Get all codes of conduct
    ///
    /// Returns array of all GitHub's codes of conduct.
    ///
    /// [GitHub API docs for get_all_codes_of_conduct](https://docs.github.com/rest/codes-of-conduct/codes-of-conduct#get-all-codes-of-conduct)
    ///
    /// ---
    pub async fn get_all_codes_of_conduct_async(&self) -> Result<Vec<CodeOfConduct>, AdapterError> {

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


        let req = GitHubRequest {
            uri: request_uri,
            body: None::<C::Body>,
            method: "GET",
            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() {
                304 => Err(CodesOfConductGetAllCodesOfConductError::Status304.into()),
                code => Err(CodesOfConductGetAllCodesOfConductError::Generic { code }.into()),
            }
        }
    }

    /// ---
    ///
    /// # Get all codes of conduct
    ///
    /// Returns array of all GitHub's codes of conduct.
    ///
    /// [GitHub API docs for get_all_codes_of_conduct](https://docs.github.com/rest/codes-of-conduct/codes-of-conduct#get-all-codes-of-conduct)
    ///
    /// ---
    #[cfg(not(target_arch = "wasm32"))]
    pub fn get_all_codes_of_conduct(&self) -> Result<Vec<CodeOfConduct>, AdapterError> {

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


        let req = GitHubRequest {
            uri: request_uri,
            body: None,
            method: "GET",
            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() {
                304 => Err(CodesOfConductGetAllCodesOfConductError::Status304.into()),
                code => Err(CodesOfConductGetAllCodesOfConductError::Generic { code }.into()),
            }
        }
    }

    /// ---
    ///
    /// # Get a code of conduct
    ///
    /// Returns information about the specified GitHub code of conduct.
    ///
    /// [GitHub API docs for get_conduct_code](https://docs.github.com/rest/codes-of-conduct/codes-of-conduct#get-a-code-of-conduct)
    ///
    /// ---
    pub async fn get_conduct_code_async(&self, key: &str) -> Result<CodeOfConduct, AdapterError> {

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


        let req = GitHubRequest {
            uri: request_uri,
            body: None::<C::Body>,
            method: "GET",
            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() {
                404 => Err(CodesOfConductGetConductCodeError::Status404(github_response.to_json_async().await?).into()),
                304 => Err(CodesOfConductGetConductCodeError::Status304.into()),
                code => Err(CodesOfConductGetConductCodeError::Generic { code }.into()),
            }
        }
    }

    /// ---
    ///
    /// # Get a code of conduct
    ///
    /// Returns information about the specified GitHub code of conduct.
    ///
    /// [GitHub API docs for get_conduct_code](https://docs.github.com/rest/codes-of-conduct/codes-of-conduct#get-a-code-of-conduct)
    ///
    /// ---
    #[cfg(not(target_arch = "wasm32"))]
    pub fn get_conduct_code(&self, key: &str) -> Result<CodeOfConduct, AdapterError> {

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


        let req = GitHubRequest {
            uri: request_uri,
            body: None,
            method: "GET",
            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() {
                404 => Err(CodesOfConductGetConductCodeError::Status404(github_response.to_json()?).into()),
                304 => Err(CodesOfConductGetConductCodeError::Status304.into()),
                code => Err(CodesOfConductGetConductCodeError::Generic { code }.into()),
            }
        }
    }

}