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
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
#![deny(warnings)]
#![deny(missing_docs)]
//! Interface to the [remember the milk](https://www.rememberthemilk.com/) to-do
//! app via the [REST API](https://www.rememberthemilk.com/services/api/).
//!
//! This crate is unofficial and not not supported by remember the milk.  To use
//! it, you will need a free for non-commercial use [API
//! key](https://www.rememberthemilk.com/services/api/), which is not included
//! with the crate.
//!
//! Before doing anything else, you need to get an [API] object which needs your
//! API key and secret, and authenticate with the API - this means both your
//! application key and the user's account.
//!
//! ```no_run
//! # #[tokio::main]
//! # async fn main() -> Result<(), failure::Error> {
//! // Create the API object
//! # use rememberthemilk::{API, Perms};
//! let mut rtm_api = API::new("my key".to_string(), "my secret".to_string());
//! // Begin authentication using your API key
//! let auth = rtm_api.start_auth(Perms::Read).await?;
//! // auth.url is a URL which the user should visit to authorise the application
//! // using their rememberthemilk.com account.  The user needs to visit this URL
//! // and sign in before continuing below.
//! if rtm_api.check_auth(&auth).await? {
//!    // Successful authentication!  Can continue to use the API now.
//! }
//! # Ok(())
//! # }
//! ```
//!
//! If the authentication is successful, the [API](API) object will have an
//! authentication token which can be re-used later.  See [to_config](API::to_config)
//! and [from_config](API::from_config) which can be used to save the token and
//! API keys (so they should be stored somewhere relatively secure).
//!
//! The rest of the API can then be used:
//!
//! ```no_run
//! # #[tokio::main]
//! # async fn main() -> Result<(), failure::Error> {
//! # use rememberthemilk::API;
//! # let api: API = unimplemented!();
//! let tasks = api.get_all_tasks().await?;
//! for list in tasks.list {
//!    if let Some(v) = list.taskseries {
//!        for ts in v {
//!            println!("  {}", ts.name);
//!        }
//!    }
//! }
//! # Ok(())
//! # }
//! ```
use chrono::{DateTime, Duration, Utc};
use failure::{bail, Error};
use serde::{de::Unexpected, Deserialize, Serialize};
use serde_json::from_str;

#[cfg(test)]
fn get_auth_url() -> String {
    mockito::server_url()
}

#[cfg(not(test))]
fn get_auth_url() -> String {
    static MILK_AUTH_URL: &str = "https://www.rememberthemilk.com/services/auth/";
    MILK_AUTH_URL.to_string()
}

#[cfg(test)]
fn get_rest_url() -> String {
    mockito::server_url()
}

#[cfg(not(test))]
fn get_rest_url() -> String {
    static MILK_REST_URL: &str = "https://api.rememberthemilk.com/services/rest/";
    MILK_REST_URL.to_string()
}
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
#[serde(rename = "err")]
/// Error type for Remember the Milk API calls.
pub struct RTMError {
    code: isize,
    msg: String,
}

#[derive(Serialize, Deserialize, Default)]
/// rememberthemilk API and authentication configuration.
/// This holds the persistent state for the app authentication
/// and possibly user authentication.
pub struct RTMConfig {
    /// The rememberthemilk API key.  See [RTM API](https://www.rememberthemilk.com/services/api/)
    /// to request an API key and secret.
    pub api_key: Option<String>,
    /// The rememberthemilk API secret.  See [RTM API](https://www.rememberthemilk.com/services/api/)
    /// to request an API key and secret.
    pub api_secret: Option<String>,
    /// A user authentication token retrieved from rememberthemilk.  This can be `None` but the user
    /// will need to authenticate before using the API.
    pub token: Option<String>,
    /// Details of the currently authenticated user.
    pub user: Option<User>,
}

impl RTMConfig {
    /// Clear any user-specific data (auth tokens, user info, etc.)
    pub fn clear_user_data(&mut self) {
        self.token = None;
        self.user = None;
    }
}

/// The rememberthemilk API object.  All rememberthemilk operations are done using methods on here.
pub struct API {
    api_key: String,
    api_secret: String,
    token: Option<String>,
    user: Option<User>,
}

#[derive(Deserialize, Debug, Serialize, Eq, PartialEq)]
struct FrobResponse {
    stat: Stat,
    frob: String,
}

