20
20
mod client_request;
21
21
mod precondition;
22
22
23
+ use crate :: {
24
+ error:: ClientError ,
25
+ event:: { Event , EventCandidate , ManagementEvent } ,
26
+ } ;
23
27
use client_request:: {
24
28
ClientRequest , ListEventTypesRequest , ListSubjectsRequest , OneShotRequest , PingRequest ,
25
29
RegisterEventSchemaRequest , StreamingRequest , VerifyApiTokenRequest , WriteEventsRequest ,
26
30
list_event_types:: EventType ,
27
31
} ;
28
-
29
32
use futures:: Stream ;
30
33
pub use precondition:: Precondition ;
31
34
use reqwest;
32
35
use url:: Url ;
33
36
34
- use crate :: {
35
- error:: ClientError ,
36
- event:: { Event , EventCandidate , ManagementEvent } ,
37
- } ;
38
-
39
37
/// Client for an [EventsourcingDB](https://www.eventsourcingdb.io/) instance.
40
38
#[ derive( Debug ) ]
41
39
pub struct Client {
@@ -84,8 +82,6 @@ impl Client {
84
82
///
85
83
/// This function will return a [`reqwest::RequestBuilder`] which can be used to send the request.
86
84
///
87
- /// This function will return a [`reqwest::RequestBuilder`] which can be used to send the request.
88
- ///
89
85
/// # Errors
90
86
/// This function will return an error if the request fails or if the URL is invalid.
91
87
fn build_request < R : ClientRequest > (
@@ -203,6 +199,34 @@ impl Client {
203
199
204
200
/// Registers an event schema with the DB instance.
205
201
///
202
+ /// ```
203
+ /// use eventsourcingdb_client_rust::event::EventCandidate;
204
+ /// use futures::StreamExt;
205
+ /// # use serde_json::json;
206
+ /// # tokio_test::block_on(async {
207
+ /// # let container = eventsourcingdb_client_rust::container::Container::start_default().await.unwrap();
208
+ /// let db_url = "http://localhost:3000/";
209
+ /// let api_token = "secrettoken";
210
+ /// # let db_url = container.get_base_url().await.unwrap();
211
+ /// # let api_token = container.get_api_token();
212
+ /// let client = eventsourcingdb_client_rust::client::Client::new(db_url, api_token);
213
+ /// let event_type = "io.eventsourcingdb.test";
214
+ /// let schema = json!({
215
+ /// "type": "object",
216
+ /// "properties": {
217
+ /// "id": {
218
+ /// "type": "string"
219
+ /// },
220
+ /// "name": {
221
+ /// "type": "string"
222
+ /// }
223
+ /// },
224
+ /// "required": ["id", "name"]
225
+ /// });
226
+ /// client.register_event_schema(event_type, &schema).await.expect("Failed to list event types");
227
+ /// # })
228
+ /// ```
229
+ ///
206
230
/// # Errors
207
231
/// This function will return an error if the request fails or if the provided schema is invalid.
208
232
pub async fn register_event_schema (
@@ -216,6 +240,44 @@ impl Client {
216
240
217
241
/// List all subjects in the DB instance.
218
242
///
243
+ /// To get all subjects in the DB, just pass `None` as the `base_subject`.
244
+ /// ```
245
+ /// use eventsourcingdb_client_rust::event::EventCandidate;
246
+ /// use futures::StreamExt;
247
+ /// # use serde_json::json;
248
+ /// # tokio_test::block_on(async {
249
+ /// # let container = eventsourcingdb_client_rust::container::Container::start_default().await.unwrap();
250
+ /// let db_url = "http://localhost:3000/";
251
+ /// let api_token = "secrettoken";
252
+ /// # let db_url = container.get_base_url().await.unwrap();
253
+ /// # let api_token = container.get_api_token();
254
+ /// let client = eventsourcingdb_client_rust::client::Client::new(db_url, api_token);
255
+ /// let mut subject_stream = client.list_subjects(None).await.expect("Failed to list event types");
256
+ /// while let Some(subject) = subject_stream.next().await {
257
+ /// println!("Found Type {}", subject.expect("Error while reading types"));
258
+ /// }
259
+ /// # })
260
+ /// ```
261
+ ///
262
+ /// To get all subjects under /test in the DB, just pass `Some("/test")` as the `base_subject`.
263
+ /// ```
264
+ /// use eventsourcingdb_client_rust::event::EventCandidate;
265
+ /// use futures::StreamExt;
266
+ /// # use serde_json::json;
267
+ /// # tokio_test::block_on(async {
268
+ /// # let container = eventsourcingdb_client_rust::container::Container::start_default().await.unwrap();
269
+ /// let db_url = "http://localhost:3000/";
270
+ /// let api_token = "secrettoken";
271
+ /// # let db_url = container.get_base_url().await.unwrap();
272
+ /// # let api_token = container.get_api_token();
273
+ /// let client = eventsourcingdb_client_rust::client::Client::new(db_url, api_token);
274
+ /// let mut subject_stream = client.list_subjects(Some("/test")).await.expect("Failed to list event types");
275
+ /// while let Some(subject) = subject_stream.next().await {
276
+ /// println!("Found Type {}", subject.expect("Error while reading types"));
277
+ /// }
278
+ /// # })
279
+ /// ```
280
+ ///
219
281
/// # Errors
220
282
/// This function will return an error if the request fails or if the URL is invalid.
221
283
pub async fn list_subjects (
@@ -232,6 +294,24 @@ impl Client {
232
294
233
295
/// List all event types in the DB instance.
234
296
///
297
+ /// ```
298
+ /// use eventsourcingdb_client_rust::event::EventCandidate;
299
+ /// use futures::StreamExt;
300
+ /// # use serde_json::json;
301
+ /// # tokio_test::block_on(async {
302
+ /// # let container = eventsourcingdb_client_rust::container::Container::start_default().await.unwrap();
303
+ /// let db_url = "http://localhost:3000/";
304
+ /// let api_token = "secrettoken";
305
+ /// # let db_url = container.get_base_url().await.unwrap();
306
+ /// # let api_token = container.get_api_token();
307
+ /// let client = eventsourcingdb_client_rust::client::Client::new(db_url, api_token);
308
+ /// let mut type_stream = client.list_event_types().await.expect("Failed to list event types");
309
+ /// while let Some(ty) = type_stream.next().await {
310
+ /// println!("Found Type {}", ty.expect("Error while reading types").name);
311
+ /// }
312
+ /// # })
313
+ /// ```
314
+ ///
235
315
/// # Errors
236
316
/// This function will return an error if the request fails or if the URL is invalid.
237
317
pub async fn list_event_types (
0 commit comments