Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more operations & refactor #6

Merged
merged 17 commits into from
Apr 1, 2022
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ pub mod meta;
#[cfg(feature = "openid")]
pub mod openid;
pub mod registry;
pub mod tokens;

mod serde;
mod translator;
pub mod util;

pub use translator::*;
189 changes: 53 additions & 136 deletions src/registry/v1/client.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
use super::data::*;
use crate::core::WithTracing;
use crate::openid::TokenProvider;
use crate::{error::ClientError, openid::TokenInjector, Translator};
use crate::util::Client;
use crate::{error::ClientError, Translator};
use futures::{stream, StreamExt, TryStreamExt};
use reqwest::{Response, StatusCode};
use serde::de::DeserializeOwned;
use std::fmt::Debug;
use tracing::instrument;
use url::Url;

/// A device registry client, backed by reqwest.
/// A device registry client
#[derive(Clone, Debug)]
pub struct Client<TP>
pub struct RegistryClient<TP>
jbtrystram marked this conversation as resolved.
Show resolved Hide resolved
where
TP: TokenProvider,
{
Expand All @@ -22,19 +20,33 @@ where

type ClientResult<T> = Result<T, ClientError<reqwest::Error>>;

impl<TP> Client<TP>
impl<TP> Client<TP> for RegistryClient<TP>
where
TP: TokenProvider,
{
/// Create a new client instance.
pub fn new(client: reqwest::Client, registry_url: Url, token_provider: TP) -> Self {
fn new(client: reqwest::Client, registry_url: Url, token_provider: TP) -> Self {
Self {
client,
registry_url,
token_provider,
}
}

fn client(&self) -> &reqwest::Client {
&self.client
}

fn token_provider(&self) -> &TP {
&self.token_provider
}
}

impl<TP> RegistryClient<TP>
where
TP: TokenProvider,
{
/// craft url for the registry
fn url(&self, application: Option<&str>, device: Option<&str>) -> ClientResult<Url> {
let mut url = self.registry_url.clone();

Expand Down Expand Up @@ -70,14 +82,7 @@ where
/// as a response instead of "forbidden".
#[instrument]
pub async fn list_apps(&self) -> ClientResult<Option<Vec<Application>>> {
let req = self
.client
.get(self.url(None, None)?)
.propagate_current_context()
.inject_token(&self.token_provider)
.await?;

Self::get_response(req.send().await?).await
self.read(self.url(None, None)?).await
}

/// Get an application by name.
Expand All @@ -92,14 +97,7 @@ where
where
A: AsRef<str> + Debug,
{
let req = self
.client
.get(self.url(Some(application.as_ref()), None)?)
.propagate_current_context()
.inject_token(&self.token_provider)
.await?;

Self::get_response(req.send().await?).await
self.read(self.url(Some(application.as_ref()), None)?).await
}

/// Get a device by name.
Expand All @@ -115,14 +113,8 @@ where
A: AsRef<str> + Debug,
D: AsRef<str> + Debug,
{
let req = self
.client
.get(self.url(Some(application.as_ref()), Some(device.as_ref()))?)
.propagate_current_context()
.inject_token(&self.token_provider)
.await?;

Self::get_response(req.send().await?).await
self.read(self.url(Some(application.as_ref()), Some(device.as_ref()))?)
.await
}

/// Get a list of devices.
Expand Down Expand Up @@ -158,15 +150,10 @@ where
A: AsRef<str> + Debug,
D: AsRef<str> + Debug,
{
let req = self
.client
.get(self.url(Some(application.as_ref()), Some(device.as_ref()))?)
.propagate_current_context()
.inject_token(&self.token_provider)
let device: Option<Device> = self
.read(self.url(Some(application.as_ref()), Some(device.as_ref()))?)
.await?;

let device: Option<Device> = Self::get_response(req.send().await?).await?;

if let Some(device) = device {
let gateways = if let Some(gw_sel) = device
.section::<DeviceSpecGatewaySelector>()
Expand All @@ -185,106 +172,56 @@ where
}
}

async fn get_response<T: DeserializeOwned>(response: Response) -> ClientResult<Option<T>> {
log::debug!("Eval get response: {:#?}", response);
match response.status() {
StatusCode::OK => Ok(Some(response.json().await?)),
StatusCode::NOT_FOUND => Ok(None),
_ => Self::default_response(response).await,
}
}

/// Update (overwrite) an application.
///
/// The application must exist, otherwise `false` is returned.
#[instrument]
pub async fn update_app(&self, application: &Application) -> ClientResult<bool> {
let req = self
.client
.put(self.url(Some(&application.metadata.name), None)?)
.json(&application)
.propagate_current_context()
.inject_token(&self.token_provider)
.await?;

Self::update_response(req.send().await?).await
self.update(
self.url(Some(application.metadata.name.as_str()), None)?,
application,
)
.await
}

/// Update (overwrite) a device.
///
/// The application must exist, otherwise `false` is returned.
#[instrument]
pub async fn update_device(&self, device: &Device) -> ClientResult<bool> {
let req = self
.client
.put(self.url(
Some(&device.metadata.application),
Some(&device.metadata.name),
)?)
.json(&device)
.propagate_current_context()
.inject_token(&self.token_provider)
.await?;

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

async fn update_response(response: Response) -> ClientResult<bool> {
log::debug!("Eval update response: {:#?}", response);
match response.status() {
StatusCode::OK | StatusCode::NO_CONTENT => Ok(true),
StatusCode::NOT_FOUND => Ok(false),
_ => Self::default_response(response).await,
}
}

async fn delete_response(response: Response) -> ClientResult<bool> {
log::debug!("Eval delete response: {:#?}", response);
match response.status() {
StatusCode::OK | StatusCode::NO_CONTENT => Ok(true),
StatusCode::NOT_FOUND => Ok(false),
_ => Self::default_response(response).await,
}
self.update(
self.url(
Some(device.metadata.application.as_str()),
Some(device.metadata.name.as_str()),
)?,
device,
)
.await
}

/// Create a new application.
#[instrument]
pub async fn create_app(&self, app: &Application) -> ClientResult<()> {
let req = self
.client
.post(self.url(None, None)?)
.json(&app)
.inject_token(&self.token_provider)
.await?;

Self::create_response(req.send().await?).await
pub async fn create_app(&self, app: &Application) -> ClientResult<Option<()>> {
self.create(self.url(None, None)?, Some(app)).await
}

#[instrument]
pub async fn delete_app<A>(&self, application: A) -> ClientResult<bool>
where
A: AsRef<str> + Debug,
{
let req = self
.client
.delete(self.url(Some(application.as_ref()), None)?)
.inject_token(&self.token_provider)
.await?;

Self::delete_response(req.send().await?).await
self.delete(self.url(Some(application.as_ref()), None)?)
.await
}

/// Create a new device.
#[instrument]
pub async fn create_device(&self, device: &Device) -> ClientResult<()> {
let req = self
.client
.post(self.url(Some(&device.metadata.application), Some(""))?)
.json(&device)
.inject_token(&self.token_provider)
.await?;

Self::create_response(req.send().await?).await
pub async fn create_device(&self, device: &Device) -> ClientResult<Option<()>> {
self.create(
self.url(Some(device.metadata.application.as_str()), Some(""))?,
Some(device),
)
.await
}

#[instrument]
Expand All @@ -293,28 +230,8 @@ where
A: AsRef<str> + Debug,
D: AsRef<str> + Debug,
{
let req = self
.client
.delete(self.url(Some(application.as_ref()), Some(device.as_ref()))?)
.inject_token(&self.token_provider)
.await?;

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

async fn create_response(response: Response) -> ClientResult<()> {
log::debug!("Eval create response: {:#?}", response);
match response.status() {
StatusCode::CREATED => Ok(()),
_ => Self::default_response(response).await,
}
}

async fn default_response<T>(response: Response) -> ClientResult<T> {
match response.status() {
code if code.is_client_error() => Err(ClientError::Service(response.json().await?)),
code => Err(ClientError::Request(format!("Unexpected code {:?}", code))),
}
self.delete(self.url(Some(application.as_ref()), Some(device.as_ref()))?)
.await
}
}

Expand All @@ -325,7 +242,7 @@ mod test {

#[test]
fn test_url_list() -> anyhow::Result<()> {
let client = Client::new(
let client = RegistryClient::new(
Default::default(),
Url::parse("http://localhost")?,
NoTokenProvider,
Expand All @@ -342,7 +259,7 @@ mod test {

#[test]
fn test_url_app() -> anyhow::Result<()> {
let client = Client::new(
let client = RegistryClient::new(
Default::default(),
Url::parse("http://localhost")?,
NoTokenProvider,
Expand Down
2 changes: 2 additions & 0 deletions src/tokens/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
//! Access Token client API.
pub mod v1;
Loading