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,
    },
};

/// Get the profile of a user.
///
/// Maps parameters of https://www.scoop.it/dev/api/1/urls#user
///
/// Documentation of each field comes from the page above. Default values documented are used only
/// ff the field is not present (`None`), `Default` implementation for this struct may differ from
/// Scoop.it defaults to avoid retrieving the world while only looking at the user profile.
#[derive(Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct GetProfileRequest {
    /// string optional - the shortName of the user to lookup - defaults to the current user
    pub short_name: Option<String>,
    /// long optional - the id of the user to lookup - defaults to the current user
    pub id: Option<String>,
    /// bool optional - default to false. returns or not stats for each returned topic
    pub get_stats: bool,
    /// bool optional - default to true. returns or not list of tags for each returned topic
    pub get_tags: bool,
    /// int optional - default to 0, number of curated posts to retrieve for each topic present in user data
    pub curated: Option<u32>,
    /// int optional - default to 0, number of curable posts to retrieve for each topic the current user is the curator (so it should not be specified if the "id" parameter is specified)
    pub curable: Option<u32>,
    /// int optional - default to 0, the maximum number of comments to retrieve for each curated post found in each topic present in user data
    pub ncomments: Option<u32>,
    /// bool optional - default to true. returns or not list of followed topics
    pub get_followed_topics: bool,
    /// bool optional - default to true. returns or not list of curated topics
    pub get_curated_topics: bool,
    ///timestamp optional - default to 0 (unix epoch). Filter curated topics by creation date.
    pub filter_curated_topics_by_creation_date_from: Option<u64>,
    ///timestamp optional - default to 2^63. Filter curated topics by creation date.
    pub filter_curated_topics_by_creation_date_to: Option<u64>,
    /// bool optional - default to true. returns or not creator of each returned topic
    pub get_creator: bool,
}

impl Default for GetProfileRequest {
    fn default() -> Self {
        // sane defaults
        Self {
            short_name: None,
            id: None,
            get_stats: false,           // no stats by default
            get_tags: false,            // no tags by default
            curated: Some(0),           // do not retrieve posts on curated topics
            curable: Some(0),           // do not retrieve suggestion on curated topics
            ncomments: Some(0),         // force no comments
            get_followed_topics: false, // no followed topics by default
            get_curated_topics: true,   // get curated topics by default
            filter_curated_topics_by_creation_date_from: None,
            filter_curated_topics_by_creation_date_to: None,
            get_creator: false,
        }
    }
}

/// Get a Topic.
///
/// Maps parameters of https://www.scoop.it/dev/api/1/urls#topic
///
/// Documentation of each field comes from the page above. Default values documented are used only
/// ff the field is not present (`None`), `Default` implementation for this struct may differ from
/// Scoop.it defaults to avoid retrieving the world while only looking at the user profile.
#[derive(Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct GetTopicRequest {
    /// long required, unless 'urlName' is provided - the id of the topic to lookup
    pub id: Option<u64>,
    /// string required, unless 'id' is provided - the urlName of the topic to lookup
    pub url_name: Option<String>,
    /// int optional, default to 30 - number of curated posts to retrieve for this topic
    pub curated: Option<u32>,
    /// int optional, default to 0
    pub page: Option<u32>,
    /// int optional, default to 0 - for this topic, this parameter is ignored if the current user is not the curator of this topic
    pub curable: Option<u32>,
    /// int optional, default to 0 - for this topic, this parameter is ignored if the current user is not the curator of this topic - get a given page of curable posts
    pub curable_page: Option<u32>,
    /// string mandatory if "since" parameter is not specified - sort order of the curated posts, can be "tag" (see below), "search" (filter result on query "q" mandatory - see below), "curationDate", "user" (same order as seen in the scoop.it website)
    pub order: Option<GetTopicOrder>,
    /// string[] mandatory if "order"=="tag"
    pub tag: Option<Vec<String>>,
    ///  string mandatory if "order"=="search" - the query to use to search in the topic
    pub q: Option<String>,
    ///timestamp - only retrieve curated post newer than this timestamp
    pub since: Option<i64>,
    /// timestamp optional - used with "since" parameter, retrieve curated posts posts older then this timestamp
    pub to: Option<i64>,
    /// int optional, default to 100 - each curated post found in this topic
    pub ncomments: Option<u32>,
    /// boolean optional, default to false - if true, the response will include the scheduled posts
    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,
        }
    }
}

