eventsourcingdb 0.13.2

A client library for the EventsourcingDB by the native web.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
//! Client for the [EventsourcingDB](https://www.eventsourcingdb.io/) API.
//!
//! To use the client, create it with the base URL and API token of your [EventsourcingDB](https://www.eventsourcingdb.io/) instance.
//! ```
//! # tokio_test::block_on(async {
//! # let container = eventsourcingdb::container::Container::start_preview().await.unwrap();
//! let db_url = "http://localhost:3000/";
//! let api_token = "secrettoken";
//! # let db_url = container.get_base_url().await.unwrap();
//! # let api_token = container.get_api_token();
//! let client = eventsourcingdb::client::Client::new(db_url, api_token);
//! client.ping().await.expect("Failed to ping");
//! client.verify_api_token().await.expect("Failed to verify API token");
//! # })
//! ```
//!
//! With the code above you can verify that the DB is reachable and that the API token is valid.
//! If this works, it means that the client is correctly configured and you can use it to make requests to the DB.

mod client_request;
mod precondition;
pub mod request_options;

use crate::{
    client::client_request::ReadEventTypeRequest,
    error::ClientError,
    event::{Event, EventCandidate, ManagementEvent},
    request_options::EventType,
};
use client_request::{
    ClientRequest, ListEventTypesRequest, ListSubjectsRequest, ObserveEventsRequest,
    OneShotRequest, PingRequest, ReadEventsRequest, RegisterEventSchemaRequest,
    RunEventqlQueryRequest, StreamingRequest, VerifyApiTokenRequest, WriteEventsRequest,
};
use futures::Stream;
pub use precondition::Precondition;
use reqwest;
use url::Url;

/// Client for an [EventsourcingDB](https://www.eventsourcingdb.io/) instance.
#[derive(Debug)]
pub struct Client {
    base_url: Url,
    api_token: String,
    reqwest: reqwest::Client,
}

impl Client {
    /// Validates that the server header starts with "EventSourcingDB/"
    fn validate_server_headers(response: &reqwest::Response) -> Result<(), ClientError> {
        match response
            .headers()
            .get("Server")
            .and_then(|h| h.to_str().ok())
            .map(|s| s.starts_with("EventSourcingDB/"))
        {
            Some(true) => Ok(()),
            _ => Err(ClientError::InvalidServerHeader),
        }
    }

    /// Creates a new client instance based on the base URL and API token
    pub fn new(base_url: Url, api_token: impl Into<String>) -> Self {
        Client {
            base_url,
            api_token: api_token.into(),
            reqwest: reqwest::Client::new(),
        }
    }

    /// Get the base URL of the client to use for API calls
    /// ```
    /// # use url::Url;
    /// # use eventsourcingdb::client::Client;
    /// # let client = Client::new("http://localhost:8080/".parse().unwrap(), "token");
    /// let base_url = client.get_base_url();
    /// # assert_eq!(base_url.as_str(), "http://localhost:8080/");
    /// ```
    #[must_use]
    pub fn get_base_url(&self) -> &Url {
        &self.base_url
    }

    /// Get the API token of the client to use for API calls
    /// ```
    /// # use eventsourcingdb::client::Client;
    /// # use url::Url;
    /// # let client = Client::new("http://localhost:8080/".parse().unwrap(), "secrettoken");
    /// let api_token = client.get_api_token();
    /// # assert_eq!(api_token, "secrettoken");
    /// ```
    #[must_use]
    pub fn get_api_token(&self) -> &str {
        &self.api_token
    }

    /// Utility function to request an endpoint of the API.
    ///
    /// This function will return a [`reqwest::RequestBuilder`] which can be used to send the request.
    ///
    /// # Errors
    /// This function will return an error if the request fails or if the URL is invalid.
    fn build_request<R: ClientRequest>(
        &self,
        endpoint: &R,
    ) -> Result<reqwest::RequestBuilder, ClientError> {
        let url = self
            .base_url
            .join(endpoint.url_path())
            .map_err(ClientError::URLParseError)?;

        let request = match endpoint.method() {
            reqwest::Method::GET => self.reqwest.get(url),
            reqwest::Method::POST => self.reqwest.post(url),
            _ => return Err(ClientError::InvalidRequestMethod),
        }
        .bearer_auth(&self.api_token);
        let request = if let Some(body) = endpoint.body() {
            request
                .header("Content-Type", "application/json")
                .json(&body?)
        } else {
            request
        };
        Ok(request)
    }

