Skip to content

Commit

Permalink
feat: added query function in aw-client
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikBjare committed Apr 2, 2024
1 parent 1b56e03 commit 965c438
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
6 changes: 6 additions & 0 deletions aw-client-rust/src/blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ impl AwClient {
stop: Option<DateTime<Utc>>,
limit: Option<u64>
);
proxy_method!(
query,
serde_json::Value,
query: &str,
timeperiods: Vec<(DateTime<Utc>, DateTime<Utc>)>
);
proxy_method!(insert_event, (), bucketname: &str, event: &Event);
proxy_method!(insert_events, (), bucketname: &str, events: Vec<Event>);
proxy_method!(
Expand Down
27 changes: 26 additions & 1 deletion aw-client-rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::vec::Vec;
use std::{collections::HashMap, error::Error};

use chrono::{DateTime, Utc};
use serde_json::Map;
use serde_json::{Map, json};

pub use aw_models::{Bucket, BucketMetadata, Event};

Expand Down Expand Up @@ -98,6 +98,31 @@ impl AwClient {
Ok(())
}

pub async fn query(
&self,
query: &str,
timeperiods: Vec<(DateTime<Utc>, DateTime<Utc>)>,
) -> Result<serde_json::Value, reqwest::Error> {
let url = reqwest::Url::parse(
format!("{}/api/0/query", self.baseurl).as_str(),
)
.unwrap();

// Format timeperiods as ISO8601 strings, separated by /
let timeperiods_str: Vec<String> = timeperiods
.iter()
.map(|(start, stop)| (start.to_rfc3339(), stop.to_rfc3339()))
.map(|(start, stop)| format!("{}/{}", start, stop))
.collect();

self.client.post(url)
.json(&json!({
"query": query,
"timeperiods": timeperiods_str,
}))
.send().await?.json().await
}

pub async fn get_events(
&self,
bucketname: &str,
Expand Down

0 comments on commit 965c438

Please sign in to comment.