#[derive(Deserialize, Serialize, Debug, Eq, PartialEq, Copy, Clone)]
#[serde(rename_all = "lowercase")]
/// rememberthemilk API permissions.
pub enum Perms {
    /// Permission to read the user's tasks and other data
    Read,
    /// Permission to modify the user's tasks and other data, but
    /// not to delete tasks.  This includes Read permission.
    Write,
    /// Permission to modify the user's tasks and other data, including
    /// deleting tasks.
    Delete,
}

impl Perms {
    /// Return true if this permission includes the rights to do `other`.
    fn includes(self, other: Perms) -> bool {
        match (self, other) {
            (Self::Delete, _)
            | (Self::Write, Self::Read)
            | (Self::Write, Self::Write)
            | (Self::Read, Self::Read) => true,
            _ => false,
        }
    }

    /// Return a string representation suitable for the RTM API
    fn as_str(self) -> &'static str {
        match self {
            Self::Read => "read",
            Self::Write => "write",
            Self::Delete => "delete",
        }
    }
}

impl ToString for Perms {
    fn to_string(&self) -> String {
        self.as_str().to_string()
    }
}

impl std::str::FromStr for Perms {
    type Err = String;
    fn from_str(s: &str) -> Result<Self, String> {
        match s {
            "read" => Ok(Self::Read),
            "write" => Ok(Self::Write),
            "delete" => Ok(Self::Delete),
            _ => Err("Invalid perms string".into()),
        }
    }
}

#[derive(Deserialize, Serialize, Debug, Eq, PartialEq, Clone)]
/// Information about a rememberthemilk user.
pub struct User {
    id: String,
    username: String,
    fullname: String,
}

#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
#[serde(rename = "auth")]
struct Auth {
    token: String,
    perms: Perms,
    user: User,
}

#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
#[serde(rename_all = "lowercase")]
enum Stat {
    Ok,
    Fail,
}

#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
struct AuthResponse {
    stat: Stat,
    auth: Auth,
}

trait RTMToResult {
    type Type;
    fn into_result(self) -> Result<Self::Type, RTMError>;
}

impl RTMToResult for AuthResponse {
    type Type = Auth;
    fn into_result(self) -> Result<Auth, RTMError> {
        match self.stat {
            Stat::Ok => Ok(self.auth),
            Stat::Fail => panic!(),
        }
    }
}

use serde::de::IntoDeserializer;

// Thanks to https://github.com/serde-rs/serde/issues/1425#issuecomment-462282398
fn empty_string_as_none<'de, D, T>(de: D) -> Result<Option<T>, D::Error>
where
    D: serde::Deserializer<'de>,
    T: serde::Deserialize<'de>,
{
    let opt = Option::<String>::deserialize(de)?;
    let opt = opt.as_deref();
    match opt {
        None | Some("") => Ok(None),
        Some(s) => T::deserialize(s.into_deserializer()).map(Some),
    }
}

fn bool_from_string<'de, D>(deserializer: D) -> Result<bool, D::Error>
where
    D: serde::Deserializer<'de>,
{
    match String::deserialize(deserializer)?.as_ref() {
        "0" => Ok(false),
        "1" => Ok(true),
        other => Err(serde::de::Error::invalid_value(
            Unexpected::Str(other),
            &"0 or 1",
        )),
    }
}

#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
#[serde(untagged)]
enum TagSer {
    List(Vec<()>),
    Tags { tag: Vec<String> },
}

fn deser_tags<'de, D>(de: D) -> Result<Vec<String>, D::Error>
where
    D: serde::Deserializer<'de>,
{
    let res = TagSer::deserialize(de);
    match res {
        Err(e) => Err(e),
        Ok(TagSer::List(_)) => Ok(vec![]),
        Ok(TagSer::Tags { tag }) => Ok(tag),
    }
}

#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
/// A recurrence rule for a repeating task.
pub struct RRule {
    /// If true, the recurrence rule is an "every" rule, which means it
    /// continues repeating even if the task isn't completed.  Otherwise,
    /// it is an "after" task.
    #[serde(deserialize_with = "bool_from_string")]
    pub every: bool,

    /// The recurrence rule; see RFC 2445 for the meaning.
    #[serde(rename = "$t")]
    pub rule: String,
}

