1818//! If this works, it means that the client is correctly configured and you can use it to make requests to the DB.
1919
2020mod client_request;
21+ mod precondition;
2122
22- use client_request:: { ClientRequest , PingRequest , VerifyApiTokenRequest } ;
23+ use client_request:: {
24+ ClientRequest , OneShotRequest , PingRequest , VerifyApiTokenRequest , WriteEventsRequest ,
25+ } ;
2326
27+ pub use precondition:: Precondition ;
2428use reqwest;
2529use url:: Url ;
2630
27- use crate :: error:: ClientError ;
31+ use crate :: {
32+ error:: ClientError ,
33+ event:: { Event , EventCandidate } ,
34+ } ;
2835
2936/// Client for an [EventsourcingDB](https://www.eventsourcingdb.io/) instance.
3037#[ derive( Debug ) ]
@@ -72,9 +79,14 @@ impl Client {
7279
7380 /// Utility function to request an endpoint of the API.
7481 ///
82+ /// This function will return a [`reqwest::RequestBuilder`] which can be used to send the request.
83+ ///
7584 /// # Errors
7685 /// This function will return an error if the request fails or if the URL is invalid.
77- async fn request < R : ClientRequest > ( & self , endpoint : R ) -> Result < R :: Response , ClientError > {
86+ fn build_request < R : ClientRequest > (
87+ & self ,
88+ endpoint : & R ,
89+ ) -> Result < reqwest:: RequestBuilder , ClientError > {
7890 let url = self
7991 . base_url
8092 . join ( endpoint. url_path ( ) )
@@ -93,15 +105,27 @@ impl Client {
93105 } else {
94106 request
95107 } ;
108+ Ok ( request)
109+ }
96110
97- let response = request. send ( ) . await ?;
111+ /// Utility function to request an endpoint of the API as a oneshot.
112+ ///
113+ /// This means, that the response is not streamed, but returned as a single value.
114+ ///
115+ /// # Errors
116+ /// This function will return an error if the request fails or if the URL is invalid.
117+ async fn request_oneshot < R : OneShotRequest > (
118+ & self ,
119+ endpoint : R ,
120+ ) -> Result < R :: Response , ClientError > {
121+ let response = self . build_request ( & endpoint) ?. send ( ) . await ?;
98122
99123 if response. status ( ) . is_success ( ) {
100124 let result = response. json ( ) . await ?;
101125 endpoint. validate_response ( & result) ?;
102126 Ok ( result)
103127 } else {
104- Err ( ClientError :: DBError (
128+ Err ( ClientError :: DBApiError (
105129 response. status ( ) ,
106130 response. text ( ) . await . unwrap_or_default ( ) ,
107131 ) )
@@ -125,7 +149,7 @@ impl Client {
125149 /// # Errors
126150 /// This function will return an error if the request fails or if the URL is invalid.
127151 pub async fn ping ( & self ) -> Result < ( ) , ClientError > {
128- let _ = self . request ( PingRequest ) . await ?;
152+ let _ = self . request_oneshot ( PingRequest ) . await ?;
129153 Ok ( ( ) )
130154 }
131155
@@ -146,7 +170,45 @@ impl Client {
146170 /// # Errors
147171 /// This function will return an error if the request fails or if the URL is invalid.
148172 pub async fn verify_api_token ( & self ) -> Result < ( ) , ClientError > {
149- let _ = self . request ( VerifyApiTokenRequest ) . await ?;
173+ let _ = self . request_oneshot ( VerifyApiTokenRequest ) . await ?;
150174 Ok ( ( ) )
151175 }
176+
177+ /// Writes events to the DB instance.
178+ ///
179+ /// ```
180+ /// use eventsourcingdb_client_rust::event::EventCandidate;
181+ /// # use serde_json::json;
182+ /// # tokio_test::block_on(async {
183+ /// # let container = eventsourcingdb_client_rust::container::Container::start_default().await.unwrap();
184+ /// let db_url = "http://localhost:3000/";
185+ /// let api_token = "secrettoken";
186+ /// # let db_url = container.get_base_url().await.unwrap();
187+ /// # let api_token = container.get_api_token();
188+ /// let client = eventsourcingdb_client_rust::client::Client::new(db_url, api_token);
189+ /// let candidates = vec![
190+ /// EventCandidate::builder()
191+ /// .source("https://www.eventsourcingdb.io".to_string())
192+ /// .data(json!({"value": 1}))
193+ /// .subject("/test".to_string())
194+ /// .r#type("io.eventsourcingdb.test".to_string())
195+ /// .build()
196+ /// ];
197+ /// let written_events = client.write_events(candidates, vec![]).await.expect("Failed to write events");
198+ /// # })
199+ /// ```
200+ ///
201+ /// # Errors
202+ /// This function will return an error if the request fails or if the URL is invalid.
203+ pub async fn write_events (
204+ & self ,
205+ events : Vec < EventCandidate > ,
206+ preconditions : Vec < Precondition > ,
207+ ) -> Result < Vec < Event > , ClientError > {
208+ self . request_oneshot ( WriteEventsRequest {
209+ events,
210+ preconditions,
211+ } )
212+ . await
213+ }
152214}
0 commit comments