use async_trait::async_trait;
use crate::context::Context;
use crate::error::Error;
use crate::model::delete_grant_response::DeleteGrantResponse;
use crate::model::error_body::ErrorBody;
use crate::model::errors_body::ErrorsBody;
use crate::model::role::Role;
pub enum GrantError {
Error400(ErrorsBody),
Error401(ErrorBody),
Error404(ErrorBody),
Error500(ErrorBody),
}
#[async_trait]
pub trait GrantClient {
async fn get(&self, account_id: &str) -> Result<Vec<Role>, Error<GrantError>>;
async fn role_get(&self, account_id: &str, role: &Role) -> Result<Role, Error<GrantError>>;
async fn role_put(&self, account_id: &str, role: &Role) -> Result<Role, Error<GrantError>>;
async fn role_delete(&self, account_id: &str, role: &Role) -> Result<DeleteGrantResponse, Error<GrantError>>;
}
pub struct GrantClientLive {
pub context: Context,
}
#[async_trait]
impl GrantClient for GrantClientLive {
async fn get(&self, account_id: &str) -> Result<Vec<Role>, Error<GrantError>> {
let mut url = self.context.base_url.clone();
url.path_segments_mut().unwrap()
.push("v2")
.push("accounts")
.push(account_id)
.push("grants");
let mut request = self
.context
.client
.get(url.clone());
{
tracing::info!(method="get", endpoint="/v2/accounts/{account_id}/grants", url=url.to_string(), "get");
}
if let Some(token) = self.context.bearer_token() {
request = request.bearer_auth(token);
}
let response = request.send().await?;
let status = response.status().as_u16();
match status {
200 => {
Ok(response.json::<Vec<Role>>().await?)
}
400 => {
let body = response.json::<ErrorsBody>().await?;
Err(Error::Item(GrantError::Error400(body)))
}
401 => {
let body = response.json::<ErrorBody>().await?;
Err(Error::Item(GrantError::Error401(body)))
}
404 => {
let body = response.json::<ErrorBody>().await?;
Err(Error::Item(GrantError::Error404(body)))
}
500 => {
let body = response.json::<ErrorBody>().await?;
Err(Error::Item(GrantError::Error500(body)))
}
_ => Err(Error::unexpected(status, response.bytes().await?)),
}
}
async fn role_get(&self, account_id: &str, role: &Role) -> Result<Role, Error<GrantError>> {
let mut url = self.context.base_url.clone();
url.path_segments_mut().unwrap()
.push("v2")
.push("accounts")
.push(account_id)
.push("grants")
.push(&role.to_string());
let mut request = self
.context
.client
.get(url.clone());
{
tracing::info!(method="get", endpoint="/v2/accounts/{account_id}/grants/{role}", url=url.to_string(), "role_get");
}
if let Some(token) = self.context.bearer_token() {
request = request.bearer_auth(token);
}
let response = request.send().await?;
let status = response.status().as_u16();
match status {
200 => {
Ok(response.json::<Role>().await?)
}
400 => {
let body = response.json::<ErrorsBody>().await?;
Err(Error::Item(GrantError::Error400(body)))
}
401 => {
let body = response.json::<ErrorBody>().await?;
Err(Error::Item(GrantError::Error401(body)))
}
404 => {
let body = response.json::<ErrorBody>().await?;
Err(Error::Item(GrantError::Error404(body)))
}
500 => {
let body = response.json::<ErrorBody>().await?;
Err(Error::Item(GrantError::Error500(body)))
}
_ => Err(Error::unexpected(status, response.bytes().await?)),
}
}
async fn role_put(&self, account_id: &str, role: &Role) -> Result<Role, Error<GrantError>> {
let mut url = self.context.base_url.clone();
url.path_segments_mut().unwrap()
.push("v2")
.push("accounts")
.push(account_id)
.push("grants")
.push(&role.to_string());
let mut request = self
.context
.client
.put(url.clone());
{
tracing::info!(method="put", endpoint="/v2/accounts/{account_id}/grants/{role}", url=url.to_string(), "role_put");
}
if let Some(token) = self.context.bearer_token() {
request = request.bearer_auth(token);
}
let response = request.send().await?;
let status = response.status().as_u16();
match status {
200 => {
Ok(response.json::<Role>().await?)
}
400 => {
let body = response.json::<ErrorsBody>().await?;
Err(Error::Item(GrantError::Error400(body)))
}
401 => {
let body = response.json::<ErrorBody>().await?;
Err(Error::Item(GrantError::Error401(body)))
}
404 => {
let body = response.json::<ErrorBody>().await?;
Err(Error::Item(GrantError::Error404(body)))
}
500 => {
let body = response.json::<ErrorBody>().await?;
Err(Error::Item(GrantError::Error500(body)))
}
_ => Err(Error::unexpected(status, response.bytes().await?)),
}
}
async fn role_delete(&self, account_id: &str, role: &Role) -> Result<DeleteGrantResponse, Error<GrantError>> {
let mut url = self.context.base_url.clone();
url.path_segments_mut().unwrap()
.push("v2")
.push("accounts")
.push(account_id)
.push("grants")
.push(&role.to_string());
let mut request = self
.context
.client
.delete(url.clone());
{
tracing::info!(method="delete", endpoint="/v2/accounts/{account_id}/grants/{role}", url=url.to_string(), "role_delete");
}
if let Some(token) = self.context.bearer_token() {
request = request.bearer_auth(token);
}
let response = request.send().await?;
let status = response.status().as_u16();
match status {
200 => {
Ok(response.json::<DeleteGrantResponse>().await?)
}
400 => {
let body = response.json::<ErrorsBody>().await?;
Err(Error::Item(GrantError::Error400(body)))
}
401 => {
let body = response.json::<ErrorBody>().await?;
Err(Error::Item(GrantError::Error401(body)))
}
404 => {
let body = response.json::<ErrorBody>().await?;
Err(Error::Item(GrantError::Error404(body)))
}
500 => {
let body = response.json::<ErrorBody>().await?;
Err(Error::Item(GrantError::Error500(body)))
}
_ => Err(Error::unexpected(status, response.bytes().await?)),
}
}
}