#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
/// A rememberthemilk Task Series.  This corresponds to a single to-do item,
/// and has the fields such as name and tags.  It also may contain some
/// [Task]s, each of which is an instance of a possibly recurring or
/// repeating task.
pub struct TaskSeries {
    /// The task series' unique id within its list.
    pub id: String,
    /// The name of the task.
    pub name: String,
    /// The creation time.
    pub created: DateTime<Utc>,
    /// The last modification time.
    pub modified: DateTime<Utc>,
    /// The tasks within this series, if any.
    pub task: Vec<Task>,
    #[serde(deserialize_with = "deser_tags")]
    /// A list of the tags attached to this task series.
    pub tags: Vec<String>,
    /// Repetition information
    #[serde(rename = "rrule")]
    pub repeat: Option<RRule>,
}

#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
/// A rememberthemilk Task.  In rememberthemilk a task is
/// a specific instance of a possibly repeating item.  For
/// example, a weekly task to take out the bins is
/// represented as a single [TaskSeries] with a different
/// [Task] every week.  A Task's main characteristic is a
/// due date.
pub struct Task {
    /// The task's unique (within the list and task series) id.
    pub id: String,
    #[serde(deserialize_with = "empty_string_as_none")]
    /// The task's due date, if any.
    pub due: Option<DateTime<Utc>>,
    /// If true then there is a due date and time, not just date.
    #[serde(deserialize_with = "bool_from_string")]
    pub has_due_time: bool,
    #[serde(deserialize_with = "empty_string_as_none")]
    /// The task's deleted date, if any.
    pub deleted: Option<DateTime<Utc>>,
    #[serde(deserialize_with = "empty_string_as_none")]
    /// The date/time when this task was added
    pub added: Option<DateTime<Utc>>,
    #[serde(deserialize_with = "empty_string_as_none")]
    /// The date/time when this task was completed
    pub completed: Option<DateTime<Utc>>,
}

/// Describes how much time is left to complete this task, or perhaps
/// that it is overdue or has been deleted.
#[derive(Debug, Copy, Clone)]
pub enum TimeLeft {
    /// The length of time in seconds until this item is due (in the future)
    Remaining(u64),
    /// The task is overdue by this count of seconds
    Overdue(u64),
    /// Already completed
    Completed,
    /// No due date
    NoDue,
}

impl Task {
    /// Return the time left (or time since it was due) of a task.
    /// For tasks with no due date, or which are already completed,
    /// returns Completed.
    pub fn get_time_left(&self) -> TimeLeft {
        if self.completed.is_some() {
            return TimeLeft::Completed;
        }
        if self.deleted.is_some() {
            return TimeLeft::NoDue;
        }
        if self.due.is_none() || self.deleted.is_some() {
            return TimeLeft::NoDue;
        }
        if let Some(mut due) = self.due {
            if !self.has_due_time {
                // If no due time, assume it's due at the end of the day,
                // or the start of the next day.
                due = due + Duration::days(1);
            }
            let time_left = due.signed_duration_since(chrono::Utc::now());
            let seconds = time_left.num_seconds();
            if seconds < 0 {
                TimeLeft::Overdue((-seconds) as u64)
            } else {
                TimeLeft::Remaining(seconds as u64)
            }
        } else {
            // We would have found it in the previous test
            unreachable!()
        }
    }
}

#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
/// The response from fetching a list of tasks.
pub struct RTMTasks {
    rev: String,
    #[serde(default)]
    /// The list of tasks.
    pub list: Vec<RTMLists>,
}

#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
/// A container for a list of task series.
pub struct RTMLists {
    /// The unique id for this list of tasks series.
    pub id: String,
    /// The task series themselves.
    pub taskseries: Option<Vec<TaskSeries>>,
}

#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
struct TasksResponse {
    stat: Stat,
    tasks: RTMTasks,
}

#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
#[serde(rename = "list")]
/// The details of a list of to-do items.
pub struct RTMList {
    /// The list's unique ID.
    pub id: String,
    /// The name of this list.
    pub name: String,
}

#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
struct ListContainer {
    list: Vec<RTMList>,
}

#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
struct ListsResponse {
    stat: Stat,
    lists: ListContainer,
}

#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
struct Transaction {
    id: String,
    undoable: String,
}

#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
struct AddTagResponse {
    stat: Stat,
    list: RTMLists,
}

#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
struct AddTaskResponse {
    stat: Stat,
    transaction: Transaction,
    list: RTMLists,
}
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
struct RTMResponse<T> {
    rsp: T,
}

