19
19
20
20
mod client_request;
21
21
22
- use client_request:: { ClientRequest , PingRequest , VerifyApiTokenRequest } ;
22
+ use client_request:: {
23
+ list_event_types:: EventType , ClientRequest , ListEventTypesRequest , ListSubjectsRequest , OneShotRequest , PingRequest , RegisterEventSchemaRequest , StreamingRequest , VerifyApiTokenRequest
24
+ } ;
23
25
26
+ use futures:: Stream ;
24
27
use reqwest;
25
28
use url:: Url ;
26
29
27
- use crate :: error:: ClientError ;
30
+ use crate :: { error:: ClientError , event :: ManagementEvent } ;
28
31
29
32
/// Client for an [EventsourcingDB](https://www.eventsourcingdb.io/) instance.
30
33
#[ derive( Debug ) ]
@@ -72,9 +75,14 @@ impl Client {
72
75
73
76
/// Utility function to request an endpoint of the API.
74
77
///
78
+ /// This function will return a [`reqwest::RequestBuilder`] which can be used to send the request.
79
+ ///
75
80
/// # Errors
76
81
/// 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 > {
82
+ fn build_request < R : ClientRequest > (
83
+ & self ,
84
+ endpoint : & R ,
85
+ ) -> Result < reqwest:: RequestBuilder , ClientError > {
78
86
let url = self
79
87
. base_url
80
88
. join ( endpoint. url_path ( ) )
@@ -93,15 +101,49 @@ impl Client {
93
101
} else {
94
102
request
95
103
} ;
104
+ Ok ( request)
105
+ }
96
106
97
- let response = request. send ( ) . await ?;
107
+ /// Utility function to request an endpoint of the API as a oneshot.
108
+ ///
109
+ /// This means, that the response is not streamed, but returned as a single value.
110
+ ///
111
+ /// # Errors
112
+ /// This function will return an error if the request fails or if the URL is invalid.
113
+ async fn request_oneshot < R : OneShotRequest > (
114
+ & self ,
115
+ endpoint : R ,
116
+ ) -> Result < R :: Response , ClientError > {
117
+ let response = self . build_request ( & endpoint) ?. send ( ) . await ?;
98
118
99
119
if response. status ( ) . is_success ( ) {
100
120
let result = response. json ( ) . await ?;
101
121
endpoint. validate_response ( & result) ?;
102
122
Ok ( result)
103
123
} else {
104
- Err ( ClientError :: DBError (
124
+ Err ( ClientError :: DBApiError (
125
+ response. status ( ) ,
126
+ response. text ( ) . await . unwrap_or_default ( ) ,
127
+ ) )
128
+ }
129
+ }
130
+
131
+ /// Utility function to request an endpoint of the API as a stream.
132
+ ///
133
+ /// This means, that the response is streamed and returned as a stream of values.
134
+ ///
135
+ /// # Errors
136
+ /// This function will return an error if the request fails or if the URL is invalid.
137
+ async fn request_streaming < R : StreamingRequest > (
138
+ & self ,
139
+ endpoint : R ,
140
+ ) -> Result < impl Stream < Item = Result < R :: ItemType , ClientError > > , ClientError > {
141
+ let response = self . build_request ( & endpoint) ?. send ( ) . await ?;
142
+
143
+ if response. status ( ) . is_success ( ) {
144
+ Ok ( endpoint. build_stream ( response) )
145
+ } else {
146
+ Err ( ClientError :: DBApiError (
105
147
response. status ( ) ,
106
148
response. text ( ) . await . unwrap_or_default ( ) ,
107
149
) )
@@ -125,7 +167,7 @@ impl Client {
125
167
/// # Errors
126
168
/// This function will return an error if the request fails or if the URL is invalid.
127
169
pub async fn ping ( & self ) -> Result < ( ) , ClientError > {
128
- let _ = self . request ( PingRequest ) . await ?;
170
+ let _ = self . request_oneshot ( PingRequest ) . await ?;
129
171
Ok ( ( ) )
130
172
}
131
173
@@ -146,7 +188,45 @@ impl Client {
146
188
/// # Errors
147
189
/// This function will return an error if the request fails or if the URL is invalid.
148
190
pub async fn verify_api_token ( & self ) -> Result < ( ) , ClientError > {
149
- let _ = self . request ( VerifyApiTokenRequest ) . await ?;
191
+ let _ = self . request_oneshot ( VerifyApiTokenRequest ) . await ?;
150
192
Ok ( ( ) )
151
193
}
194
+
195
+ /// Registers an event schema with the DB instance.
196
+ ///
197
+ /// # Errors
198
+ /// This function will return an error if the request fails or if the provided schema is invalid.
199
+ pub async fn register_event_schema (
200
+ & self ,
201
+ event_type : & str ,
202
+ schema : & serde_json:: Value ,
203
+ ) -> Result < ManagementEvent , ClientError > {
204
+ self . request_oneshot ( RegisterEventSchemaRequest :: try_new ( event_type, schema) ?)
205
+ . await
206
+ }
207
+
208
+ /// List all subjects in the DB instance.
209
+ ///
210
+ /// # Errors
211
+ /// This function will return an error if the request fails or if the URL is invalid.
212
+ pub async fn list_subjects (
213
+ & self ,
214
+ base_subject : Option < & str > ,
215
+ ) -> Result < impl Stream < Item = Result < String , ClientError > > , ClientError > {
216
+ let response = self
217
+ . request_streaming ( ListSubjectsRequest { base_subject } )
218
+ . await ?;
219
+ Ok ( response)
220
+ }
221
+
222
+ /// List all event types in the DB instance.
223
+ ///
224
+ /// # Errors
225
+ /// This function will return an error if the request fails or if the URL is invalid.
226
+ pub async fn list_event_types (
227
+ & self ,
228
+ ) -> Result < impl Stream < Item = Result < EventType , ClientError > > , ClientError > {
229
+ let response = self . request_streaming ( ListEventTypesRequest ) . await ?;
230
+ Ok ( response)
231
+ }
152
232
}
0 commit comments