Skip to content

Commit

Permalink
feat: added query parameters to aw-client-rust's get_events, fixed cl…
Browse files Browse the repository at this point in the history
…ippy complaints
  • Loading branch information
ErikBjare committed May 15, 2021
1 parent 602ddb2 commit 9a82ea2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 18 deletions.
36 changes: 29 additions & 7 deletions aw-client-rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
extern crate aw_models;
extern crate gethostname;
extern crate reqwest;
#[macro_use]
extern crate aw_models;
extern crate serde_json;

use std::collections::HashMap;
use std::vec::Vec;

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

pub use aw_models::{Bucket, BucketMetadata, Event};
Expand Down Expand Up @@ -67,15 +67,37 @@ impl AwClient {
Ok(())
}

pub fn get_events(&self, bucketname: &str) -> Result<Vec<Event>, reqwest::Error> {
let url = format!("{}/api/0/buckets/{}/events", self.baseurl, bucketname);
Ok(self.client.get(&url).send()?.json()?)
pub fn get_events(
&self,
bucketname: &str,
start: Option<DateTime<Utc>>,
stop: Option<DateTime<Utc>>,
limit: Option<u64>,
) -> Result<Vec<Event>, reqwest::Error> {
let mut url = reqwest::Url::parse(
format!("{}/api/0/buckets/{}/events", self.baseurl, bucketname).as_str(),
)
.unwrap();

// Must be a better way to build URLs
if let Some(s) = start {
url.query_pairs_mut()
.append_pair("start", s.to_rfc3339().as_str());
};
if let Some(s) = stop {
url.query_pairs_mut()
.append_pair("end", s.to_rfc3339().as_str());
};
if let Some(s) = limit {
url.query_pairs_mut()
.append_pair("limit", s.to_string().as_str());
};
self.client.get(url).send()?.json()
}

pub fn insert_event(&self, bucketname: &str, event: &Event) -> Result<(), reqwest::Error> {
let url = format!("{}/api/0/buckets/{}/events", self.baseurl, bucketname);
let mut eventlist = Vec::new();
eventlist.push(event.clone());
let eventlist = vec![event.clone()];
self.client.post(&url).json(&eventlist).send()?;
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion aw-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fn main() {
opts.optflag("h", "help", "print this help menu");
let matches = match opts.parse(&args[1..]) {
Ok(m) => m,
Err(f) => panic!(f.to_string()),
Err(f) => panic!("{}", f.to_string()),
};
if matches.opt_present("h") {
print_usage(&program, opts);
Expand Down
20 changes: 10 additions & 10 deletions aw-sync/src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ impl AccessMethod for Datastore {
self.get_bucket(bucket_id)
}
fn create_bucket(&self, bucket: &Bucket) -> Result<(), DatastoreError> {
let res = self.create_bucket(bucket)?;
self.create_bucket(bucket)?;
self.force_commit().unwrap();
Ok(res)
Ok(())
}
fn get_events(
&self,
Expand Down Expand Up @@ -85,23 +85,23 @@ impl AccessMethod for AwClient {
end: Option<DateTime<Utc>>,
limit: Option<u64>,
) -> Result<Vec<Event>, String> {
Ok(self.get_events(bucket_id).unwrap())
Ok(self.get_events(bucket_id, start, end, limit).unwrap())
}
fn insert_events(&self, bucket_id: &str, events: Vec<Event>) -> Result<Vec<Event>, String> {
fn insert_events(&self, _bucket_id: &str, _events: Vec<Event>) -> Result<Vec<Event>, String> {
//Ok(self.insert_events(bucket_id, &events[..]).unwrap())
Err("Not implemented".to_string())
}
fn get_event_count(&self, bucket_id: &str) -> Result<i64, String> {
fn get_event_count(&self, _bucket_id: &str) -> Result<i64, String> {
//Ok(self.get_event_count(bucket_id, None, None).unwrap())
Err("Not implemented".to_string())
}
fn create_bucket(&self, bucket: &Bucket) -> Result<(), DatastoreError> {
Ok(self
.create_bucket(bucket.id.as_str(), bucket._type.as_str())
.unwrap())
self.create_bucket(bucket.id.as_str(), bucket._type.as_str())
.unwrap();
Ok(())
//Err(DatastoreError::InternalError("Not implemented".to_string()))
}
fn heartbeat(&self, bucket_id: &str, event: Event, duration: f64) -> Result<Event, String> {
fn heartbeat(&self, _bucket_id: &str, _event: Event, _duration: f64) -> Result<Event, String> {
Err("Not implemented".to_string())
}
}
Expand Down Expand Up @@ -216,7 +216,7 @@ fn get_or_create_sync_bucket(bucket_from: &Bucket, ds_to: &dyn AccessMethod) ->
ds_to.create_bucket(&bucket_new).unwrap();
ds_to.get_bucket(new_id.as_str()).unwrap()
}
Err(e) => panic!(e),
Err(e) => panic!("{:?}", e),
}
}

Expand Down

0 comments on commit 9a82ea2

Please sign in to comment.