19
19
20
20
mod client_request;
21
21
22
- use client_request:: ClientRequest ;
22
+ use client_request:: { ClientRequest , PingRequest , VerifyApiTokenRequest } ;
23
23
24
24
use reqwest;
25
25
use url:: Url ;
26
26
27
- use crate :: { error:: ClientError , event :: ManagementEvent } ;
27
+ use crate :: error:: ClientError ;
28
28
29
29
/// Client for an [EventsourcingDB](https://www.eventsourcingdb.io/) instance.
30
30
#[ derive( Debug ) ]
@@ -74,7 +74,7 @@ impl Client {
74
74
///
75
75
/// # Errors
76
76
/// This function will return an error if the request fails or if the URL is invalid.
77
- async fn request ( & self , endpoint : ClientRequest ) -> Result < reqwest :: Response , ClientError > {
77
+ async fn request < R : ClientRequest > ( & self , endpoint : R ) -> Result < R :: Response , ClientError > {
78
78
let url = self
79
79
. base_url
80
80
. join ( endpoint. url_path ( ) )
@@ -86,7 +86,7 @@ impl Client {
86
86
_ => return Err ( ClientError :: InvalidRequestMethod ) ,
87
87
}
88
88
. header ( "Authorization" , format ! ( "Bearer {}" , self . api_token) ) ;
89
- let request = if let Some ( body) = endpoint. json ( ) {
89
+ let request = if let Some ( body) = endpoint. body ( ) {
90
90
request
91
91
. header ( "Content-Type" , "application/json" )
92
92
. json ( & body?)
@@ -97,7 +97,9 @@ impl Client {
97
97
let response = request. send ( ) . await ?;
98
98
99
99
if response. status ( ) . is_success ( ) {
100
- Ok ( response)
100
+ let result = response. json ( ) . await ?;
101
+ endpoint. validate_response ( & result) ?;
102
+ Ok ( result)
101
103
} else {
102
104
Err ( ClientError :: DBError (
103
105
response. status ( ) ,
@@ -107,7 +109,7 @@ impl Client {
107
109
}
108
110
109
111
/// Pings the DB instance to check if it is reachable.
110
- ///
112
+ ///
111
113
/// ```
112
114
/// # tokio_test::block_on(async {
113
115
/// # let container = eventsourcingdb_client_rust::container::Container::start_default().await.unwrap();
@@ -123,17 +125,12 @@ impl Client {
123
125
/// # Errors
124
126
/// This function will return an error if the request fails or if the URL is invalid.
125
127
pub async fn ping ( & self ) -> Result < ( ) , ClientError > {
126
- let response = self . request ( ClientRequest :: Ping ) . await ?;
127
- if response. json :: < ManagementEvent > ( ) . await ?. ty ( ) == "io.eventsourcingdb.api.ping-received"
128
- {
129
- Ok ( ( ) )
130
- } else {
131
- Err ( ClientError :: PingFailed )
132
- }
128
+ let _ = self . request ( PingRequest ) . await ?;
129
+ Ok ( ( ) )
133
130
}
134
131
135
132
/// Verifies the API token by sending a request to the DB instance.
136
- ///
133
+ ///
137
134
/// ```
138
135
/// # tokio_test::block_on(async {
139
136
/// # let container = eventsourcingdb_client_rust::container::Container::start_default().await.unwrap();
@@ -149,13 +146,7 @@ impl Client {
149
146
/// # Errors
150
147
/// This function will return an error if the request fails or if the URL is invalid.
151
148
pub async fn verify_api_token ( & self ) -> Result < ( ) , ClientError > {
152
- let response = self . request ( ClientRequest :: VerifyApiToken ) . await ?;
153
- if response. json :: < ManagementEvent > ( ) . await ?. ty ( )
154
- == "io.eventsourcingdb.api.api-token-verified"
155
- {
156
- Ok ( ( ) )
157
- } else {
158
- Err ( ClientError :: APITokenInvalid )
159
- }
149
+ let _ = self . request ( VerifyApiTokenRequest ) . await ?;
150
+ Ok ( ( ) )
160
151
}
161
152
}
0 commit comments