1
- use super :: error:: { ApiError , ApiErrorResponse } ;
1
+ use crate :: ws:: Queryable ;
2
+ #[ allow( unused) ]
3
+ use crate :: ws:: TimedEvent ;
2
4
3
- use super :: models :: base :: { Endpoint , Model , RTArray , RTObject } ;
5
+ use super :: error :: ApiError ;
4
6
5
7
#[ derive( Default , Debug , Clone ) ]
6
8
pub struct Client {
@@ -15,35 +17,11 @@ impl Client {
15
17
16
18
// impl FETCH
17
19
impl Client {
18
- pub async fn fetch < T > ( & self ) -> Result < T , ApiError >
20
+ pub async fn fetch < T > ( & self ) -> Result < T :: Return , ApiError >
19
21
where
20
- T : Model + Endpoint + RTObject ,
22
+ T : Queryable ,
21
23
{
22
- let response = self . session . get ( T :: endpoint_en ( ) ) . send ( ) . await ?;
23
-
24
- if response. status ( ) . is_success ( ) {
25
- dbg ! ( "Request came through" ) ;
26
- let json_result = response. json :: < T > ( ) . await ?;
27
- Ok ( json_result)
28
- } else {
29
- let error_response = response. json :: < ApiErrorResponse > ( ) . await ?;
30
- Err ( ApiError :: ApiError ( error_response) )
31
- }
32
- }
33
-
34
- pub async fn fetch_arr < T > ( & self ) -> Result < Vec < T > , ApiError >
35
- where
36
- T : Model + Endpoint + RTArray ,
37
- {
38
- let response = self . session . get ( T :: endpoint_en ( ) ) . send ( ) . await ?;
39
-
40
- if response. status ( ) . is_success ( ) {
41
- let json_result = response. json :: < Vec < T > > ( ) . await ?;
42
- Ok ( json_result)
43
- } else {
44
- let error_response = response. json :: < ApiErrorResponse > ( ) . await ?;
45
- Err ( ApiError :: ApiError ( error_response) )
46
- }
24
+ <T as Queryable >:: query ( & self . session ) . await
47
25
}
48
26
}
49
27
@@ -58,7 +36,7 @@ impl Client {
58
36
///
59
37
/// # Generic Constraints
60
38
///
61
- /// - `T`: Must implement the `Model`, `Endpoint`, `RTObject`, and `TimedEvent` traits.
39
+ /// - `T`: Must implement the `Queryable` and `TimedEvent` traits.
62
40
/// - `Callback`: Must implement the `ListenerCallback` trait with a lifetime parameter `'any` and type parameter `T`.
63
41
///
64
42
/// # Returns
@@ -92,7 +70,7 @@ impl Client {
92
70
/// ```
93
71
pub async fn call_on_update < T , Callback > ( & self , callback : Callback ) -> Result < ( ) , ApiError >
94
72
where
95
- T : Model + Endpoint + RTObject + crate :: worldstate :: models :: TimedEvent ,
73
+ T : TimedEvent + Queryable < Return = T > ,
96
74
for < ' any > Callback : crate :: worldstate:: listener:: ListenerCallback < ' any , T > ,
97
75
{
98
76
log:: debug!( "{} (LISTENER) :: Started" , std:: any:: type_name:: <T >( ) ) ;
@@ -134,7 +112,7 @@ impl Client {
134
112
///
135
113
/// # Generic Constraints
136
114
///
137
- /// - `T`: Must implement the `Model `, `Endpoint`, `RTObject`, and `TimedEvent ` traits.
115
+ /// - `T`: Must implement the `Queryable `, `TimedEvent` and `PartialEq ` traits.
138
116
/// - `Callback`: Must implement the `ListenerCallback` trait with a lifetime parameter `'any` and type parameter `T`.
139
117
///
140
118
/// # Returns
@@ -180,11 +158,11 @@ impl Client {
180
158
callback : Callback ,
181
159
) -> Result < ( ) , ApiError >
182
160
where
183
- T : Model + Endpoint + RTArray + crate :: worldstate :: models :: TimedEvent + PartialEq ,
161
+ T : TimedEvent + Queryable < Return = Vec < T > > + PartialEq ,
184
162
for < ' any > Callback : crate :: worldstate:: listener:: NestedListenerCallback < ' any , T > ,
185
163
{
186
164
log:: debug!( "{} (LISTENER) :: Started" , std:: any:: type_name:: <Vec <T >>( ) ) ;
187
- let mut items = self . fetch_arr :: < T > ( ) . await ?;
165
+ let mut items = self . fetch :: < T > ( ) . await ?;
188
166
189
167
loop {
190
168
tokio:: time:: sleep ( std:: time:: Duration :: from_secs ( 30 ) ) . await ;
@@ -193,7 +171,7 @@ impl Client {
193
171
"{} (LISTENER) :: Fetching new possible state" ,
194
172
std:: any:: type_name:: <Vec <T >>( )
195
173
) ;
196
- let new_items = self . fetch_arr :: < T > ( ) . await ?;
174
+ let new_items = self . fetch :: < T > ( ) . await ?;
197
175
198
176
let diff = crate :: worldstate:: listener:: CrossDiff :: new ( & items, & new_items) ;
199
177
@@ -229,7 +207,7 @@ impl Client {
229
207
/// # Generic Parameters
230
208
///
231
209
/// - `S`: The type of the state object. It must be `Sized`, `Send`, `Sync`, and `Clone`.
232
- /// - `T`: The type of the item. It must implement the `Model`, `Endpoint`, `RTObject`, and `TimedEvent` traits.
210
+ /// - `T`: Must implement the `Queryable` and `TimedEvent` traits.
233
211
/// - `Callback`: The type of the callback function. It must implement the `StatefulListenerCallback` trait with the item type `T` and the state type `S`.
234
212
///
235
213
/// # Returns
@@ -283,7 +261,7 @@ impl Client {
283
261
) -> Result < ( ) , ApiError >
284
262
where
285
263
S : Sized + Send + Sync + Clone ,
286
- T : Model + Endpoint + RTObject + crate :: worldstate :: models :: TimedEvent ,
264
+ T : TimedEvent + Queryable < Return = T > ,
287
265
for < ' any > Callback : crate :: worldstate:: listener:: StatefulListenerCallback < ' any , T , S > ,
288
266
{
289
267
let mut item = self . fetch :: < T > ( ) . await ?;
@@ -328,7 +306,7 @@ impl Client {
328
306
/// # Generic Constraints
329
307
///
330
308
/// * `S` - The type of the state, which must be `Sized`, `Send`, `Sync`, and `Clone`.
331
- /// * `T` - The type of the items, which must implement the `Model `, `Endpoint`, `RTArray`, ` TimedEvent`, and `PartialEq` traits.
309
+ /// * `T` - Must implement the `Queryable `, `TimedEvent` and `PartialEq` traits.
332
310
/// * `Callback` - The type of the callback function, which must implement the `StatefulNestedListenerCallback` trait.
333
311
///
334
312
/// # Returns
@@ -383,11 +361,11 @@ impl Client {
383
361
) -> Result < ( ) , ApiError >
384
362
where
385
363
S : Sized + Send + Sync + Clone ,
386
- T : Model + Endpoint + RTArray + crate :: worldstate :: models :: TimedEvent + PartialEq ,
364
+ T : Queryable < Return = Vec < T > > + TimedEvent + PartialEq ,
387
365
for < ' any > Callback : crate :: worldstate:: listener:: StatefulNestedListenerCallback < ' any , T , S > ,
388
366
{
389
367
log:: debug!( "{} (LISTENER) :: Started" , std:: any:: type_name:: <Vec <T >>( ) ) ;
390
- let mut items = self . fetch_arr :: < T > ( ) . await ?;
368
+ let mut items = self . fetch :: < T > ( ) . await ?;
391
369
392
370
loop {
393
371
tokio:: time:: sleep ( std:: time:: Duration :: from_secs ( 30 ) ) . await ;
@@ -396,7 +374,7 @@ impl Client {
396
374
"{} (LISTENER) :: Fetching new possible state" ,
397
375
std:: any:: type_name:: <Vec <T >>( )
398
376
) ;
399
- let new_items = self . fetch_arr :: < T > ( ) . await ?;
377
+ let new_items = self . fetch :: < T > ( ) . await ?;
400
378
401
379
let diff = crate :: worldstate:: listener:: CrossDiff :: new ( & items, & new_items) ;
402
380
@@ -424,47 +402,11 @@ impl Client {
424
402
impl Client {
425
403
pub async fn fetch_using_lang < T > (
426
404
& self ,
427
- language : super :: language :: Language ,
428
- ) -> Result < T , ApiError >
405
+ language : crate :: ws :: Language ,
406
+ ) -> Result < T :: Return , ApiError >
429
407
where
430
- T : Model + Endpoint + RTObject ,
408
+ T : Queryable ,
431
409
{
432
- let response = self
433
- . session
434
- . get ( T :: endpoint ( language) )
435
- . send ( )
436
- . await
437
- . unwrap ( ) ;
438
-
439
- if response. status ( ) . is_success ( ) {
440
- let json_result = response. json :: < T > ( ) . await ?;
441
- Ok ( json_result)
442
- } else {
443
- let error_response = response. json :: < ApiErrorResponse > ( ) . await ?;
444
- Err ( ApiError :: ApiError ( error_response) )
445
- }
446
- }
447
-
448
- pub async fn fetch_arr_using_lang < T > (
449
- & self ,
450
- language : super :: language:: Language ,
451
- ) -> Result < Vec < T > , ApiError >
452
- where
453
- T : Model + Endpoint + RTArray ,
454
- {
455
- let response = self
456
- . session
457
- . get ( T :: endpoint ( language) )
458
- . send ( )
459
- . await
460
- . unwrap ( ) ;
461
-
462
- if response. status ( ) . is_success ( ) {
463
- let json_result = response. json :: < Vec < T > > ( ) . await ?;
464
- Ok ( json_result)
465
- } else {
466
- let error_response = response. json :: < ApiErrorResponse > ( ) . await ?;
467
- Err ( ApiError :: ApiError ( error_response) )
468
- }
410
+ T :: query_with_language ( & self . session , language) . await
469
411
}
470
412
}
0 commit comments