Skip to content

Commit

Permalink
Merge branch 'tokens' into admin_client
Browse files Browse the repository at this point in the history
  • Loading branch information
jbtrystram authored Mar 30, 2022
2 parents adb8b57 + 96c6076 commit f1c48b1
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ pub mod tokens;

mod serde;
mod translator;
mod util;
pub mod util;

pub use translator::*;
27 changes: 24 additions & 3 deletions src/tokens/v1/client.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::data::*;
use crate::error::ClientError;
use crate::openid::TokenProvider;
use crate::openid::{TokenInjector, TokenProvider};
use crate::util::Client;
use std::fmt::Debug;
use tracing::instrument;
Expand Down Expand Up @@ -77,8 +77,29 @@ where
///
/// The result will contain the full token. This value is only available once.
#[instrument]
pub async fn create_token(&self) -> ClientResult<Option<CreatedAccessToken>> {
self.create(self.url(Some(""))?, None::<()>).await
pub async fn create_token<D>(
&self,
description: Option<D>,
) -> ClientResult<Option<CreatedAccessToken>>
where
D: AsRef<str> + Debug,
{
// here we need to override the trait method as we need to add a query parameter with the description.
let url = self.url(Some(""))?;

let mut query: Vec<(&str, &str)> = Vec::new();
if let Some(description) = description.as_ref() {
query.push(("description", description.as_ref()))
}

let req = self
.client()
.post(url)
.query(&query)
.inject_token(self.token_provider())
.await?;

Self::create_response(req.send().await?).await
}

/// Delete an existing token for this user.
Expand Down
28 changes: 23 additions & 5 deletions src/util/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ where
/// The correct authentication and tracing headers will be added to the request.
async fn read<T>(&self, url: Url) -> Result<Option<T>, ClientError<reqwest::Error>>
where
Self: std::marker::Send,
T: DeserializeOwned,
{
let req = self
Expand All @@ -39,8 +40,12 @@ where
.inject_token(self.token_provider())
.await?;

let response = req.send().await?;
Self::read_response(req.send().await?).await
}

async fn read_response<T: DeserializeOwned>(
response: Response,
) -> Result<Option<T>, ClientError<reqwest::Error>> {
log::debug!("Eval get response: {:#?}", response);
match response.status() {
StatusCode::OK => Ok(Some(response.json().await?)),
Expand All @@ -61,6 +66,7 @@ where
payload: Option<A>,
) -> Result<bool, ClientError<reqwest::Error>>
where
Self: std::marker::Send,
A: Serialize + Send + Sync,
{
let req = if let Some(p) = payload {
Expand All @@ -72,8 +78,10 @@ where
.inject_token(self.token_provider())
.await?;

let response = req.send().await?;
Self::update_response(req.send().await?).await
}

async fn update_response(response: Response) -> Result<bool, ClientError<reqwest::Error>> {
log::debug!("Eval update response: {:#?}", response);
match response.status() {
StatusCode::OK | StatusCode::NO_CONTENT | StatusCode::ACCEPTED => Ok(true),
Expand All @@ -87,15 +95,20 @@ where
/// The resource must exist, otherwise `false` is returned.
///
/// The correct authentication and tracing headers will be added to the request.
async fn delete(&self, url: Url) -> Result<bool, ClientError<reqwest::Error>> {
async fn delete(&self, url: Url) -> Result<bool, ClientError<reqwest::Error>>
where
Self: std::marker::Send,
{
let req = self
.client()
.delete(url)
.inject_token(self.token_provider())
.await?;

let response = req.send().await?;
Self::delete_response(req.send().await?).await
}

async fn delete_response(response: Response) -> Result<bool, ClientError<reqwest::Error>> {
log::debug!("Eval delete response: {:#?}", response);
match response.status() {
StatusCode::OK | StatusCode::NO_CONTENT => Ok(true),
Expand All @@ -113,6 +126,7 @@ where
payload: Option<P>,
) -> Result<Option<T>, ClientError<reqwest::Error>>
where
Self: std::marker::Send,
P: Serialize + Send + Sync,
T: DeserializeOwned,
{
Expand All @@ -125,8 +139,12 @@ where
.inject_token(self.token_provider())
.await?;

let response = req.send().await?;
Self::create_response(req.send().await?).await
}

async fn create_response<T: DeserializeOwned>(
response: Response,
) -> Result<Option<T>, ClientError<reqwest::Error>> {
log::debug!("Eval create response: {:#?}", response);
match response.status() {
StatusCode::CREATED => Ok(None),
Expand Down

0 comments on commit f1c48b1

Please sign in to comment.