Skip to content
This repository has been archived by the owner on Oct 3, 2024. It is now read-only.

simplify client program api #93

Merged
merged 1 commit into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions openadr-client/src/bin/cli.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use openadr_client::{ClientCredentials, Target};
use openadr_client::ClientCredentials;
use openadr_wire::program::ProgramContent;

#[tokio::main]
Expand All @@ -9,7 +9,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
);
let _created_program = client.create_program(ProgramContent::new("name")).await?;
// let created_program_1 = client.create_program(ProgramContent::new("name1")).await?;
let program = client.get_program(Target::Program("name")).await?;
let program = client.get_program_by_name("name").await?;
// let created_event = program
// .create_event(program.new_event().with_event_name("prices3").with_priority(0))
// .await?;
Expand Down
4 changes: 2 additions & 2 deletions openadr-client/src/bin/everest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use openadr_wire::{
values_map::Value,
};

use openadr_client::{ProgramClient, Target, Timeline};
use openadr_client::{ProgramClient, Timeline};
use std::{error::Error, time::Duration};
use tokio::sync::mpsc::{Receiver, Sender};
use tokio::{select, sync::mpsc};
Expand Down Expand Up @@ -39,7 +39,7 @@ impl Clock for ChronoClock {
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let client = openadr_client::Client::with_url("http://localhost:3000/".try_into()?, None);
let program = client.get_program(Target::Program("name")).await?;
let program = client.get_program_by_name("name").await?;

// channel used to send new timelines
let (sender, receiver) = mpsc::channel(1);
Expand Down
77 changes: 34 additions & 43 deletions openadr-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,26 +380,27 @@ impl Client {
Ok(ProgramClient::from_program(self.clone(), program))
}

/// Get a list of programs from the VTN with the given query parameters
pub async fn get_programs_request(
/// Lowlevel operation that gets a list of programs from the VTN with the given query parameters
pub async fn get_programs(
&self,
target_type: Option<TargetLabel>,
targets: &[&str],
filter: Filter<'_>,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now consistent with the equivalent api for events

pagination: PaginationOptions,
) -> Result<Vec<ProgramClient>> {
// convert query params
let target_type_str = target_type.map(|t| t.to_string());
let skip_str = pagination.skip.to_string();
let limit_str = pagination.limit.to_string();

// insert into query params
let mut query = vec![];
if let Some(target_type_ref) = &target_type_str {
for target in targets {
query.push(("targetValues", *target));

if let Filter::By(ref target_label, target_values) = filter {
query.push(("targetType", target_label.as_str()));

for target_value in target_values {
query.push(("targetValues", *target_value));
}
query.push(("targetType", target_type_ref.as_str()));
}

query.push(("skip", &skip_str));
query.push(("limit", &limit_str));

Expand All @@ -411,25 +412,6 @@ impl Client {
.collect())
}

/// Get a single program from the VTN that matches the given target
pub async fn get_program(&self, target: Target<'_>) -> Result<ProgramClient> {
let pagination = PaginationOptions { skip: 0, limit: 2 };

let mut programs = self
.get_programs_request(
Some(target.target_label()),
target.target_values(),
pagination,
)
.await?;

match programs[..] {
[] => Err(crate::Error::ObjectNotFound),
[_] => Ok(programs.remove(0)),
[..] => Err(crate::Error::DuplicateObject),
}
}

Comment on lines -414 to -432
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the only target that you can give that gives a single result is ProgramName, so we can just use a get_program_by_name

/// Get a list of programs from the VTN with the given query parameters
pub async fn get_program_list(&self, target: Target<'_>) -> Result<Vec<ProgramClient>> {
let page_size = self.client_ref.default_page_size();
Expand All @@ -442,9 +424,8 @@ impl Client {
};

let received = self
.get_programs_request(
Some(target.target_label()),
target.target_values(),
.get_programs(
Filter::By(target.target_label(), target.target_values()),
pagination,
)
.await?;
Expand All @@ -467,24 +448,22 @@ impl Client {
pub async fn get_all_programs(&self) -> Result<Vec<ProgramClient>> {
let page_size = self.client_ref.default_page_size();
let mut programs = vec![];
let mut page = 0;
loop {

for page in 0.. {
// TODO: this pagination should really depend on that the server indicated there are more results
let pagination = PaginationOptions {
skip: page * page_size,
limit: page_size,
};

let received = self.get_programs_request(None, &[], pagination).await?;
let received = self.get_programs(Filter::None, pagination).await?;
let received_all = received.len() < page_size;
for program in received {
programs.push(program);
}

if received_all {
break;
} else {
page += 1;
}
}

Expand All @@ -493,7 +472,21 @@ impl Client {

/// Get a program by name
pub async fn get_program_by_name(&self, name: &str) -> Result<ProgramClient> {
self.get_program(Target::Program(name)).await
let target = Target::Program(name);

let pagination = PaginationOptions { skip: 0, limit: 2 };
let mut programs = self
.get_programs(
Filter::By(target.target_label(), target.target_values()),
pagination,
)
.await?;

match programs[..] {
[] => Err(crate::Error::ObjectNotFound),
[_] => Ok(programs.remove(0)),
[..] => Err(crate::Error::DuplicateObject),
}
}

/// Get a program by id
Expand All @@ -512,8 +505,8 @@ impl Client {
Ok(EventClient::from_event(self.client_ref.clone(), event))
}

/// Get a list of events from the VTN with the given query parameters
pub async fn get_events_request(
/// Lowlevel operation that gets a list of events from the VTN with the given query parameters
pub async fn get_events(
&self,
program_id: Option<&ProgramId>,
filter: Filter<'_>,
Expand Down Expand Up @@ -563,7 +556,7 @@ impl Client {
};

let received = self
.get_events_request(
.get_events(
program_id,
Filter::By(target.target_label(), target.target_values()),
pagination,
Expand Down Expand Up @@ -596,9 +589,7 @@ impl Client {
limit: page_size,
};

let received = self
.get_events_request(None, Filter::None, pagination)
.await?;
let received = self.get_events(None, Filter::None, pagination).await?;
let received_all = received.len() < page_size;
for event in received {
events.push(event);
Expand Down
4 changes: 2 additions & 2 deletions openadr-client/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ impl ProgramClient {
pagination: PaginationOptions,
) -> Result<Vec<EventClient>> {
self.client
.get_events_request(Some(self.id()), filter, pagination)
.get_events(Some(self.id()), filter, pagination)
.await
}

Expand All @@ -130,7 +130,7 @@ impl ProgramClient {

let received = self
.client
.get_events_request(Some(self.id()), Filter::None, pagination)
.get_events(Some(self.id()), Filter::None, pagination)
.await?;
let received_all = received.len() < page_size;
for event in received {
Expand Down
18 changes: 8 additions & 10 deletions openadr-client/tests/program.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use axum::http::StatusCode;

use openadr_client::{Error, PaginationOptions};
use openadr_client::{Error, Filter, PaginationOptions};
use openadr_wire::{program::ProgramContent, target::TargetLabel};

mod common;
Expand Down Expand Up @@ -162,30 +162,29 @@ async fn retrieve_all_with_filter() {
}

let programs = client
.get_programs_request(None, &[], PaginationOptions { skip: 0, limit: 50 })
.get_programs(Filter::None, PaginationOptions { skip: 0, limit: 50 })
.await
.unwrap();
assert_eq!(programs.len(), 3);

// skip
let programs = client
.get_programs_request(None, &[], PaginationOptions { skip: 1, limit: 50 })
.get_programs(Filter::None, PaginationOptions { skip: 1, limit: 50 })
.await
.unwrap();
assert_eq!(programs.len(), 2);

// limit
let programs = client
.get_programs_request(None, &[], PaginationOptions { skip: 0, limit: 2 })
.get_programs(Filter::None, PaginationOptions { skip: 0, limit: 2 })
.await
.unwrap();
assert_eq!(programs.len(), 2);

// program name
let err = client
.get_programs_request(
Some(TargetLabel::Private("NONSENSE".to_string())),
&[],
.get_programs(
Filter::By(TargetLabel::Private("NONSENSE".to_string()), &[]),
PaginationOptions { skip: 0, limit: 2 },
)
.await
Expand All @@ -196,9 +195,8 @@ async fn retrieve_all_with_filter() {
assert_eq!(problem.status, StatusCode::NOT_IMPLEMENTED);

let programs = client
.get_programs_request(
Some(TargetLabel::ProgramName),
&["program1", "program2"],
.get_programs(
Filter::By(TargetLabel::ProgramName, &["program1", "program2"]),
PaginationOptions { skip: 0, limit: 50 },
)
.await
Expand Down