    /// Utility function to request an endpoint of the API as a oneshot.
    ///
    /// This means, that the response is not streamed, but returned as a single value.
    ///
    /// # Errors
    /// This function will return an error if the request fails or if the URL is invalid.
    async fn request_oneshot<R: OneShotRequest>(
        &self,
        endpoint: R,
    ) -> Result<R::Response, ClientError> {
        let response = self.build_request(&endpoint)?.send().await?;

        if response.status().is_success() {
            Self::validate_server_headers(&response)?;
            let result = response.json().await?;
            endpoint.validate_response(&result)?;
            Ok(result)
        } else {
            Err(ClientError::DBApiError(
                response.status(),
                response.text().await.unwrap_or_default(),
            ))
        }
    }

    /// Utility function to request an endpoint of the API as a stream.
    ///
    /// This means, that the response is streamed and returned as a stream of values.
    ///
    /// # Errors
    /// This function will return an error if the request fails or if the URL is invalid.
    async fn request_streaming<R: StreamingRequest>(
        &self,
        endpoint: R,
    ) -> Result<impl Stream<Item = Result<R::ItemType, ClientError>>, ClientError> {
        let response = self.build_request(&endpoint)?.send().await?;
        Self::validate_server_headers(&response)?;
        if response.status().is_success() {
            Ok(R::build_stream(response))
        } else {
            Err(ClientError::DBApiError(
                response.status(),
                response.text().await.unwrap_or_default(),
            ))
        }
    }

    /// Pings the DB instance to check if it is reachable.
    ///
    /// ```
    /// # tokio_test::block_on(async {
    /// # let container = eventsourcingdb::container::Container::start_preview().await.unwrap();
    /// let db_url = "http://localhost:3000/";
    /// let api_token = "secrettoken";
    /// # let db_url = container.get_base_url().await.unwrap();
    /// # let api_token = container.get_api_token();
    /// let client = eventsourcingdb::client::Client::new(db_url, api_token);
    /// client.ping().await.expect("Failed to ping");
    /// # })
    /// ```
    ///
    /// # Errors
    /// This function will return an error if the request fails or if the URL is invalid.
    pub async fn ping(&self) -> Result<(), ClientError> {
        let _ = self.request_oneshot(PingRequest).await?;
        Ok(())
    }

