#![allow(
clippy::all
)]
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 }
}
#[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))
}
}
}
#[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> {
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()),
}
}
}
#[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()),
}
}
}
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()),
}
}
}
#[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()),
}
}
}
}