diff options
author | HampusM <hampus@hampusmat.com> | 2022-09-05 17:57:28 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2022-09-05 17:57:28 +0200 |
commit | d7929e7e9fee879a28871c2195620869db291441 (patch) | |
tree | 4b2c9a6b3bf64144a994d36d0a522404548116f3 /src/client.rs | |
parent | afade48668042eeb0740c7c5a1cc3806baaedc94 (diff) |
feat: add authentication
Diffstat (limited to 'src/client.rs')
-rw-r--r-- | src/client.rs | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/client.rs b/src/client.rs new file mode 100644 index 0000000..5c6858b --- /dev/null +++ b/src/client.rs @@ -0,0 +1,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) + } +} |