    /// Reads events from the DB instance.
    ///
    /// ```
    /// use eventsourcingdb::event::EventCandidate;
    /// use futures::StreamExt;
    /// # use serde_json::json;
    /// # tokio_test::block_on(async {
    /// # let container = eventsourcingdb::container::Container::start_preview().await.unwrap();
    /// let db_url = "http://localhost:3000/";
    /// let api_token = "secrettoken";
    /// # let db_url = container.get_base_url().await.unwrap();
    /// # let api_token = container.get_api_token();
    /// let client = eventsourcingdb::client::Client::new(db_url, api_token);
    /// let mut event_stream = client.read_events("/", None).await.expect("Failed to read events");
    /// while let Some(event) = event_stream.next().await {
    ///     println!("Found Type {:?}", event.expect("Error while reading events"));
    /// }
    /// # })
    /// ```
    ///
    /// # Errors
    /// This function will return an error if the request fails or if the URL is invalid.
    pub async fn read_events<'a>(
        &self,
        subject: &'a str,
        options: Option<request_options::ReadEventsOptions<'a>>,
    ) -> Result<impl Stream<Item = Result<Event, ClientError>>, ClientError> {
        let response = self
            .request_streaming(ReadEventsRequest { subject, options })
            .await?;
        Ok(response)
    }

    /// Reads a specific event type from the DB instance.
    ///
    /// ```
    /// use eventsourcingdb::event::EventCandidate;
    /// use futures::StreamExt;
    /// # use serde_json::json;
    /// # tokio_test::block_on(async {
    /// # let container = eventsourcingdb::container::Container::start_preview().await.unwrap();
    /// let db_url = "http://localhost:3000/";
    /// let api_token = "secrettoken";
    /// # let db_url = container.get_base_url().await.unwrap();
    /// # let api_token = container.get_api_token();
    /// let client = eventsourcingdb::client::Client::new(db_url, api_token);
    /// let event_type = "io.eventsourcingdb.test";
    /// let schema = json!({
    ///     "type": "object",
    ///     "properties": {
    ///         "id": {
    ///             "type": "string"
    ///         },
    ///         "name": {
    ///             "type": "string"
    ///         }
    ///     },
    ///     "required": ["id", "name"]
    /// });
    /// client.register_event_schema(event_type, &schema).await.expect("Failed to register event types");
    /// let type_info = client.read_event_type(event_type).await.expect("Failed to read event type");
    /// println!("Found Type {} with schema {:?}", type_info.name, type_info.schema);
    /// # })
    /// ```
    ///
    /// # Errors
    /// This function will return an error if the request fails or if the URL is invalid.
    pub async fn read_event_type(&self, event_type: &str) -> Result<EventType, ClientError> {
        let response = self
            .request_oneshot(ReadEventTypeRequest {
                event_type: event_type.to_string(),
            })
            .await?;
        Ok(response)
    }

    /// Observe events from the DB instance.
    ///
    /// ```
    /// use eventsourcingdb::event::EventCandidate;
    /// use futures::StreamExt;
    /// # use serde_json::json;
    /// # tokio_test::block_on(async {
    /// # let container = eventsourcingdb::container::Container::start_preview().await.unwrap();
    /// let db_url = "http://localhost:3000/";
    /// let api_token = "secrettoken";
    /// # let db_url = container.get_base_url().await.unwrap();
    /// # let api_token = container.get_api_token();
    /// let client = eventsourcingdb::client::Client::new(db_url, api_token);
    /// # client.write_events(
    /// #   vec![
    /// #     EventCandidate::builder()
    /// #        .source("https://www.eventsourcingdb.io".to_string())
    /// #        .data(json!({"value": 1}))
    /// #        .subject("/test".to_string())
    /// #        .ty("io.eventsourcingdb.test".to_string())
    /// #        .build()
    /// #   ],
    /// #   vec![]
    /// # ).await.expect("Failed to write events");
    /// let mut event_stream = client.observe_events("/test", None).await.expect("Failed to observe events");
    /// match event_stream.next().await {
    ///     Some(Ok(event)) => println!("Found Event {:?}", event),
    ///     Some(Err(e)) => eprintln!("Error while reading event: {:?}", e),
    ///     None => println!("No more events."),
    /// }
    /// # })
    /// ```
    ///
    /// # Errors
    /// This function will return an error if the request fails or if the URL is invalid.
    pub async fn observe_events<'a>(
        &self,
        subject: &'a str,
        options: Option<request_options::ObserveEventsOptions<'a>>,
    ) -> Result<impl Stream<Item = Result<Event, ClientError>>, ClientError> {
        let response = self
            .request_streaming(ObserveEventsRequest { subject, options })
            .await?;
        Ok(response)
    }

    /// Verifies the API token by sending a request to the DB instance.
    ///
    /// ```
    /// # tokio_test::block_on(async {
    /// # let container = eventsourcingdb::container::Container::start_preview().await.unwrap();
    /// let db_url = "http://localhost:3000/";
    /// let api_token = "secrettoken";
    /// # let db_url = container.get_base_url().await.unwrap();
    /// # let api_token = container.get_api_token();
    /// let client = eventsourcingdb::client::Client::new(db_url, api_token);
    /// client.verify_api_token().await.expect("Failed to ping");
    /// # })
    /// ```
    ///
    /// # Errors
    /// This function will return an error if the request fails or if the URL is invalid.
    pub async fn verify_api_token(&self) -> Result<(), ClientError> {
        let _ = self.request_oneshot(VerifyApiTokenRequest).await?;
        Ok(())
    }

    /// Registers an event schema with the DB instance.
    ///
    /// ```
    /// use eventsourcingdb::event::EventCandidate;
    /// use futures::StreamExt;
    /// # use serde_json::json;
    /// # tokio_test::block_on(async {
    /// # let container = eventsourcingdb::container::Container::start_preview().await.unwrap();
    /// let db_url = "http://localhost:3000/";
    /// let api_token = "secrettoken";
    /// # let db_url = container.get_base_url().await.unwrap();
    /// # let api_token = container.get_api_token();
    /// let client = eventsourcingdb::client::Client::new(db_url, api_token);
    /// let event_type = "io.eventsourcingdb.test";
    /// let schema = json!({
    ///     "type": "object",
    ///     "properties": {
    ///         "id": {
    ///             "type": "string"
    ///         },
    ///         "name": {
    ///             "type": "string"
    ///         }
    ///     },
    ///     "required": ["id", "name"]
    /// });
    /// client.register_event_schema(event_type, &schema).await.expect("Failed to list event types");
    /// # })
    /// ```
    ///
    /// # Errors
    /// This function will return an error if the request fails or if the provided schema is invalid.
    pub async fn register_event_schema(
        &self,
        event_type: &str,
        schema: &serde_json::Value,
    ) -> Result<ManagementEvent, ClientError> {
        self.request_oneshot(RegisterEventSchemaRequest::try_new(event_type, schema)?)
            .await
    }

    /// List all subjects in the DB instance.
    ///
    /// To get all subjects in the DB, just pass `None` as the `base_subject`.
    /// ```
    /// use eventsourcingdb::event::EventCandidate;
    /// use futures::StreamExt;
    /// # use serde_json::json;
    /// # tokio_test::block_on(async {
    /// # let container = eventsourcingdb::container::Container::start_preview().await.unwrap();
    /// let db_url = "http://localhost:3000/";
    /// let api_token = "secrettoken";
    /// # let db_url = container.get_base_url().await.unwrap();
    /// # let api_token = container.get_api_token();
    /// let client = eventsourcingdb::client::Client::new(db_url, api_token);
    /// let mut subject_stream = client.list_subjects(None).await.expect("Failed to list event types");
    /// while let Some(subject) = subject_stream.next().await {
    ///     println!("Found Type {}", subject.expect("Error while reading types"));
    /// }
    /// # })
    /// ```
    ///
    /// To get all subjects under /test in the DB, just pass `Some("/test")` as the `base_subject`.
    /// ```
    /// use eventsourcingdb::event::EventCandidate;
    /// use futures::StreamExt;
    /// # use serde_json::json;
    /// # tokio_test::block_on(async {
    /// # let container = eventsourcingdb::container::Container::start_preview().await.unwrap();
    /// let db_url = "http://localhost:3000/";
    /// let api_token = "secrettoken";
    /// # let db_url = container.get_base_url().await.unwrap();
    /// # let api_token = container.get_api_token();
    /// let client = eventsourcingdb::client::Client::new(db_url, api_token);
    /// let mut subject_stream = client.list_subjects(Some("/test")).await.expect("Failed to list event types");
    /// while let Some(subject) = subject_stream.next().await {
    ///     println!("Found Type {}", subject.expect("Error while reading types"));
    /// }
    /// # })
    /// ```
    ///
    /// # Errors
    /// This function will return an error if the request fails or if the URL is invalid.
    pub async fn list_subjects(
        &self,
        base_subject: Option<&str>,
    ) -> Result<impl Stream<Item = Result<String, ClientError>>, ClientError> {
        let response = self
            .request_streaming(ListSubjectsRequest {
                base_subject: base_subject.unwrap_or("/"),
            })
            .await?;
        Ok(response)
    }

    /// List all event types in the DB instance.
    ///
    /// ```
    /// use eventsourcingdb::event::EventCandidate;
    /// use futures::StreamExt;
    /// # use serde_json::json;
    /// # tokio_test::block_on(async {
    /// # let container = eventsourcingdb::container::Container::start_preview().await.unwrap();
    /// let db_url = "http://localhost:3000/";
    /// let api_token = "secrettoken";
    /// # let db_url = container.get_base_url().await.unwrap();
    /// # let api_token = container.get_api_token();
    /// let client = eventsourcingdb::client::Client::new(db_url, api_token);
    /// let mut type_stream = client.list_event_types().await.expect("Failed to list event types");
    /// while let Some(ty) = type_stream.next().await {
    ///     println!("Found Type {}", ty.expect("Error while reading types").name);
    /// }
    /// # })
    /// ```
    ///
    /// # Errors
    /// This function will return an error if the request fails or if the URL is invalid.
    pub async fn list_event_types(
        &self,
    ) -> Result<impl Stream<Item = Result<EventType, ClientError>>, ClientError> {
        let response = self.request_streaming(ListEventTypesRequest).await?;
        Ok(response)
    }

    /// Writes events to the DB instance.
    ///
    /// ```
    /// use eventsourcingdb::event::EventCandidate;
    /// # use serde_json::json;
    /// # tokio_test::block_on(async {
    /// # let container = eventsourcingdb::container::Container::start_preview().await.unwrap();
    /// let db_url = "http://localhost:3000/";
    /// let api_token = "secrettoken";
    /// # let db_url = container.get_base_url().await.unwrap();
    /// # let api_token = container.get_api_token();
    /// let client = eventsourcingdb::client::Client::new(db_url, api_token);
    /// let candidates = vec![
    ///     EventCandidate::builder()
    ///        .source("https://www.eventsourcingdb.io".to_string())
    ///        .data(json!({"value": 1}))
    ///        .subject("/test".to_string())
    ///        .ty("io.eventsourcingdb.test".to_string())
    ///        .build()
    /// ];
    /// let written_events = client.write_events(candidates, vec![]).await.expect("Failed to write events");
    /// # })
    /// ```
    ///
    /// # Errors
    /// This function will return an error if the request fails or if the URL is invalid.
    pub async fn write_events(
        &self,
        events: Vec<EventCandidate>,
        preconditions: Vec<Precondition>,
    ) -> Result<Vec<Event>, ClientError> {
        self.request_oneshot(WriteEventsRequest {
            events,
            preconditions,
        })
        .await
    }

    /// Run an eventql query against the DB.
    ///
    /// ```
    /// use eventsourcingdb::event::EventCandidate;
    /// use futures::StreamExt;
    /// # use serde_json::json;
    /// # tokio_test::block_on(async {
    /// # let container = eventsourcingdb::container::Container::start_preview().await.unwrap();
    /// let db_url = "http://localhost:3000/";
    /// let api_token = "secrettoken";
    /// # let db_url = container.get_base_url().await.unwrap();
    /// # let api_token = container.get_api_token();
    /// let client = eventsourcingdb::client::Client::new(db_url, api_token);
    /// let query = "FROM e IN events ORDER BY e.time DESC TOP 100 PROJECT INTO e";
    /// let mut row_stream = client.run_eventql_query(query).await.expect("Failed to list event types");
    /// while let Some(row) = row_stream.next().await {
    ///     println!("Found row {:?}", row.expect("Error while reading row"));
    /// }
    /// # })
    /// ```
    ///
    /// # Errors
    /// This function will return an error if the request fails or if the URL is invalid.
    pub async fn run_eventql_query(
        &self,
        query: &str,
    ) -> Result<impl Stream<Item = Result<serde_json::Value, ClientError>>, ClientError> {
        let response = self
            .request_streaming(RunEventqlQueryRequest { query })
            .await?;
        Ok(response)
    }
}