1919
2020mod client_request;
2121
22- use client_request:: ClientRequest ;
22+ use client_request:: { ClientRequest , PingRequest , VerifyApiTokenRequest } ;
2323
2424use reqwest;
2525use url:: Url ;
2626
27- use crate :: { error:: ClientError , event :: ManagementEvent } ;
27+ use crate :: error:: ClientError ;
2828
2929/// Client for an [EventsourcingDB](https://www.eventsourcingdb.io/) instance.
3030#[ derive( Debug ) ]
@@ -74,7 +74,7 @@ impl Client {
7474 ///
7575 /// # Errors
7676 /// 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 > {
7878 let url = self
7979 . base_url
8080 . join ( endpoint. url_path ( ) )
@@ -86,7 +86,7 @@ impl Client {
8686 _ => return Err ( ClientError :: InvalidRequestMethod ) ,
8787 }
8888 . 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 ( ) {
9090 request
9191 . header ( "Content-Type" , "application/json" )
9292 . json ( & body?)
@@ -97,7 +97,9 @@ impl Client {
9797 let response = request. send ( ) . await ?;
9898
9999 if response. status ( ) . is_success ( ) {
100- Ok ( response)
100+ let result = response. json ( ) . await ?;
101+ endpoint. validate_response ( & result) ?;
102+ Ok ( result)
101103 } else {
102104 Err ( ClientError :: DBError (
103105 response. status ( ) ,
@@ -107,7 +109,7 @@ impl Client {
107109 }
108110
109111 /// Pings the DB instance to check if it is reachable.
110- ///
112+ ///
111113 /// ```
112114 /// # tokio_test::block_on(async {
113115 /// # let container = eventsourcingdb_client_rust::container::Container::start_default().await.unwrap();
@@ -123,17 +125,12 @@ impl Client {
123125 /// # Errors
124126 /// This function will return an error if the request fails or if the URL is invalid.
125127 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 ( ( ) )
133130 }
134131
135132 /// Verifies the API token by sending a request to the DB instance.
136- ///
133+ ///
137134 /// ```
138135 /// # tokio_test::block_on(async {
139136 /// # let container = eventsourcingdb_client_rust::container::Container::start_default().await.unwrap();
@@ -149,13 +146,7 @@ impl Client {
149146 /// # Errors
150147 /// This function will return an error if the request fails or if the URL is invalid.
151148 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 ( ( ) )
160151 }
161152}
0 commit comments