summaryrefslogtreecommitdiff
path: root/src/client.rs
blob: 5c6858b66702622709d60d0bcffa047ddb5ec8dc (plain)
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
//! Deezer client.
use std::error::Error;

use serde::Deserialize;

/// A user.
#[derive(Debug, Deserialize)]
pub struct User
{
    /// The user ID.
    pub id: u32,

    /// The user name.
    pub name: String,
}

/// Deezer client.
#[derive(Default)]
pub struct DeezerClient
{
    client: reqwest::Client,
    api_url: &'static str,
}

impl DeezerClient
{
    /// Creates a new Deezer client.
    #[must_use]
    pub fn new() -> Self
    {
        Self {
            client: reqwest::Client::new(),
            api_url: "https://api.deezer.com",
        }
    }

    /// Returns the authenticated user.
    pub async fn get_me(&self) -> Result<User, Box<dyn Error + Send + Sync>>
    {
        let user = self
            .client
            .get(format!("{}/user/me", self.api_url))
            .send()
            .await?
            .json::<User>()
            .await?;

        Ok(user)
    }
}