15
15
16
16
pub mod account;
17
17
pub mod core;
18
+ pub mod event;
18
19
pub mod jwt;
19
20
pub mod kvs;
21
+ mod rpc;
20
22
pub mod transactor;
21
23
22
24
pub use reqwest_middleware:: { ClientWithMiddleware as HttpClient , RequestBuilder } ;
@@ -27,20 +29,28 @@ use std::time::Duration;
27
29
use reqwest:: { self , Response , Url } ;
28
30
use reqwest:: { StatusCode , header:: HeaderValue } ;
29
31
use reqwest_middleware:: ClientBuilder ;
30
- use reqwest_retry:: {
31
- RetryTransientMiddleware , Retryable , RetryableStrategy , default_on_request_failure,
32
- policies:: ExponentialBackoff ,
33
- } ;
34
32
use secrecy:: SecretString ;
35
33
use serde:: { Deserialize , Serialize , de:: DeserializeOwned } ;
36
34
use serde_json:: { self as json, Value } ;
37
35
use tracing:: * ;
38
36
39
37
use crate :: services:: core:: { AccountUuid , WorkspaceUuid } ;
38
+ use crate :: services:: transactor:: backend:: http:: HttpBackend ;
39
+ use crate :: services:: transactor:: backend:: ws:: { WsBackend , WsBackendOpts } ;
40
+ use crate :: { Error , Result , config:: Config } ;
41
+ use account:: AccountClient ;
42
+ use jwt:: Claims ;
43
+ use kvs:: KvsClient ;
44
+ use transactor:: TransactorClient ;
45
+
40
46
#[ cfg( feature = "kafka" ) ]
41
47
use crate :: services:: transactor:: kafka;
42
- use crate :: { Error , Result , config:: Config } ;
43
- use { account:: AccountClient , jwt:: Claims , kvs:: KvsClient , transactor:: TransactorClient } ;
48
+
49
+ #[ cfg( feature = "reqwest_middleware" ) ]
50
+ use reqwest_retry:: {
51
+ RetryTransientMiddleware , Retryable , RetryableStrategy , default_on_request_failure,
52
+ policies:: ExponentialBackoff ,
53
+ } ;
44
54
45
55
pub trait RequestBuilderExt {
46
56
fn send_ext ( self ) -> impl Future < Output = Result < Response > > ;
@@ -54,11 +64,12 @@ pub trait BasePathProvider {
54
64
fn provide_base_path ( & self ) -> & Url ;
55
65
}
56
66
57
- pub trait ForceHttpScheme {
67
+ pub trait ForceScheme {
58
68
fn force_http_scheme ( self ) -> Url ;
69
+ fn force_ws_scheme ( self ) -> Url ;
59
70
}
60
71
61
- impl ForceHttpScheme for Url {
72
+ impl ForceScheme for Url {
62
73
fn force_http_scheme ( mut self ) -> Url {
63
74
match self . scheme ( ) {
64
75
"ws" => {
@@ -74,6 +85,24 @@ impl ForceHttpScheme for Url {
74
85
75
86
self
76
87
}
88
+
89
+ fn force_ws_scheme ( mut self ) -> Url {
90
+ match self . scheme ( ) {
91
+ "http" => {
92
+ self . set_scheme ( "ws" ) . unwrap ( ) ;
93
+ }
94
+
95
+ "https" => {
96
+ self . set_scheme ( "wss" ) . unwrap ( ) ;
97
+ }
98
+
99
+ "ws" | "wss" => { }
100
+
101
+ _ => panic ! ( ) ,
102
+ } ;
103
+
104
+ self
105
+ }
77
106
}
78
107
79
108
impl RequestBuilderExt for RequestBuilder {
@@ -116,13 +145,13 @@ fn from_value<T: DeserializeOwned>(value: Value) -> Result<T> {
116
145
pub trait JsonClient {
117
146
fn get < U : TokenProvider , R : DeserializeOwned > (
118
147
& self ,
119
- user : U ,
148
+ user : & U ,
120
149
url : Url ,
121
150
) -> impl Future < Output = Result < R > > ;
122
151
123
152
fn post < U : TokenProvider , Q : Serialize , R : DeserializeOwned > (
124
153
& self ,
125
- user : U ,
154
+ user : & U ,
126
155
url : Url ,
127
156
body : & Q ,
128
157
) -> impl Future < Output = Result < R > > ;
@@ -134,7 +163,7 @@ impl JsonClient for HttpClient {
134
163
skip( self , user, url) ,
135
164
fields( %url, method = "get" , type = "json" )
136
165
) ]
137
- async fn get < U : TokenProvider , R : DeserializeOwned > ( & self , user : U , url : Url ) -> Result < R > {
166
+ async fn get < U : TokenProvider , R : DeserializeOwned > ( & self , user : & U , url : Url ) -> Result < R > {
138
167
trace ! ( "request" ) ;
139
168
140
169
let mut request = self . get ( url. clone ( ) ) ;
@@ -148,7 +177,7 @@ impl JsonClient for HttpClient {
148
177
149
178
async fn post < U : TokenProvider , Q : Serialize , R : DeserializeOwned > (
150
179
& self ,
151
- user : U ,
180
+ user : & U ,
152
181
url : Url ,
153
182
body : & Q ,
154
183
) -> Result < R > {
@@ -170,7 +199,7 @@ impl JsonClient for HttpClient {
170
199
}
171
200
}
172
201
173
- #[ derive( Deserialize , Debug , Clone , strum:: Display ) ]
202
+ #[ derive( Serialize , Deserialize , Debug , Clone , strum:: Display ) ]
174
203
#[ serde( rename_all = "UPPERCASE" ) ]
175
204
pub enum Severity {
176
205
Ok ,
@@ -179,11 +208,11 @@ pub enum Severity {
179
208
Error ,
180
209
}
181
210
182
- #[ derive( Deserialize , Debug , Clone , thiserror:: Error ) ]
211
+ #[ derive( Serialize , Deserialize , Debug , Clone , thiserror:: Error ) ]
183
212
pub struct Status {
184
213
pub severity : Severity ,
185
214
pub code : String ,
186
- pub params : HashMap < String , String > ,
215
+ pub params : HashMap < String , Value > ,
187
216
}
188
217
189
218
impl std:: fmt:: Display for Status {
@@ -431,7 +460,11 @@ impl ServiceFactory {
431
460
)
432
461
}
433
462
434
- pub fn new_transactor_client ( & self , base : Url , claims : & Claims ) -> Result < TransactorClient > {
463
+ pub fn new_transactor_client (
464
+ & self ,
465
+ base : Url ,
466
+ claims : & Claims ,
467
+ ) -> Result < TransactorClient < HttpBackend > > {
435
468
TransactorClient :: new (
436
469
self . transactor_http . clone ( ) ,
437
470
base,
@@ -445,15 +478,45 @@ impl ServiceFactory {
445
478
)
446
479
}
447
480
481
+ pub async fn new_transactor_client_ws (
482
+ & self ,
483
+ base : Url ,
484
+ claims : & Claims ,
485
+ opts : WsBackendOpts ,
486
+ ) -> Result < TransactorClient < WsBackend > > {
487
+ TransactorClient :: new_ws (
488
+ base,
489
+ claims. workspace ( ) ?,
490
+ claims. encode (
491
+ self . config
492
+ . token_secret
493
+ . as_ref ( )
494
+ . ok_or ( Error :: Other ( "NoSecret" ) ) ?,
495
+ ) ?,
496
+ opts,
497
+ )
498
+ . await
499
+ }
500
+
448
501
pub fn new_transactor_client_from_token (
449
502
& self ,
450
503
base : Url ,
451
504
workspace : WorkspaceUuid ,
452
505
token : impl Into < SecretString > ,
453
- ) -> Result < TransactorClient > {
506
+ ) -> Result < TransactorClient < HttpBackend > > {
454
507
TransactorClient :: new ( self . transactor_http . clone ( ) , base, workspace, token)
455
508
}
456
509
510
+ pub async fn new_transactor_client_ws_from_token (
511
+ & self ,
512
+ base : Url ,
513
+ workspace : WorkspaceUuid ,
514
+ token : impl Into < SecretString > ,
515
+ opts : WsBackendOpts ,
516
+ ) -> Result < TransactorClient < WsBackend > > {
517
+ TransactorClient :: new_ws ( base, workspace, token, opts) . await
518
+ }
519
+
457
520
#[ cfg( feature = "kafka" ) ]
458
521
pub fn new_kafka_publisher ( & self , topic : & str ) -> Result < kafka:: KafkaProducer > {
459
522
kafka:: KafkaProducer :: new ( & self . config , topic)
0 commit comments