1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
use std::{
borrow::Cow,
convert::{TryFrom, TryInto},
fmt::Debug,
str::FromStr,
};
use anyhow::anyhow;
use reqwest::Method;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use crate::{
serde_qs,
types::{
Post, RecipientsList, SearchResults, Source, SourceTypeData, SuggestionEngine, Topic, User,
},
};
#[derive(Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct GetProfileRequest {
pub short_name: Option<String>,
pub id: Option<String>,
pub get_stats: bool,
pub get_tags: bool,
pub curated: Option<u32>,
pub curable: Option<u32>,
pub ncomments: Option<u32>,
pub get_followed_topics: bool,
pub get_curated_topics: bool,
pub filter_curated_topics_by_creation_date_from: Option<u64>,
pub filter_curated_topics_by_creation_date_to: Option<u64>,
pub get_creator: bool,
}
impl Default for GetProfileRequest {
fn default() -> Self {
Self {
short_name: None,
id: None,
get_stats: false, get_tags: false, curated: Some(0), curable: Some(0), ncomments: Some(0), get_followed_topics: false, get_curated_topics: true, filter_curated_topics_by_creation_date_from: None,
filter_curated_topics_by_creation_date_to: None,
get_creator: false,
}
}
}
#[derive(Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct GetTopicRequest {
pub id: Option<u64>,
pub url_name: Option<String>,
pub curated: Option<u32>,
pub page: Option<u32>,
pub curable: Option<u32>,
pub curable_page: Option<u32>,
pub order: Option<GetTopicOrder>,
pub tag: Option<Vec<String>>,
pub q: Option<String>,
pub since: Option<i64>,
pub to: Option<i64>,
pub ncomments: Option<u32>,
pub show_scheduled: bool,
}
#[derive(Serialize, Debug)]
pub enum GetTopicOrder {
#[serde(rename = "tag")]
Tag,
#[serde(rename = "search")]
Search,
#[serde(rename = "curationDate")]
CurationDate,
#[serde(rename = "user")]
User,
}
impl Default for GetTopicRequest {
fn default() -> Self {
Self {
id: None,
url_name: None,
curated: Some(30),
page: None,
curable: Some(0),
curable_page: None,
order: None,
tag: None,
q: None,
since: None,
to: None,
ncomments: Some(100),
show_scheduled: false,
}
}
}
pub trait GetRequest: Serialize + Debug {
type Response: TryInto<Self::Output, Error = anyhow::Error> + DeserializeOwned;
type Output;
fn endpoint(&self) -> Cow<'static, str>;
}
pub trait UpdateRequest: Serialize + Debug {
type Response: TryInto<Self::Output, Error = anyhow::Error> + DeserializeOwned;
type Output;
fn endpoint(&self) -> Cow<'static, str>;
fn content_type() -> &'static str {
"application/x-www-form-urlencoded; charset=utf-8"
}
fn body(&self) -> anyhow::Result<Vec<u8>> {
Ok(serde_qs::to_string(&self)?.into_bytes())
}
fn method(&self) -> Method {
Method::POST
}
}
impl GetRequest for GetTopicRequest {
type Response = TopicResponse;
type Output = Topic;
fn endpoint(&self) -> Cow<'static, str> {
"topic".into()
}
}
impl GetRequest for GetProfileRequest {
type Response = UserResponse;
type Output = User;
fn endpoint(&self) -> Cow<'static, str> {
"profile".into()
}
}
#[derive(Deserialize)]
pub struct TopicResponse {
pub topic: Option<Topic>,
pub error: Option<String>,
}
#[derive(Deserialize)]
pub struct UserResponse {
pub user: Option<User>,
pub error: Option<String>,
}
impl TryFrom<UserResponse> for User {
type Error = anyhow::Error;
fn try_from(value: UserResponse) -> Result<Self, Self::Error> {
if let Some(error) = value.error {
Err(anyhow::anyhow!("Server returned an error: {}", error))
} else {
value
.user
.ok_or(anyhow::anyhow!("No user nor error in response body!"))
}
}
}
impl TryFrom<TopicResponse> for Topic {
type Error = anyhow::Error;
fn try_from(value: TopicResponse) -> Result<Self, Self::Error> {
if let Some(error) = value.error {
Err(anyhow::anyhow!("Server returned an error: {}", error))
} else {
value
.topic
.ok_or(anyhow::anyhow!("No user no topic in response body!"))
}
}
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub enum SearchRequestType {
User,
Topic,
Post,
}
impl FromStr for SearchRequestType {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"user" => Ok(SearchRequestType::User),
"topic" => Ok(SearchRequestType::Topic),
"post" => Ok(SearchRequestType::Post),
other => Err(anyhow::anyhow!("Invalid request type: {}", other)),
}
}
}
#[derive(Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct SearchRequest {
#[serde(rename = "type")]
pub search_type: SearchRequestType,
pub query: String,
pub count: Option<u32>,
pub page: Option<u32>,
pub lang: Option<String>,
pub topic_id: Option<u32>,
pub get_tags: bool,
pub get_creator: bool,
pub get_stats: bool,
pub get_tags_for_topic: bool,
pub get_stats_for_topic: bool,
}
impl Default for SearchRequest {
fn default() -> Self {
Self {
search_type: SearchRequestType::Post,
query: "".to_string(),
count: Some(50),
page: None,
lang: None,
topic_id: None,
get_tags: false,
get_creator: true,
get_stats: false,
get_tags_for_topic: false,
get_stats_for_topic: false,
}
}
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct SearchResponse {
pub users: Option<Vec<User>>,
pub topics: Option<Vec<Topic>>,
pub posts: Option<Vec<Post>>,
pub total_found: i32,
}
impl GetRequest for SearchRequest {
type Response = SearchResponse;
type Output = SearchResults;
fn endpoint(&self) -> Cow<'static, str> {
"search".into()
}
}
impl TryFrom<SearchResponse> for SearchResults {
type Error = anyhow::Error;
fn try_from(value: SearchResponse) -> Result<Self, Self::Error> {
let SearchResponse {
users,
topics,
posts,
total_found,
} = value;
Ok(SearchResults {
users,
topics,
posts,
total_found,
})
}
}
#[derive(Serialize, Debug, Default)]
pub struct GetRecipientsListRequest {
_dummy: (),
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct GetRecipientsListResponse {
list: Vec<RecipientsList>,
}
impl GetRequest for GetRecipientsListRequest {
type Response = GetRecipientsListResponse;
type Output = Vec<RecipientsList>;
fn endpoint(&self) -> Cow<'static, str> {
"recipients-list".into()
}
}
impl TryFrom<GetRecipientsListResponse> for Vec<RecipientsList> {
type Error = anyhow::Error;
fn try_from(value: GetRecipientsListResponse) -> Result<Self, Self::Error> {
Ok(value.list)
}
}
#[derive(Serialize, Debug, Default)]
pub struct TestRequest {
_dummy: (),
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct TestResponse {
connected_user: Option<String>,
error: Option<String>,
}
impl GetRequest for TestRequest {
type Response = TestResponse;
type Output = Option<String>;
fn endpoint(&self) -> Cow<'static, str> {
"test".into()
}
}
impl TryFrom<TestResponse> for Option<String> {
type Error = anyhow::Error;
fn try_from(value: TestResponse) -> Result<Self, Self::Error> {
if let Some(error) = value.error {
Err(anyhow::anyhow!("Server returned an error: {}", error))
} else {
Ok(value.connected_user)
}
}
}
#[derive(Debug, Serialize)]
pub struct LoginRequest {
pub email: String,
pub password: String,
}
impl UpdateRequest for LoginRequest {
type Response = LoginResponse;
type Output = LoginAccessToken;
fn endpoint(&self) -> Cow<'static, str> {
"login".into()
}
}
#[derive(Debug, Deserialize)]
#[serde(untagged)]
pub enum LoginResponse {
Ok {
#[serde(rename = "accessToken")]
access_token: LoginAccessToken,
},
Err {
errors: Vec<String>,
},
}
#[derive(Debug, Deserialize)]
pub struct LoginAccessToken {
pub oauth_token: String,
pub oauth_token_secret: String,
}
impl TryFrom<LoginResponse> for LoginAccessToken {
type Error = anyhow::Error;
fn try_from(value: LoginResponse) -> Result<Self, Self::Error> {
match value {
LoginResponse::Ok { access_token } => Ok(access_token),
LoginResponse::Err { errors } => Err(anyhow!(
"Unable to login with errors: {}",
errors.join(", ")
)),
}
}
}
#[derive(Debug, Default, Serialize)]
pub struct GetSuggestionEnginesRequest {
_dummy: (),
}
#[derive(Debug, Deserialize)]
#[serde(untagged)]
pub enum GetSuggestionEnginesResponse {
Ok {
suggestion_engines: Vec<SuggestionEngine>,
},
Err {
error: String,
},
}
impl GetRequest for GetSuggestionEnginesRequest {
type Response = GetSuggestionEnginesResponse;
type Output = Vec<SuggestionEngine>;
fn endpoint(&self) -> Cow<'static, str> {
"se".into()
}
}
impl TryFrom<GetSuggestionEnginesResponse> for Vec<SuggestionEngine> {
type Error = anyhow::Error;
fn try_from(value: GetSuggestionEnginesResponse) -> Result<Self, Self::Error> {
match value {
GetSuggestionEnginesResponse::Ok { suggestion_engines } => Ok(suggestion_engines),
GetSuggestionEnginesResponse::Err { error } => {
Err(anyhow!("Server returned an error: {error}"))
}
}
}
}
#[derive(Debug, Serialize)]
pub struct GetSuggestionEngineSourcesRequest {
#[serde(skip)]
pub suggestion_engine_id: i64,
}
#[derive(Debug, Deserialize)]
#[serde(untagged)]
pub enum GetSuggestionEngineSourcesResponse {
Ok { sources: Vec<Source> },
Err { error: String },
}
impl GetRequest for GetSuggestionEngineSourcesRequest {
type Response = GetSuggestionEngineSourcesResponse;
type Output = Vec<Source>;
fn endpoint(&self) -> Cow<'static, str> {
format!("se/{}/sources", self.suggestion_engine_id).into()
}
}
impl TryFrom<GetSuggestionEngineSourcesResponse> for Vec<Source> {
type Error = anyhow::Error;
fn try_from(value: GetSuggestionEngineSourcesResponse) -> Result<Self, Self::Error> {
match value {
GetSuggestionEngineSourcesResponse::Ok { sources } => Ok(sources),
GetSuggestionEngineSourcesResponse::Err { error } => {
Err(anyhow!("Server returned an error: {error}"))
}
}
}
}
#[derive(Deserialize, Debug)]
#[serde(untagged)]
pub enum EmptyUpdateResponse {
Err { error: String },
Ok {},
}
impl EmptyUpdateResponse {
pub fn is_ok(&self) -> bool {
if let EmptyUpdateResponse::Ok {} = &self {
true
} else {
false
}
}
pub fn is_err(&self) -> bool {
!self.is_ok()
}
}
impl TryFrom<EmptyUpdateResponse> for () {
type Error = anyhow::Error;
fn try_from(value: EmptyUpdateResponse) -> Result<Self, Self::Error> {
match value {
EmptyUpdateResponse::Err { error } => Err(anyhow!("Server returned an error: {error}")),
EmptyUpdateResponse::Ok {} => Ok(()),
}
}
}
#[derive(Serialize, Debug)]
pub struct DeleteSuggestionEngineSourceRequest {
#[serde(skip)]
pub suggestion_engine_id: i64,
#[serde(skip)]
pub source_id: i64,
}
impl UpdateRequest for DeleteSuggestionEngineSourceRequest {
type Response = EmptyUpdateResponse;
type Output = ();
fn endpoint(&self) -> Cow<'static, str> {
format!(
"se/{}/sources/{}",
self.suggestion_engine_id, self.source_id
)
.into()
}
fn method(&self) -> Method {
Method::DELETE
}
}
#[derive(Serialize, Debug)]
pub struct UpdateSuggestionEngineSourceRequest {
#[serde(skip)]
pub suggestion_engine_id: i64,
#[serde(skip)]
pub source_id: i64,
pub name: Option<String>,
}
impl UpdateRequest for UpdateSuggestionEngineSourceRequest {
type Response = EmptyUpdateResponse;
type Output = ();
fn endpoint(&self) -> Cow<'static, str> {
format!(
"se/{}/sources/{}",
self.suggestion_engine_id, self.source_id
)
.into()
}
}
#[derive(Serialize, Debug)]
pub struct CreateSuggestionEngineSourceRequest {
#[serde(skip)]
pub suggestion_engine_id: i64,
pub name: Option<String>,
#[serde(flatten)]
pub source_data: SourceTypeData,
}
#[derive(Debug, Deserialize)]
#[serde(untagged)]
pub enum CreateSuggestionEngineSourceResponse {
Ok { source: Source },
Err { error: String },
}
impl UpdateRequest for CreateSuggestionEngineSourceRequest {
type Response = CreateSuggestionEngineSourceResponse;
type Output = Source;
fn endpoint(&self) -> Cow<'static, str> {
format!("se/{}/sources", self.suggestion_engine_id).into()
}
fn method(&self) -> Method {
Method::PUT
}
}
impl TryFrom<CreateSuggestionEngineSourceResponse> for Source {
type Error = anyhow::Error;
fn try_from(value: CreateSuggestionEngineSourceResponse) -> Result<Self, Self::Error> {
match value {
CreateSuggestionEngineSourceResponse::Ok { source } => Ok(source),
CreateSuggestionEngineSourceResponse::Err { error } => {
Err(anyhow!("Server returned an error: {error}"))
}
}
}
}