11//! This is a purely internal module to represent client requests to the database.
22
3- use reqwest:: Method ;
4- use serde:: Serialize ;
3+ mod ping;
4+ mod verify_api_token;
5+ mod write_events;
56
6- use crate :: {
7- error:: ClientError ,
8- event:: { Event , EventCandidate , ManagementEvent } ,
9- } ;
7+ pub use ping:: PingRequest ;
8+ pub use verify_api_token:: VerifyApiTokenRequest ;
9+ pub use write_events:: WriteEventsRequest ;
1010
11- use super :: precondition:: Precondition ;
11+ use crate :: error:: ClientError ;
12+ use reqwest:: Method ;
13+ use serde:: { Serialize , de:: DeserializeOwned } ;
1214
1315/// Represents a request to the database client
1416pub trait ClientRequest {
1517 const URL_PATH : & ' static str ;
1618 const METHOD : Method ;
17- type Response : serde:: de:: DeserializeOwned ;
1819
1920 /// Returns the URL path for the request
2021 fn url_path ( & self ) -> & ' static str {
@@ -28,59 +29,16 @@ pub trait ClientRequest {
2829
2930 /// Returns the body for the request
3031 fn body ( & self ) -> Option < Result < impl Serialize , ClientError > > {
31- None :: < Result < ( ) , ClientError > >
32+ None :: < Result < ( ) , _ > >
3233 }
34+ }
35+
36+ /// Represents a request to the database that expects a single response
37+ pub trait OneShotRequest : ClientRequest {
38+ type Response : DeserializeOwned ;
3339
3440 /// Validate the response from the database
3541 fn validate_response ( & self , _response : & Self :: Response ) -> Result < ( ) , ClientError > {
3642 Ok ( ( ) )
3743 }
3844}
39-
40- /// Ping the Database instance
41- #[ derive( Debug , Clone , Copy ) ]
42- pub struct PingRequest ;
43-
44- impl ClientRequest for PingRequest {
45- const URL_PATH : & ' static str = "/api/v1/ping" ;
46- const METHOD : Method = Method :: GET ;
47- type Response = ManagementEvent ;
48-
49- fn validate_response ( & self , response : & Self :: Response ) -> Result < ( ) , ClientError > {
50- ( response. ty ( ) == "io.eventsourcingdb.api.ping-received" )
51- . then_some ( ( ) )
52- . ok_or ( ClientError :: PingFailed )
53- }
54- }
55-
56- /// Verify the API token
57- #[ derive( Debug , Clone , Copy ) ]
58- pub struct VerifyApiTokenRequest ;
59-
60- impl ClientRequest for VerifyApiTokenRequest {
61- const URL_PATH : & ' static str = "/api/v1/verify-api-token" ;
62- const METHOD : Method = Method :: POST ;
63- type Response = ManagementEvent ;
64-
65- fn validate_response ( & self , response : & Self :: Response ) -> Result < ( ) , ClientError > {
66- ( response. ty ( ) == "io.eventsourcingdb.api.api-token-verified" )
67- . then_some ( ( ) )
68- . ok_or ( ClientError :: APITokenInvalid )
69- }
70- }
71-
72- #[ derive( Debug , Serialize ) ]
73- pub struct WriteEvents {
74- pub events : Vec < EventCandidate > ,
75- pub preconditions : Vec < Precondition > ,
76- }
77-
78- impl ClientRequest for WriteEvents {
79- const URL_PATH : & ' static str = "/api/v1/write-events" ;
80- const METHOD : Method = Method :: POST ;
81- type Response = Vec < Event > ;
82-
83- fn body ( & self ) -> Option < Result < impl Serialize , ClientError > > {
84- Some ( Ok ( self ) )
85- }
86- }
0 commit comments