Skip to content

Commit

Permalink
Several improvements to the client
Browse files Browse the repository at this point in the history
Signed-off-by: Ruben Nijveld <[email protected]>
  • Loading branch information
rnijveld committed Jan 28, 2025
1 parent eec7251 commit fdef262
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 23 deletions.
6 changes: 3 additions & 3 deletions openleadr-client/src/bin/everest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use openleadr_wire::{
values_map::Value,
};

use openleadr_client::{ProgramClient, Timeline};
use openleadr_client::{Filter, ProgramClient, Timeline};
use std::{error::Error, time::Duration};
use tokio::{
select,
Expand Down Expand Up @@ -66,14 +66,14 @@ async fn main() -> Result<(), Box<dyn Error>> {
}

async fn poll_timeline(
mut program: ProgramClient,
program: ProgramClient,
poll_interval: Duration,
sender: Sender<Timeline>,
) -> Result<(), openleadr_client::Error> {
loop {
tokio::time::sleep(poll_interval).await;

let timeline = program.get_timeline().await?;
let timeline = program.get_timeline(Filter::none()).await?;

let Ok(_) = sender.send(timeline).await else {
return Ok(());
Expand Down
2 changes: 1 addition & 1 deletion openleadr-client/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use openleadr_wire::{event::EventContent, report::ReportContent, Event, Report};
/// event.update().await.unwrap()
/// # })
/// ```
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct EventClient {
client: Arc<ClientRef>,
data: Event,
Expand Down
29 changes: 18 additions & 11 deletions openleadr-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ pub trait HttpClient: Debug {
/// Can be used to implement both, the VEN and the business logic.
///
/// If using the VTN of this project with the built-in OAuth authentication provider,
/// the [`Client`] also allows managing the users.
/// the [`Client`] also allows managing the users.
#[derive(Debug, Clone)]
pub struct Client {
client_ref: Arc<ClientRef>,
Expand Down Expand Up @@ -407,24 +407,31 @@ pub struct PaginationOptions {
/// There has been some discussion with the authors of the standard in
/// <https://github.com/oadr3-org/openadr3-vtn-reference-implementation/issues/83> (sadly not public).
#[derive(Debug, Clone)]
pub enum Filter<'a> {
pub enum Filter<'a, S: AsRef<str>> {
/// Do not apply any filtering
None,
/// Filter by [`TargetType`] and a list of values.
///
/// It will be encoded to the request as query parameters,
/// e.g., `/programs?targetType=GROUP&targetValues=Group-1&targetValues=Group-2`.
By(TargetType, &'a [&'a str]),
By(TargetType, &'a [S]),
}

impl<'a> Filter<'a> {
impl<'a> Filter<'a, &'static str> {
/// Create a new filter that does not apply any filtering.
pub const fn none() -> Filter<'a, &'static str> {
Filter::None
}
}

impl<'a, S: AsRef<str>> Filter<'a, S> {
pub(crate) fn to_query_params(&'a self) -> Vec<(&'a str, &'a str)> {
let mut query = vec![];
if let Filter::By(ref target_label, target_values) = self {
query.push(("targetType", target_label.as_str()));

for target_value in *target_values {
query.push(("targetValues", *target_value));
query.push(("targetValues", target_value.as_ref()));
}
}
query
Expand Down Expand Up @@ -501,7 +508,7 @@ impl Client {
/// Lowlevel operation that gets a list of programs from the VTN with the given query parameters
pub async fn get_programs(
&self,
filter: Filter<'_>,
filter: Filter<'_, impl AsRef<str>>,
pagination: PaginationOptions,
) -> Result<Vec<ProgramClient>> {
// convert query params
Expand All @@ -523,7 +530,7 @@ impl Client {
/// Get all programs from the VTN with the given query parameters
///
/// It automatically tries to iterate pages where necessary.
pub async fn get_program_list(&self, filter: Filter<'_>) -> Result<Vec<ProgramClient>> {
pub async fn get_program_list(&self, filter: Filter<'_, impl AsRef<str> + Clone>) -> Result<Vec<ProgramClient>> {
self.client_ref
.iterate_pages(|skip, limit| {
self.get_programs(filter.clone(), PaginationOptions { skip, limit })
Expand All @@ -547,7 +554,7 @@ impl Client {
pub async fn get_events(
&self,
program_id: Option<&ProgramId>,
filter: Filter<'_>,
filter: Filter<'_, impl AsRef<str>>,
pagination: PaginationOptions,
) -> Result<Vec<EventClient>> {
// convert query params
Expand Down Expand Up @@ -576,7 +583,7 @@ impl Client {
pub async fn get_event_list(
&self,
program_id: Option<&ProgramId>,
filter: Filter<'_>,
filter: Filter<'_, impl AsRef<str> + Clone>,
) -> Result<Vec<EventClient>> {
self.client_ref
.iterate_pages(|skip, limit| {
Expand Down Expand Up @@ -609,7 +616,7 @@ impl Client {
&self,
skip: usize,
limit: usize,
filter: Filter<'_>,
filter: Filter<'_, impl AsRef<str>>,
) -> Result<Vec<VenClient>> {
let skip_str = skip.to_string();
let limit_str = limit.to_string();
Expand All @@ -628,7 +635,7 @@ impl Client {
/// Get all VENs from the VTN with the given query parameters.
///
/// The client automatically tries to iterate pages where necessary.
pub async fn get_ven_list(&self, filter: Filter<'_>) -> Result<Vec<VenClient>> {
pub async fn get_ven_list(&self, filter: Filter<'_, impl AsRef<str> + Clone>) -> Result<Vec<VenClient>> {
self.client_ref
.iterate_pages(|skip, limit| self.get_vens(skip, limit, filter.clone()))
.await
Expand Down
10 changes: 5 additions & 5 deletions openleadr-client/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use openleadr_wire::{

/// A client for interacting with the data in a specific program and the events
/// contained in the program.
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct ProgramClient {
client: Client,
data: Program,
Expand Down Expand Up @@ -104,7 +104,7 @@ impl ProgramClient {
/// To automatically iterate pages, use [`self.get_event_list`]
pub async fn get_events_request(
&self,
filter: Filter<'_>,
filter: Filter<'_, impl AsRef<str>>,
pagination: PaginationOptions,
) -> Result<Vec<EventClient>> {
self.client
Expand All @@ -113,13 +113,13 @@ impl ProgramClient {
}

/// Get a list of events from the VTN with the given query parameters
pub async fn get_event_list(&self, filter: Filter<'_>) -> Result<Vec<EventClient>> {
pub async fn get_event_list(&self, filter: Filter<'_, impl AsRef<str> + Clone>) -> Result<Vec<EventClient>> {
self.client.get_event_list(Some(self.id()), filter).await
}

/// Retrieves the events for this program from the VTN and tries to build a [`Timeline`] from it.
pub async fn get_timeline(&mut self) -> Result<Timeline> {
let events = self.get_event_list(Filter::None).await?;
pub async fn get_timeline(&self, filter: Filter<'_, impl AsRef<str> + Clone>) -> Result<Timeline> {
let events = self.get_event_list(filter).await?;
let events = events.iter().map(|e| e.content()).collect();
Timeline::from_events(&self.data, events).ok_or(Error::InvalidInterval)
}
Expand Down
2 changes: 1 addition & 1 deletion openleadr-client/src/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::{error::Result, ClientRef};
/// report.update().await.unwrap()
/// # })
/// ```
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct ReportClient {
client: Arc<ClientRef>,
data: Report,
Expand Down
2 changes: 1 addition & 1 deletion openleadr-client/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::sync::Arc;
/// stored as a child element of a VEN on the VTN.
///
/// To retrieve or create a resource, refer to the [`VenClient`](crate::VenClient).
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct ResourceClient {
client: Arc<ClientRef>,
ven_id: VenId,
Expand Down
2 changes: 1 addition & 1 deletion openleadr-client/src/ven.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use openleadr_wire::{
use std::sync::Arc;

/// A client for interacting with the data in a specific VEN and the resources contained in the VEN.
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct VenClient {
client: Arc<ClientRef>,
data: Ven,
Expand Down

0 comments on commit fdef262

Please sign in to comment.