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

Commit

Permalink
Merge pull request #78 from tweedegolf/testing-client
Browse files Browse the repository at this point in the history
add `MockClientRef` to be able to test the VTN and client together
  • Loading branch information
tdittr authored Aug 19, 2024
2 parents f687d53 + 77f8edf commit 26a3578
Show file tree
Hide file tree
Showing 8 changed files with 227 additions and 86 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ iso8601-duration = { version = "0.2.0", features = ["chrono"] }
rangemap = "1.5.1"
jsonwebtoken = "9.3.0"
async-trait = "0.1.81"
tower = { version = "0.4", features = ["util"] }
http-body-util = "0.1.0"

[dev-dependencies]
quickcheck = "1.0.3"
tower = { version = "0.4", features = ["util"] }
http-body-util = "0.1.0"
mime = "0.3"
tokio = { version = "1.37.0", features = ["full", "test-util"] }

Expand Down
6 changes: 3 additions & 3 deletions src/bin/everest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use openadr::{
event::{EventType, EventValuesMap},
values_map::Value,
},
ProgramClient, Target, Timeline,
ClientRef, ProgramClient, Target, Timeline,
};
use std::{error::Error, time::Duration};
use tokio::sync::mpsc::{Receiver, Sender};
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::new("http://localhost:3000/".try_into()?, None);
let client = openadr::Client::with_url("http://localhost:3000/".try_into()?, None);
let program = client.get_program(Target::Program("name")).await?;

// channel used to send new timelines
Expand All @@ -60,7 +60,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
}

async fn poll_timeline(
mut program: ProgramClient,
mut program: ProgramClient<impl ClientRef>,
poll_interval: std::time::Duration,
sender: mpsc::Sender<Timeline>,
) -> Result<(), openadr::Error> {
Expand Down
2 changes: 1 addition & 1 deletion src/cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use openadr::{ClientCredentials, ProgramContent, Target};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = openadr::Client::new(
let client = openadr::Client::with_url(
"http://localhost:3000/".try_into()?,
Some(ClientCredentials::new(
"admin".to_string(),
Expand Down
25 changes: 12 additions & 13 deletions src/client/event.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
use std::sync::Arc;

use crate::{
client::ClientRef,
wire::{
report::{ReportContent, ReportObjectType},
Event, Report,
},
EventContent, ReportClient, Result,
};

use super::ClientRef;

#[derive(Debug)]
pub struct EventClient {
client: Arc<ClientRef>,
pub struct EventClient<C> {
client: Arc<C>,
data: Event,
}

impl EventClient {
pub(super) fn from_event(client: Arc<ClientRef>, event: Event) -> EventClient {
EventClient {
impl<C: ClientRef> EventClient<C> {
pub(super) fn from_event(client: Arc<C>, event: Event) -> Self {
Self {
client,
data: event,
}
Expand Down Expand Up @@ -75,7 +74,7 @@ impl EventClient {
}

/// Create a new report for the event
pub async fn create_report(&self, report_data: ReportContent) -> Result<ReportClient> {
pub async fn create_report(&self, report_data: ReportContent) -> Result<ReportClient<C>> {
if report_data.program_id != self.data().program_id {
return Err(crate::Error::InvalidParentObject);
}
Expand All @@ -93,7 +92,7 @@ impl EventClient {
client_name: Option<&str>,
skip: usize,
limit: usize,
) -> Result<Vec<ReportClient>> {
) -> Result<Vec<ReportClient<C>>> {
let skip_str = skip.to_string();
let limit_str = limit.to_string();

Expand All @@ -116,8 +115,8 @@ impl EventClient {
}

/// Get all reports from the VTN for a specific client, trying to paginate whenever possible
pub async fn get_client_reports(&self, client_name: &str) -> Result<Vec<ReportClient>> {
let page_size = self.client.default_page_size;
pub async fn get_client_reports(&self, client_name: &str) -> Result<Vec<ReportClient<C>>> {
let page_size = self.client.default_page_size();
let mut reports = vec![];
let mut page = 0;
loop {
Expand All @@ -140,8 +139,8 @@ impl EventClient {
}

/// Get all reports from the VTN, trying to paginate whenever possible
pub async fn get_all_reports(&self) -> Result<Vec<ReportClient>> {
let page_size = self.client.default_page_size;
pub async fn get_all_reports(&self) -> Result<Vec<ReportClient<C>>> {
let page_size = self.client.default_page_size();
let mut reports = vec![];
let mut page = 0;
loop {
Expand Down
Loading

0 comments on commit 26a3578

Please sign in to comment.