Skip to main content

lastfm_client/api/user/
info.rs

1use std::sync::Arc;
2
3use crate::api::constants::{BASE_URL, METHOD_USER_INFO};
4use crate::api::user_params;
5use crate::client::HttpClient;
6use crate::config::Config;
7use crate::error::Result;
8use crate::types::{UserInfo, UserInfoResponse};
9use crate::url_builder::Url;
10
11/// Builder for `user.getInfo` requests
12#[derive(Debug)]
13pub struct UserInfoRequestBuilder {
14    http: Arc<dyn HttpClient>,
15    config: Arc<Config>,
16    username: String,
17}
18
19impl UserInfoRequestBuilder {
20    pub(crate) fn new(http: Arc<dyn HttpClient>, config: Arc<Config>, username: String) -> Self {
21        Self {
22            http,
23            config,
24            username,
25        }
26    }
27
28    /// Fetch the user profile.
29    ///
30    /// # Errors
31    /// Returns an error if the HTTP request fails, the user does not exist, or the
32    /// response cannot be parsed.
33    pub async fn fetch(self) -> Result<UserInfo> {
34        let params = user_params(METHOD_USER_INFO, &self.username, self.config.api_key());
35        let url = Url::new(BASE_URL).add_args(params).build();
36        let value = self.http.get(&url).await?;
37        let response: UserInfoResponse = serde_json::from_value(value)?;
38
39        Ok(UserInfo::from(response))
40    }
41}