66/// It manages a sync-folder by syncing the aw-server datastore with a copy/staging datastore in the folder (one for each host).
77/// The sync folder is then synced with remotes using Syncthing/Dropbox/whatever.
88extern crate chrono;
9+ extern crate reqwest;
910extern crate serde_json;
1011
1112use std:: collections:: HashMap ;
@@ -14,6 +15,7 @@ use std::path::Path;
1415
1516use aw_client_rust:: AwClient ;
1617use chrono:: { DateTime , Duration , Utc } ;
18+ use reqwest:: StatusCode ;
1719
1820use aw_datastore:: { Datastore , DatastoreError } ;
1921use aw_models:: { Bucket , Event } ;
@@ -76,7 +78,19 @@ impl AccessMethod for AwClient {
7678 Ok ( self . get_buckets ( ) . unwrap ( ) )
7779 }
7880 fn get_bucket ( & self , bucket_id : & str ) -> Result < Bucket , DatastoreError > {
79- Ok ( self . get_bucket ( bucket_id) . unwrap ( ) )
81+ let bucket = self . get_bucket ( bucket_id) ;
82+ match bucket {
83+ Ok ( bucket) => Ok ( bucket) ,
84+ Err ( e) => {
85+ warn ! ( "{:?}" , e) ;
86+ let code = e. status ( ) . unwrap ( ) ;
87+ if code == StatusCode :: NOT_FOUND {
88+ Err ( DatastoreError :: NoSuchBucket ( bucket_id. into ( ) ) )
89+ } else {
90+ panic ! ( "Unexpected error" ) ;
91+ }
92+ }
93+ }
8094 }
8195 fn get_events (
8296 & self ,
@@ -91,9 +105,8 @@ impl AccessMethod for AwClient {
91105 //Ok(self.insert_events(bucket_id, &events[..]).unwrap())
92106 Err ( "Not implemented" . to_string ( ) )
93107 }
94- fn get_event_count ( & self , _bucket_id : & str ) -> Result < i64 , String > {
95- //Ok(self.get_event_count(bucket_id, None, None).unwrap())
96- Err ( "Not implemented" . to_string ( ) )
108+ fn get_event_count ( & self , bucket_id : & str ) -> Result < i64 , String > {
109+ Ok ( self . get_event_count ( bucket_id) . unwrap ( ) )
97110 }
98111 fn create_bucket ( & self , bucket : & Bucket ) -> Result < ( ) , DatastoreError > {
99112 self . create_bucket ( bucket. id . as_str ( ) , bucket. _type . as_str ( ) )
@@ -113,14 +126,15 @@ pub fn sync_run() {
113126 fs:: create_dir_all ( sync_directory) . unwrap ( ) ;
114127
115128 // TODO: Use the local datastore here, preferably passed from main
116- let ds_local = Datastore :: new (
117- sync_directory
118- . join ( "test-local.db" )
119- . into_os_string ( )
120- . into_string ( )
121- . unwrap ( ) ,
122- false ,
123- ) ;
129+ let ds_local = AwClient :: new ( "127.0.0.1" , "5666" , "aw-sync-rust" ) ;
130+ //let ds_local = Datastore::new(
131+ // sync_directory
132+ // .join("test-local.db")
133+ // .into_os_string()
134+ // .into_string()
135+ // .unwrap(),
136+ // false,
137+ //);
124138 info ! ( "Set up local datastore" ) ;
125139 //log_buckets(&ds_local)?;
126140
@@ -185,8 +199,7 @@ fn setup_test(sync_directory: &Path) -> std::io::Result<Vec<Datastore>> {
185199 timestamp. to_rfc3339( ) ,
186200 i
187201 ) ;
188- let event = serde_json:: from_str ( & event_jsonstr) . unwrap ( ) ;
189- event
202+ serde_json:: from_str ( & event_jsonstr) . unwrap ( )
190203 } )
191204 . collect :: < Vec < Event > > ( ) ;
192205
0 commit comments