#![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 Oidc<'api, C: Client> where AdapterError: From<<C as Client>::Err> {
client: &'api C
}
pub fn new<C: Client>(client: &C) -> Oidc<C> where AdapterError: From<<C as Client>::Err> {
Oidc { client }
}
#[derive(Debug, thiserror::Error)]
pub enum OidcGetOidcCustomSubTemplateForOrgError {
#[error("Status code: {}", code)]
Generic { code: u16 },
}
impl From<OidcGetOidcCustomSubTemplateForOrgError> for AdapterError {
fn from(err: OidcGetOidcCustomSubTemplateForOrgError) -> Self {
let (description, status_code) = match err {
OidcGetOidcCustomSubTemplateForOrgError::Generic { code } => (String::from("Generic"), code)
};
Self::Endpoint {
description,
status_code,
source: Some(Box::new(err))
}
}
}
#[derive(Debug, thiserror::Error)]
pub enum OidcUpdateOidcCustomSubTemplateForOrgError {
#[error("Resource not found")]
Status404(BasicError),
#[error("Forbidden")]
Status403(BasicError),
#[error("Status code: {}", code)]
Generic { code: u16 },
}
impl From<OidcUpdateOidcCustomSubTemplateForOrgError> for AdapterError {
fn from(err: OidcUpdateOidcCustomSubTemplateForOrgError) -> Self {
let (description, status_code) = match err {
OidcUpdateOidcCustomSubTemplateForOrgError::Status404(_) => (String::from("Resource not found"), 404),
OidcUpdateOidcCustomSubTemplateForOrgError::Status403(_) => (String::from("Forbidden"), 403),
OidcUpdateOidcCustomSubTemplateForOrgError::Generic { code } => (String::from("Generic"), code)
};
Self::Endpoint {
description,
status_code,
source: Some(Box::new(err))
}
}
}
impl<'api, C: Client> Oidc<'api, C> where AdapterError: From<<C as Client>::Err> {
pub async fn get_oidc_custom_sub_template_for_org_async(&self, org: &str) -> Result<OidcCustomSub, AdapterError> {
let request_uri = format!("{}/orgs/{}/actions/oidc/customization/sub", super::GITHUB_BASE_API_URL, org);
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() {
code => Err(OidcGetOidcCustomSubTemplateForOrgError::Generic { code }.into()),
}
}
}
#[cfg(not(target_arch = "wasm32"))]
pub fn get_oidc_custom_sub_template_for_org(&self, org: &str) -> Result<OidcCustomSub, AdapterError> {
let request_uri = format!("{}/orgs/{}/actions/oidc/customization/sub", super::GITHUB_BASE_API_URL, org);
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() {
code => Err(OidcGetOidcCustomSubTemplateForOrgError::Generic { code }.into()),
}
}
}
pub async fn update_oidc_custom_sub_template_for_org_async(&self, org: &str, body: OidcCustomSub) -> Result<EmptyObject, AdapterError> {
let request_uri = format!("{}/orgs/{}/actions/oidc/customization/sub", super::GITHUB_BASE_API_URL, org);
let req = GitHubRequest {
uri: request_uri,
body: Some(C::from_json::<OidcCustomSub>(body)?),
method: "PUT",
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(OidcUpdateOidcCustomSubTemplateForOrgError::Status404(github_response.to_json_async().await?).into()),
403 => Err(OidcUpdateOidcCustomSubTemplateForOrgError::Status403(github_response.to_json_async().await?).into()),
code => Err(OidcUpdateOidcCustomSubTemplateForOrgError::Generic { code }.into()),
}
}
}
#[cfg(not(target_arch = "wasm32"))]
pub fn update_oidc_custom_sub_template_for_org(&self, org: &str, body: OidcCustomSub) -> Result<EmptyObject, AdapterError> {
let request_uri = format!("{}/orgs/{}/actions/oidc/customization/sub", super::GITHUB_BASE_API_URL, org);
let req = GitHubRequest {
uri: request_uri,
body: Some(C::from_json::<OidcCustomSub>(body)?),
method: "PUT",
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(OidcUpdateOidcCustomSubTemplateForOrgError::Status404(github_response.to_json()?).into()),
403 => Err(OidcUpdateOidcCustomSubTemplateForOrgError::Status403(github_response.to_json()?).into()),
code => Err(OidcUpdateOidcCustomSubTemplateForOrgError::Generic { code }.into()),
}
}
}
}