|
18 | 18 | //! If this works, it means that the client is correctly configured and you can use it to make requests to the DB.
|
19 | 19 |
|
20 | 20 | mod client_request;
|
| 21 | +mod precondition; |
21 | 22 |
|
22 | 23 | use client_request::{
|
23 | 24 | ClientRequest, ListEventTypesRequest, ListSubjectsRequest, OneShotRequest, PingRequest,
|
24 |
| - RegisterEventSchemaRequest, StreamingRequest, VerifyApiTokenRequest, |
| 25 | + RegisterEventSchemaRequest, StreamingRequest, VerifyApiTokenRequest, WriteEventsRequest, |
25 | 26 | list_event_types::EventType,
|
26 | 27 | };
|
27 | 28 |
|
28 | 29 | use futures::Stream;
|
| 30 | +pub use precondition::Precondition; |
29 | 31 | use reqwest;
|
30 | 32 | use url::Url;
|
31 | 33 |
|
32 |
| -use crate::{error::ClientError, event::ManagementEvent}; |
| 34 | +use crate::{ |
| 35 | + error::ClientError, |
| 36 | + event::{Event, EventCandidate, ManagementEvent}, |
| 37 | +}; |
33 | 38 |
|
34 | 39 | /// Client for an [EventsourcingDB](https://www.eventsourcingdb.io/) instance.
|
35 | 40 | #[derive(Debug)]
|
@@ -79,6 +84,8 @@ impl Client {
|
79 | 84 | ///
|
80 | 85 | /// This function will return a [`reqwest::RequestBuilder`] which can be used to send the request.
|
81 | 86 | ///
|
| 87 | + /// This function will return a [`reqwest::RequestBuilder`] which can be used to send the request. |
| 88 | + /// |
82 | 89 | /// # Errors
|
83 | 90 | /// This function will return an error if the request fails or if the URL is invalid.
|
84 | 91 | fn build_request<R: ClientRequest>(
|
@@ -233,4 +240,42 @@ impl Client {
|
233 | 240 | let response = self.request_streaming(ListEventTypesRequest).await?;
|
234 | 241 | Ok(response)
|
235 | 242 | }
|
| 243 | + |
| 244 | + /// Writes events to the DB instance. |
| 245 | + /// |
| 246 | + /// ``` |
| 247 | + /// use eventsourcingdb_client_rust::event::EventCandidate; |
| 248 | + /// # use serde_json::json; |
| 249 | + /// # tokio_test::block_on(async { |
| 250 | + /// # let container = eventsourcingdb_client_rust::container::Container::start_default().await.unwrap(); |
| 251 | + /// let db_url = "http://localhost:3000/"; |
| 252 | + /// let api_token = "secrettoken"; |
| 253 | + /// # let db_url = container.get_base_url().await.unwrap(); |
| 254 | + /// # let api_token = container.get_api_token(); |
| 255 | + /// let client = eventsourcingdb_client_rust::client::Client::new(db_url, api_token); |
| 256 | + /// let candidates = vec![ |
| 257 | + /// EventCandidate::builder() |
| 258 | + /// .source("https://www.eventsourcingdb.io".to_string()) |
| 259 | + /// .data(json!({"value": 1})) |
| 260 | + /// .subject("/test".to_string()) |
| 261 | + /// .r#type("io.eventsourcingdb.test".to_string()) |
| 262 | + /// .build() |
| 263 | + /// ]; |
| 264 | + /// let written_events = client.write_events(candidates, vec![]).await.expect("Failed to write events"); |
| 265 | + /// # }) |
| 266 | + /// ``` |
| 267 | + /// |
| 268 | + /// # Errors |
| 269 | + /// This function will return an error if the request fails or if the URL is invalid. |
| 270 | + pub async fn write_events( |
| 271 | + &self, |
| 272 | + events: Vec<EventCandidate>, |
| 273 | + preconditions: Vec<Precondition>, |
| 274 | + ) -> Result<Vec<Event>, ClientError> { |
| 275 | + self.request_oneshot(WriteEventsRequest { |
| 276 | + events, |
| 277 | + preconditions, |
| 278 | + }) |
| 279 | + .await |
| 280 | + } |
236 | 281 | }
|
0 commit comments