#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
struct TimelineResponse {
    stat: Stat,
    timeline: String,
}

/// Handle to a rememberthemilk timeline.
///
/// This is required for API calls which can modify state.  They can also
/// be used to undo (within a timeline) but this is not yet implemented.
pub struct RTMTimeline(String);

/// The state of an ongoing user authentication attempt.
pub struct AuthState {
    frob: String,
    /// The URL to which the user should be sent.  They will be asked
    /// to log in to rememberthemilk and allow the application access.
    pub url: String,
}

impl API {
    /// Create a new rememberthemilk API instance, with no user associated.
    ///
    /// A user will need to authenticate; see [API::start_auth].
    ///
    /// The `api_key` and `api_secret` are for authenticating the application.
    /// They can be [requested from rememberthemilk](https://www.rememberthemilk.com/services/api/).
    pub fn new(api_key: String, api_secret: String) -> API {
        API {
            api_key,
            api_secret,
            token: None,
            user: None,
        }
    }

    /// Create a new rememberthemilk API instance from saved configuration.
    ///
    /// The configuration may or may not include a valid user authentication
    /// token.  If not, then the next step is callnig [API::start_auth].
    ///
    /// The `config` will usually be generated from a previous session, where
    /// [API::to_config] was used to save the session state.
    pub fn from_config(config: RTMConfig) -> API {
        API {
            api_key: config.api_key.unwrap(),
            api_secret: config.api_secret.unwrap(),
            token: config.token,
            user: config.user,
        }
    }

    /// Extract a copy of the rememberthemilk API state.
    ///
    /// If a user has been authenticated in this session (or a previous one
    /// one and restored) then this will include a user authentication token
    /// as well as the API key and secret.  This can be serialised and used
    /// next time avoiding having to go through the authentication procedure
    /// every time.
    ///
    /// Note that this contains app and user secrets, so should not be stored
    /// anywhere where other users may be able to access.
    pub fn to_config(&self) -> RTMConfig {
        RTMConfig {
            api_key: Some(self.api_key.clone()),
            api_secret: Some(self.api_secret.clone()),
            token: self.token.clone(),
            user: self.user.clone(),
        }
    }

    fn sign_keys(&self, keys: &[(&str, &str)]) -> String {
        let mut my_keys = keys.iter().collect::<Vec<&(&str, &str)>>();
        my_keys.sort();
        let mut to_sign = self.api_secret.clone();
        for &(ref k, ref v) in my_keys {
            to_sign += k;
            to_sign += v;
        }
        let digest = md5::compute(to_sign.as_bytes());
        format!("{:x}", digest)
    }

    fn make_authenticated_url(&self, url: &str, keys: &[(&str, &str)]) -> String {
        let mut url = url.to_string();
        let auth_string = self.sign_keys(&keys);
        url.push('?');
        for (k, v) in keys {
            // Todo: URL: encode - maybe reqwest can help?
            url += &k;
            url.push('=');
            url += &v;
            url.push('&');
        }
        url += "api_sig=";
        url += &auth_string;
        url
    }

