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
// Use 3rd party
use log::{debug, warn};
use reqwest::header::HeaderMap;
use reqwest::{Client, Method, Response, StatusCode};
use serde::Deserialize;
use thiserror::Error;

#[cfg(test)]
use mockito;

// Use built-in library
use std::borrow::Cow;
use std::collections::HashMap;

// Use internal modules
use crate::auth::TidalCredentials;
use crate::model::album::Album;
use crate::model::artist::Artist;
use crate::model::playlist::Playlist;
use crate::model::track::Track;

// Possible errors returned from `rstidal` client.
#[derive(Debug, Error)]
pub enum ClientError {
    #[error("request unauthorized")]
    Unauthorized,
    #[error("tidal error: {0}")]
    Api(#[from] ApiError),
    #[error("etag heeader parse error")]
    ParseEtag,
    #[error("json parse error: {0}")]
    ParseJSON(#[from] serde_json::Error),
    #[error("request error: {0}")]
    Request(#[from] reqwest::Error),
    #[error("status code: {0}")]
    StatusCode(StatusCode),
}

impl ClientError {
    async fn from_response(response: Response) -> Self {
        match response.status() {
            StatusCode::UNAUTHORIZED => Self::Unauthorized,
            status @ StatusCode::FORBIDDEN | status @ StatusCode::NOT_FOUND => response
                .json::<ApiError>()
                .await
                .map_or_else(|_| status.into(), Into::into),
            status => status.into(),
        }
    }
}

impl From<StatusCode> for ClientError {
    fn from(code: StatusCode) -> Self {
        Self::StatusCode(code)
    }
}
#[derive(Debug, Error, Deserialize)]
pub enum ApiError {
    #[error("{status}: {message}")]
    Regular {
        status: u16,
        #[serde(alias = "userMessage")]
        message: String,
    },
}

pub type ClientResult<T> = Result<T, ClientError>;

#[derive(Default, Debug, Deserialize)]
pub struct TidalItems<T> {
    pub items: Vec<T>,
}

#[derive(Default, Debug, Deserialize)]
pub struct TidalSearch {
    pub artists: TidalItems<Artist>,
    pub albums: TidalItems<Album>,
    pub playlists: TidalItems<Playlist>,
    pub tracks: TidalItems<Track>,
}

// Tidal API

pub struct Tidal {
    client: Client,
    credentials: TidalCredentials,
}

impl Tidal {
    #[must_use]
    pub fn new(credentials: TidalCredentials) -> Self {
        let _sesion_id = match &credentials.session_info {
            None => panic!("A session needs to be obtatined before using Tidal"),
            Some(session_info) => session_info.clone().session_id.expect("You need an authenticated credential to use Tidal"),
        };

        Self {
            client: Client::new(),
            credentials,
        }
    }

    fn session_id(&self) -> String {
        // Here it's safe to use unwrap because in ::new() we already checked that there's a valid session_info
        self.credentials.session_info.as_ref().unwrap().session_id.as_ref().unwrap().to_owned()
    }

    fn country_code(&self) -> String {
        // Here it's safe to use unwrap because in ::new() we already checked that there's a valid session_info
        self.credentials.session_info.as_ref().unwrap().country_code.to_owned()
    }

    pub fn user_id(&self) -> u32 {
        // Here it's safe to use unwrap because in ::new() we already checked that there's a valid session_info
        self.credentials.session_info.as_ref().unwrap().user_id.unwrap()
    }

    async fn api_call(
        &self,
        method: Method,
        url: &str,
        query: Option<&HashMap<String, String>>,
        payload: Option<&HashMap<&str, &str>>,
        etag: Option<String>,
    ) -> ClientResult<Response> {
        #[cfg(not(test))]
        let base_url: &str = "https://api.tidalhifi.com/v1";
        #[cfg(test)]
        let base_url: &str = &mockito::server_url();

        let mut url: Cow<str> = url.into();
        if !url.starts_with("http") {
            url = [base_url, &url].concat().into();
        }

        let mut headers = HeaderMap::new();
        headers.insert("X-Tidal-SessionId", self.session_id().parse().unwrap());
        headers.insert("Origin", "http://listen.tidal.com".parse().unwrap());
        if let Some(etag) = etag {
            headers.insert("If-None-Match", etag.parse().unwrap());
        }

        // Tidal's API requires countryCode to always be passed
        let mut query_params: HashMap<String, String> = HashMap::new();
        query_params.insert("countryCode".to_owned(), self.country_code());

        if let Some(query) = query {
            for (key, value) in query.iter() {
                query_params.insert(key.clone(), value.clone());
            }
        }

        let response = {
            let builder = self
                .client
                .request(method, &url.into_owned())
                .headers(headers)
                .query(&query_params);

            // Only add payload when sent
            let builder = if let Some(form) = payload {
                builder.form(form)
            } else {
                builder
            };

            debug!("request builder: {:?}", builder);
            builder.send().await.map_err(ClientError::from)?
        };

        debug!("response content: {:?}", response);
        if response.status().is_success() {
            Ok(response)
        } else {
            Err(ClientError::from_response(response).await)
        }
    }

    pub async fn etag(&self, url: &str) -> ClientResult<String> {
        // Tidal's API requires countryCode to always be passed
        let headers = self
            .api_call(Method::GET, &url, None, None, None)
            .await?
            .headers()
            .clone();

        if let Ok(etag) = headers
            .get("etag")
            .expect("etag header to be present")
            .to_str()
        {
            Ok(etag.to_owned())
        } else {
            Err(ClientError::ParseEtag)
        }
    }

    pub async fn get(
        &self,
        url: &str,
        params: &mut HashMap<String, String>,
    ) -> ClientResult<String> {
        self.api_call(Method::GET, &url, Some(params), None, None)
            .await?
            .text()
            .await
            .map_err(Into::into)
    }

    pub async fn post(
        &self,
        url: &str,
        payload: &HashMap<&str, &str>,
        etag: Option<String>,
    ) -> ClientResult<String> {
        self.api_call(Method::POST, &url, None, Some(payload), etag)
            .await?
            .text()
            .await
            .map_err(Into::into)
    }

    pub async fn put(
        &self,
        url: &str,
        payload: &HashMap<&str, &str>,
        etag: String,
    ) -> ClientResult<String> {
        self.api_call(Method::PUT, url, None, Some(payload), Some(etag))
            .await?
            .text()
            .await
            .map_err(Into::into)
    }

    // The following functions are for backward compatibility only
    //
    pub async fn search(&self, term: &str, limit: Option<u16>) -> ClientResult<TidalSearch> {
        warn!("DEPRECATION WARNING!: This method will be deprecated in the next version. Please favor using .searches().find()");
        self.searches().find(term, limit).await
    }

    pub async fn artist(&self, id: &str) -> ClientResult<Artist> {
        warn!("DEPRECATION WARNING!: This method will be deprecated in the next version. Please favor using .artists().get()");
        self.artists().get(id).await
    }

    pub async fn search_artist(&self, term: &str, limit: Option<u16>) -> ClientResult<Vec<Artist>> {
        warn!("DEPRECATION WARNING!: This method will be deprecated in the next version. Please favor using .artists().search()");
        self.artists().search(term, limit).await
    }

    pub async fn album(&self, id: &str) -> ClientResult<Album> {
        warn!("DEPRECATION WARNING!: This method will be deprecated in the next version. Please favor using .albums().get()");
        self.albums().get(id).await
    }

    pub async fn artist_albums(&self, id: &str) -> ClientResult<Vec<Album>> {
        warn!("DEPRECATION WARNING!: This method will be deprecated in the next version. Please favor using .artists().albums()");
        self.artists().albums(id).await
    }

    pub async fn search_album(&self, term: &str, limit: Option<u16>) -> ClientResult<Vec<Album>> {
        warn!("DEPRECATION WARNING!: This method will be deprecated in the next version. Please favor using .albums().search()");
        self.albums().search(term, limit).await
    }

    pub async fn album_tracks(&self, id: &str) -> ClientResult<Vec<Track>> {
        warn!("DEPRECATION WARNING!: This method will be deprecated in the next version. Please favor using .albums().tracks()");
        self.albums().tracks(id).await
    }

    pub async fn search_track(&self, term: &str, limit: Option<u16>) -> ClientResult<Vec<Track>> {
        warn!("DEPRECATION WARNING!: This method will be deprecated in the next version. Please favor using .tracks().search()");
        self.tracks().search(term, limit).await
    }

    pub async fn playlist(&self, id: &str) -> ClientResult<Playlist> {
        warn!("DEPRECATION WARNING!: This method will be deprecated in the next version. Please favor using .playlists().get()");
        self.playlists().get(id).await
    }

    pub async fn search_playlist(&self, term: &str, limit: Option<u16>) -> ClientResult<Vec<Playlist>> {
        warn!("DEPRECATION WARNING!: This method will be deprecated in the next version. Please favor using .playlists().search()");
        self.playlists().search(term, limit).await
    }

    pub async fn user_playlists(&self) -> ClientResult<Vec<Playlist>> {
        warn!("DEPRECATION WARNING!: This method will be deprecated in the next version. Please favor using .playlists().user_playlists()");
        self.playlists().user_playlists().await
    }

    pub async fn playlist_tracks(&self, id: &str) -> ClientResult<Vec<Track>> {
        warn!("DEPRECATION WARNING!: This method will be deprecated in the next version. Please favor using .playlists().tracks()");
        self.playlists().tracks(id).await
    }

    pub async fn playlist_add_tracks(&self, id: &str, tracks: Vec<Track>, add_dupes: bool) -> ClientResult<Playlist> {
        warn!("DEPRECATION WARNING!: This method will be deprecated in the next version. Please favor using .playlists().add_tracks()");
        self.playlists().add_tracks(id, tracks, add_dupes).await
    }

    pub async fn create_playlist(&self, title: &str, description: &str) -> ClientResult<Playlist> {
        warn!("DEPRECATION WARNING!: This method will be deprecated in the next version. Please favor using .playlists().create()");
        self.playlists().create(title, description).await
    }

    pub fn convert_result<'a, T: Deserialize<'a>>(input: &'a str) -> ClientResult<T> {
        serde_json::from_str::<T>(input).map_err(Into::into)
    }
}

#[cfg(test)]
pub mod tests {
    use super::*;
    use crate::auth::SessionInfo;
    use mockito::{mock, Matcher};

    #[tokio::test]
    async fn client_get() {
        let mut params: HashMap<String, String> = HashMap::new();

        // All requesets going to Tidal ned to append ?countryCode=$USER_REGION
        let _mock = mock_request_success(
            "GET",
            "/",
            vec![Matcher::UrlEncoded("countryCode".into(), "US".into())],
            r#"{"result": "ok"}"#,
        );

        let client = client();
        let response = client.get("/", &mut params).await.unwrap();
        assert_eq!(response, r#"{"result": "ok"}"#)
    }

    #[tokio::test]
    async fn client_search() {
        let _mock = mock_request_success_from_file(
            "GET",
            "/search",
            vec![
                Matcher::UrlEncoded("countryCode".into(), "US".into()),
                Matcher::UrlEncoded("query".into(), "trivium".into()),
                Matcher::UrlEncoded("limit".into(), "10".into()),
            ],
            "tests/files/search.json",
        )
        .create();

        let result: TidalSearch = client().search("trivium", None).await.unwrap();

        assert_eq!(result.artists.items.len(), 10);
        assert_eq!(result.albums.items.len(), 10);
        assert_eq!(result.tracks.items.len(), 10);
        assert_eq!(result.playlists.items.len(), 10);
    }

    #[tokio::test]
    async fn client_artist() {
        let _mock = mock_request_success_from_file(
            "GET",
            "/artists/37312",
            vec![Matcher::UrlEncoded("countryCode".into(), "US".into())],
            "tests/files/artist.json",
        )
        .create();

        let result: Artist = client().artist("37312").await.unwrap();
        let expected_result = Artist {
            id: Some(37312),
            name: Some("myband".to_owned()),
            ..Default::default()
        };
        assert_eq!(result.id, expected_result.id);
        assert_eq!(result.name, expected_result.name);
    }

    #[tokio::test]
    async fn client_search_artist() {
        let _mock = mock_request_success_from_file(
            "GET",
            "/search",
            vec![
                Matcher::UrlEncoded("countryCode".into(), "US".into()),
                Matcher::UrlEncoded("query".into(), "trivium".into()),
            ],
            "tests/files/search.json",
        )
        .create();

        let result: Vec<Artist> = client().search_artist("trivium", None).await.unwrap();

        assert_eq!(result.len(), 10);
    }

    #[tokio::test]
    async fn client_artist_albums() {
        let _mock = mock_request_success_from_file(
            "GET",
            "/artists/37312/albums",
            vec![Matcher::UrlEncoded("countryCode".into(), "US".into())],
            "tests/files/artist_albums.json",
        );

        let result: Vec<Album> = client().artist_albums("37312").await.unwrap();
        let expected_first_result = Album {
            id: Some(138458220),
            title: Some("What The Dead Men Say".to_owned()),
            ..Default::default()
        };
        assert_eq!(result[0].id, expected_first_result.id);
        assert_eq!(result[0].title, expected_first_result.title);
    }

    #[tokio::test]
    async fn client_album() {
        let _mock = mock_request_success_from_file(
            "GET",
            "/albums/79914998",
            vec![Matcher::UrlEncoded("countryCode".into(), "US".into())],
            "tests/files/album.json",
        );

        let result: Album = client().album("79914998").await.unwrap();
        let expected_result = Album {
            id: Some(79914998),
            title: Some("My Album".to_owned()),
            ..Default::default()
        };
        assert_eq!(result.id, expected_result.id);
        assert_eq!(result.title, expected_result.title);
    }

    #[tokio::test]
    async fn client_search_album() {
        let _mock = mock_request_success_from_file(
            "GET",
            "/search",
            vec![
                Matcher::UrlEncoded("countryCode".into(), "US".into()),
                Matcher::UrlEncoded("query".into(), "trivium".into()),
            ],
            "tests/files/search.json",
        )
        .create();

        let result: Vec<Album> = client().search_album("trivium", None).await.unwrap();

        assert_eq!(result.len(), 10);
    }

    #[tokio::test]
    async fn client_album_tracks() {
        let _mock = mock_request_success_from_file(
            "GET",
            "/albums/79914998/tracks",
            vec![Matcher::UrlEncoded("countryCode".into(), "US".into())],
            "tests/files/album_tracks.json",
        );

        let result: Vec<Track> = client().album_tracks("79914998").await.unwrap();
        let expected_first_result = Track {
            title: Some("The Sin and the Sentence".to_owned()),
            ..Default::default()
        };
        assert_eq!(result[0].title, expected_first_result.title);
    }

    #[tokio::test]
    async fn client_search_tracks() {
        let _mock = mock_request_success_from_file(
            "GET",
            "/search",
            vec![
                Matcher::UrlEncoded("countryCode".into(), "US".into()),
                Matcher::UrlEncoded("query".into(), "trivium".into()),
            ],
            "tests/files/search.json",
        )
        .create();

        let result: Vec<Track> = client().search_track("trivium", None).await.unwrap();

        assert_eq!(result.len(), 10);
    }

    #[tokio::test]
    async fn client_playlist() {
        let _mock = mock_request_success_from_file(
            "GET",
            "/playlists/7ce7df87-6d37-4465-80db-84535a4e44a4",
            vec![Matcher::UrlEncoded("countryCode".into(), "US".into())],
            "tests/files/playlist.json",
        );

        let result: Playlist = client()
            .playlist("7ce7df87-6d37-4465-80db-84535a4e44a4")
            .await
            .unwrap();
        let expected_result = Playlist {
            uuid: Some("7ce7df87-6d37-4465-80db-84535a4e44a4".to_owned()),
            title: Some("Metal - TIDAL Masters".to_owned()),
            ..Default::default()
        };
        assert_eq!(result.uuid, expected_result.uuid);
        assert_eq!(result.title, expected_result.title);
    }

    #[tokio::test]
    async fn client_user_playlists() {
        let _mock = mock_request_success_from_file(
            "GET",
            "/users/1234/playlists",
            vec![Matcher::UrlEncoded("countryCode".into(), "US".into())],
            "tests/files/user_playlists.json",
        );

        let result: Vec<Playlist> = client().user_playlists().await.unwrap();
        let expected_result = Playlist {
            uuid: Some("8edf5a89-fec4-4aa3-80ab-9e00a83633a2".to_owned()),
            title: Some("roadtrip".to_owned()),
            ..Default::default()
        };
        assert_eq!(result[0].uuid, expected_result.uuid);
        assert_eq!(result[0].title, expected_result.title);
    }

    #[tokio::test]
    async fn client_playlist_tracks() {
        let _mock = mock_request_success_from_file(
            "GET",
            "/playlists/7ce7df87-6d37-4465-80db-84535a4e44a4/tracks",
            vec![Matcher::UrlEncoded("countryCode".into(), "US".into())],
            "tests/files/playlist_tracks.json",
        );

        let result: Vec<Track> = client()
            .playlist_tracks("7ce7df87-6d37-4465-80db-84535a4e44a4")
            .await
            .unwrap();
        let expected_first_result = Track {
            title: Some("FULL OF HEALTH".to_owned()),
            ..Default::default()
        };
        assert_eq!(result[0].title, expected_first_result.title);
    }

    #[tokio::test]
    async fn client_playlist_add_tracks() {
        let _mock_reload_playlist = mock_request_success_from_file(
            "GET",
            "/playlists/7ce7df87-6d37-4465-80db-84535a4e44a4",
            vec![Matcher::UrlEncoded("countryCode".into(), "US".into())],
            "tests/files/playlist.json",
        );

        let track_1 = Track {
            id: Some(79914998),
            ..Default::default()
        };
        let track_2 = Track {
            id: Some(7915000),
            ..Default::default()
        };
        let tracks = vec![track_1, track_2];

        let _mock_etag_req = mock(
            "GET",
            "/playlists/7ce7df87-6d37-4465-80db-84535a4e44a4/items",
        )
        .match_query(Matcher::UrlEncoded("countryCode".into(), "US".into()))
        .with_body("")
        .with_header("etag", "123457689")
        .create();

        let mock_update_playlist = mock(
            "POST",
            "/playlists/7ce7df87-6d37-4465-80db-84535a4e44a4/items",
        )
        .match_query(Matcher::UrlEncoded("countryCode".into(), "US".into()))
        .match_header("if-none-match", "123457689")
        .with_body(r#"{ "lastUpdated": 1600273268158, "addedItemIds": [ 79914999, 79915000 ] }"#)
        .create();

        let _result: Playlist = client()
            .playlist_add_tracks("7ce7df87-6d37-4465-80db-84535a4e44a4", tracks, false)
            .await
            .unwrap();
        mock_update_playlist.assert();
    }

    fn mock_request_success(
        method: &str,
        path: &str,
        query: Vec<Matcher>,
        body: &str,
    ) -> mockito::Mock {
        mock(method, path)
            .match_query(Matcher::AllOf(query))
            .with_status(200)
            .with_body(body)
            .create()
    }

    pub fn mock_request_success_from_file(
        method: &str,
        path: &str,
        query: Vec<Matcher>,
        file_path: &str,
    ) -> mockito::Mock {
        mock(method, path)
            .match_query(Matcher::AllOf(query))
            .with_status(200)
            .with_body_from_file(file_path)
            .create()
    }

    pub fn client() -> Tidal {
        Tidal::new(credential())
    }

    fn credential() -> TidalCredentials {
        let session: SessionInfo = SessionInfo {
            user_id: Some(1234),
            session_id: Some("session-id-1".to_owned()),
            country_code: "US".to_owned(),
        };
        TidalCredentials {
            token: "some_token".to_owned(),
            session_info: Some(session),
        }
    }
}