1
1
//! This is a purely internal module to represent client requests to the database.
2
2
3
- use reqwest:: Method ;
4
- use serde:: Serialize ;
3
+ mod ping;
4
+ mod verify_api_token;
5
+ mod write_events;
5
6
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 ;
10
10
11
- use super :: precondition:: Precondition ;
11
+ use crate :: error:: ClientError ;
12
+ use reqwest:: Method ;
13
+ use serde:: { Serialize , de:: DeserializeOwned } ;
12
14
13
15
/// Represents a request to the database client
14
16
pub trait ClientRequest {
15
17
const URL_PATH : & ' static str ;
16
18
const METHOD : Method ;
17
- type Response : serde:: de:: DeserializeOwned ;
18
19
19
20
/// Returns the URL path for the request
20
21
fn url_path ( & self ) -> & ' static str {
@@ -28,59 +29,16 @@ pub trait ClientRequest {
28
29
29
30
/// Returns the body for the request
30
31
fn body ( & self ) -> Option < Result < impl Serialize , ClientError > > {
31
- None :: < Result < ( ) , ClientError > >
32
+ None :: < Result < ( ) , _ > >
32
33
}
34
+ }
35
+
36
+ /// Represents a request to the database that expects a single response
37
+ pub trait OneShotRequest : ClientRequest {
38
+ type Response : DeserializeOwned ;
33
39
34
40
/// Validate the response from the database
35
41
fn validate_response ( & self , _response : & Self :: Response ) -> Result < ( ) , ClientError > {
36
42
Ok ( ( ) )
37
43
}
38
44
}
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