/// Represents a `GET` request.
pub trait GetRequest: Serialize + Debug {
    /// The type returned by the Scoop.it API.
    ///
    /// It must be converible to this trait Output type.
    type Response: TryInto<Self::Output, Error = anyhow::Error> + DeserializeOwned;
    /// The type returned by the client
    type Output;

    fn endpoint(&self) -> Cow<'static, str>;
}

/// A request that does an update, by default the body is serialized as
/// `application/x-www-form-urlencoded` and the method is `POST`
pub trait UpdateRequest: Serialize + Debug {
    /// The type returned by the Scoop.it API.
    ///
    /// It must be convertible to this trait Output type.
    type Response: TryInto<Self::Output, Error = anyhow::Error> + DeserializeOwned;
    /// The type returned by the client
    type Output;

    fn endpoint(&self) -> Cow<'static, str>;

    /// the content type of the post request, by default `application/x-www-form-urlencoded`
    fn content_type() -> &'static str {
        "application/x-www-form-urlencoded; charset=utf-8"
    }

    /// The body as bytes, by default the type implementing this trait is serialized using serde_qs.
    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)),
        }
    }
}

/// Perform a search.
///
/// Maps parameters of https://www.scoop.it/dev/api/1/urls#search
///
/// Documentation of each field comes from the page above. Default values documented are used only
/// ff the field is not present (`None`), `Default` implementation for this struct may differ from
/// Scoop.it defaults to avoid retrieving the world while only looking at the user profile.
#[derive(Serialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct SearchRequest {
    ///string - type of object searched: "user", "topic" or "post"
    #[serde(rename = "type")]
    pub search_type: SearchRequestType,
    /// string - the search query
    pub query: String,
    /// int optional, default to 50 - the number of item per page
    pub count: Option<u32>,
    /// int optional, default to 0 -the page number to return, the first page is 0
    pub page: Option<u32>,
    /// string optional, default to "en" - the language of the content to search into
    pub lang: Option<String>,
    /// long optional - the id of the topic to search posts into
    pub topic_id: Option<u32>,
    /// bool optional, default to true - returns or not list of tags for each returned topic / post. only for type="topic" or type="post"
    pub get_tags: bool,
    /// bool optional, default to true - returns or not creator of each returned topic. only for type="topic"
    pub get_creator: bool,
    /// bool optional, default to true - returns or not stats for each returned topic. only for type="topic"
    pub get_stats: bool,
    /// bool optional, default to true - returns or not tags for topic of each returned post. only for type="post"
    pub get_tags_for_topic: bool,
    /// bool optional, default to true - returns or not stats for topic of each returned post. only for type="post"
    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,
        })
    }
}

/// Get the list of recipients lists
///
/// See https://www.scoop.it/dev/api/1/urls#recipients-list
///
#[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)
    }
}

/// Test authentication credentials.
///
/// https://www.scoop.it/dev/api/1/urls#test
#[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(", ")
            )),
        }
    }
}
/// Get the list of available suggestion engines
///
/// https://www.scoop.it/dev/api/1/urls#se
#[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}"))
            }
        }
    }
}

/// Get manual user sources of a suggestion engine.
///
/// https://www.scoop.it/dev/api/1/urls#se_sources
#[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(()),
        }
    }
}

/// Delete a manually source from a suggestion engine
///
/// https://www.scoop.it/dev/api/1/urls#se_sources_id
#[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
    }
}

/// Update a manually source from a suggestion engine
///
/// https://www.scoop.it/dev/api/1/urls#se_sources_id
#[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()
    }
}

/// Create a suggestion engine source
///
/// https://www.scoop.it/dev/api/1/urls#se_sources_id
#[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}"))
            }
        }
    }
}