    fn make_authenticated_request<'a>(
        &'a self,
        url: &'a str,
        keys: &'a [(&'a str, &'a str)],
    ) -> impl std::future::Future<Output = Result<String, failure::Error>> + 'a {
        // As an async fn, this doesn't compile due to (I think):
        // https://github.com/rust-lang/rust/issues/63033
        // One of the comments points to an explicit async block instead of using
        // an async function as a workaround.
        let url = self.make_authenticated_url(url, keys);
        async move {
            let body = reqwest::get(&url).await?.text().await?;
            //println!("Body={}", body);
            Ok(body)
        }
    }

    async fn get_frob(&self) -> Result<String, Error> {
        let response = self
            .make_authenticated_request(
                &get_rest_url(),
                &[
                    ("method", "rtm.auth.getFrob"),
                    ("format", "json"),
                    ("api_key", &self.api_key),
                ],
            )
            .await?;
        let frob_resp = from_str::<RTMResponse<FrobResponse>>(&response)
            .unwrap()
            .rsp;
        Ok(frob_resp.frob)
    }

    /// Begin user authentication.
    ///
    /// If this call is successful (which requires a valid API key and secret,
    /// and a successful interaction with the rememberthemilk API) then the
    /// returned [AuthState] contains a URL which a user should open (e.g. by
    /// a web view or separate web browser instance, redirect, etc.  depending
    /// on the type of application).
    ///
    /// After the user has logged in and authorised the application, you can
    /// use [API::check_auth] to verify that this was successful.
    pub async fn start_auth(&self, perm: Perms) -> Result<AuthState, Error> {
        let frob = self.get_frob().await?;
        let url = self.make_authenticated_url(
            &get_auth_url(),
            &[
                ("api_key", &self.api_key),
                ("format", "json"),
                ("perms", perm.as_str()),
                ("frob", &frob),
            ],
        );
        Ok(AuthState { frob, url })
    }

    /// Check whether a user authentication attempt was successful.
    ///
    /// This should be called after the user has had a chance to visit the URL
    /// returned by [API::start_auth].  It can be called multiple times to poll.
    ///
    /// If authentication has been successful then a user auth token will be
    /// available (and retrievable using [API::to_config]) and true will be
    /// returned.  Other API calls can be made.
    pub async fn check_auth(&mut self, auth: &AuthState) -> Result<bool, Error> {
        let response = self
            .make_authenticated_request(
                &get_rest_url(),
                &[
                    ("method", "rtm.auth.getToken"),
                    ("format", "json"),
                    ("api_key", &self.api_key),
                    ("frob", &auth.frob),
                ],
            )
            .await?;

        //println!("{:?}", response);
        let auth_rep = from_str::<RTMResponse<AuthResponse>>(&response)
            .unwrap()
            .rsp;
        self.token = Some(auth_rep.auth.token);
        self.user = Some(auth_rep.auth.user);
        Ok(true)
    }

    /// Check whether we have a valid user token with the provided permission
    /// level.
    ///
    /// Returns true if so, false if none, and an error if the token
    /// is not valid (e.g.  expired).  [API::start_auth] will be needed if
    /// not successful to re-authenticate the user.
    pub async fn has_token(&self, perm: Perms) -> Result<bool, Error> {
        if let Some(ref tok) = self.token {
            let response = self
                .make_authenticated_request(
                    &get_rest_url(),
                    &[
                        ("method", "rtm.auth.checkToken"),
                        ("format", "json"),
                        ("api_key", &self.api_key),
                        ("auth_token", &tok),
                    ],
                )
                .await?;
            let ar = from_str::<RTMResponse<AuthResponse>>(&response)?.rsp;
            Ok(ar.auth.perms.includes(perm))
        } else {
            Ok(false)
        }
    }

    /// Retrieve a list of all tasks.
    ///
    /// This may be a lot of tasks if the user has been using rememberthemilk
    /// for some time, and is usually not needed unless exporting or backing
    /// up the whole thing.
    ///
    /// Requires a valid user authentication token.
    pub async fn get_all_tasks(&self) -> Result<RTMTasks, Error> {
        self.get_tasks_filtered("").await
    }

    /// Retrieve a filtered list of tasks.
    ///
    /// The `filter` is a string in the [format used by
    /// rememberthemilk](https://www.rememberthemilk.com/help/?ctx=basics.search.advanced),
    /// for example to retrieve tasks which have not yet been completed and
    /// are due today or in the past, you could use:
    ///
    /// `"status:incomplete AND (dueBefore:today OR due:today)"`
    ///
    /// Requires a valid user authentication token.
    pub async fn get_tasks_filtered(&self, filter: &str) -> Result<RTMTasks, Error> {
        if let Some(ref tok) = self.token {
            let mut params = vec![
                ("method", "rtm.tasks.getList"),
                ("format", "json"),
                ("api_key", &self.api_key),
                ("auth_token", &tok),
                ("v", "2"),
            ];
            if filter != "" {
                params.push(("filter", filter));
            }
            let response = self
                .make_authenticated_request(&get_rest_url(), &params)
                .await?;
            eprintln!("Got response:\n{}", response);
            // TODO: handle failure
            let tasklist = from_str::<RTMResponse<TasksResponse>>(&response)
                .unwrap()
                .rsp
                .tasks;
            Ok(tasklist)
        } else {
            bail!("Unable to fetch tasks")
        }
    }
    /// Request a list of rememberthemilk lists.
    ///
    /// Requires a valid user authentication token.
    pub async fn get_lists(&self) -> Result<Vec<RTMList>, Error> {
        if let Some(ref tok) = self.token {
            let params = &[
                ("method", "rtm.lists.getList"),
                ("format", "json"),
                ("api_key", &self.api_key),
                ("auth_token", &tok),
            ];
            let response = self
                .make_authenticated_request(&get_rest_url(), params)
                .await?;
            //println!("Got response:\n{}", response);
            // TODO: handle failure
            let lists = from_str::<RTMResponse<ListsResponse>>(&response)
                .unwrap()
                .rsp
                .lists;
            Ok(lists.list)
        } else {
            bail!("Unable to fetch tasks")
        }
    }
    /// Request a fresh remember timeline.
    ///
    /// A timeline is required for any request which modifies data on the
    /// server.
    ///
    /// Requires a valid user authentication token.
    pub async fn get_timeline(&self) -> Result<RTMTimeline, Error> {
        if let Some(ref tok) = self.token {
            let params = &[
                ("method", "rtm.timelines.create"),
                ("format", "json"),
                ("api_key", &self.api_key),
                ("auth_token", &tok),
            ];
            let response = self
                .make_authenticated_request(&get_rest_url(), params)
                .await?;
            //println!("Got response:\n{}", response);
            // TODO: handle failure
            let tl = from_str::<RTMResponse<TimelineResponse>>(&response)
                .unwrap()
                .rsp
                .timeline;
            Ok(RTMTimeline(tl))
        } else {
            bail!("Unable to fetch tasks")
        }
    }

    /// Add one or more tags to a task.
    ///
    /// * `timeline`: a timeline as retrieved using [API::get_timeline]
    /// * `list`, `taskseries` and `task` identify the task to tag.
    /// * `tags` is a slice of tags to add to this task.
    ///
    /// Requires a valid user authentication token.
    pub async fn add_tag(
        &self,
        timeline: &RTMTimeline,
        list: &RTMLists,
        taskseries: &TaskSeries,
        task: &Task,
        tags: &[&str],
    ) -> Result<(), Error> {
        if let Some(ref tok) = self.token {
            let tags = tags.join(",");
            let params = &[
                ("method", "rtm.tasks.addTags"),
                ("format", "json"),
                ("api_key", &self.api_key),
                ("auth_token", &tok),
                ("timeline", &timeline.0),
                ("list_id", &list.id),
                ("taskseries_id", &taskseries.id),
                ("task_id", &task.id),
                ("tags", &tags),
            ];
            let response = self
                .make_authenticated_request(&get_rest_url(), params)
                .await?;
            let rsp = from_str::<RTMResponse<AddTagResponse>>(&response)?.rsp;
            if let Stat::Ok = rsp.stat {
                Ok(())
            } else {
                bail!("Error adding task")
            }
        } else {
            bail!("Unable to fetch tasks")
        }
    }

    /// Add a new task
    ///
    /// * `timeline`: a timeline as retrieved using [API::get_timeline]
    /// * `name`: the new task's name
    /// * `list`: the optional list into which the task should go
    /// * `parent`: If specified, the parent task for the new task (pro accounts only)
    /// * `external_id`: An id which can be attached to this task.
    ///
    /// Requires a valid user authentication token.
    pub async fn add_task(
        &self,
        timeline: &RTMTimeline,
        name: &str,
        list: Option<&RTMLists>,
        parent: Option<&Task>,
        external_id: Option<&str>,
    ) -> Result<(), Error> {
        if let Some(ref tok) = self.token {
            let mut params = vec![
                ("method", "rtm.tasks.add"),
                ("format", "json"),
                ("api_key", &self.api_key),
                ("auth_token", &tok),
                ("timeline", &timeline.0),
                ("name", name),
            ];
            if let Some(list) = list {
                params.push(("list_id", &list.id));
            }
            if let Some(parent) = parent {
                params.push(("task_id", &parent.id));
            }
            if let Some(external_id) = external_id {
                params.push(("external_id", &external_id));
            }
            let response = self
                .make_authenticated_request(&get_rest_url(), &params)
                .await?;
            eprintln!("Add task response: {}", response);
            let rsp = from_str::<RTMResponse<AddTaskResponse>>(&response)?.rsp;
            if let Stat::Ok = rsp.stat {
                Ok(())
            } else {
                bail!("Error adding task")
            }
        } else {
            bail!("Unable to fetch tasks")
        }
    }
}

#[cfg(test)]
mod tests;