nadeo_api/client/mod.rs
1use crate::auth::o_auth::OAuthInfo;
2
3use crate::auth::{AuthInfo, AuthType};
4use crate::request::NadeoRequest;
5use crate::{Error, Result};
6
7use reqwest::{Client, Response};
8
9use crate::client::client_builder::NadeoClientBuilder;
10use crate::request::metadata::MetaData;
11use thiserror::Error;
12
13pub mod client_builder;
14
15pub(crate) const NADEO_AUTH_URL: &str =
16 "https://prod.trackmania.core.nadeo.online/v2/authentication/token/ubiservices";
17pub(crate) const NADEO_SERVER_AUTH_URL: &str =
18 "https://prod.trackmania.core.nadeo.online/v2/authentication/token/basic";
19pub(crate) const NADEO_REFRESH_URL: &str =
20 "https://prod.trackmania.core.nadeo.online/v2/authentication/token/refresh";
21pub(crate) const UBISOFT_APP_ID: &str = "86263886-327a-4328-ac69-527f0d20a237";
22pub(crate) const EXPIRATION_TIME_BUFFER: i64 = 60;
23
24/// This client can execute [`NadeoRequest`]s and handles authentication.
25///
26/// # Examples
27///
28/// Creating a client.
29/// ```rust
30/// # use nadeo_api::NadeoClient;
31/// let mut client = NadeoClient::builder()
32/// .with_normal_auth("email", "password") // optional (but at least 1 of the 3 is required)
33/// .with_server_auth("username", "password") // optional
34/// .with_oauth("identifier", "secret") // optional
35/// .user_agent("Testing the API / mustermann.max@gmail.com") // required
36/// .build()
37/// .await?;
38/// ```
39///
40/// [`NadeoRequest`]: NadeoRequest
41#[derive(Debug, Clone)]
42pub struct NadeoClient {
43 pub(crate) client: Client,
44 pub(crate) normal_auth: Option<AuthInfo>,
45 pub(crate) live_auth: Option<AuthInfo>,
46 pub(crate) o_auth: Option<OAuthInfo>,
47 pub(crate) meta_data: MetaData,
48}
49
50impl NadeoClient {
51 pub fn builder() -> NadeoClientBuilder {
52 NadeoClientBuilder::default()
53 }
54
55 /// Executes a [`NadeoRequest`] on the given [`NadeoClient`]. For more information about the API endpoints look [here](https://webservices.openplanet.dev/).
56 ///
57 /// # Errors
58 ///
59 /// Returns an [`Error`] if the required credentials for authorizing the request were not supplied when building the client or when there is an Error while executing the request.
60 ///
61 /// # Examples
62 ///
63 /// Gets the clubtag of a player given the *accountID*.
64 /// ```rust
65 /// # use nadeo_api::auth::AuthType;
66 /// # use nadeo_api::NadeoClient;
67 /// # use nadeo_api::request::{Method, NadeoRequest};
68 ///
69 /// // create client
70 /// let mut client = NadeoClient::builder()
71 /// .with_normal_auth("email", "password")
72 /// .user_agent("Testing the API / mustermann.max@gmail.com")
73 /// .build()
74 /// .await?;
75 ///
76 /// // build request
77 /// let request = NadeoRequest::builder()
78 /// .url("https://prod.trackmania.core.nadeo.online/accounts/clubTags/?accountIdList=29e75531-1a9d-4880-98da-e2acfe17c578")
79 /// .auth_type(AuthType::NadeoServices)
80 /// .method(Method::GET)
81 /// .build()?;
82 ///
83 /// // execute request
84 /// let response = client.execute(request).await?;
85 /// ```
86 ///
87 /// [`Error`]: crate::Error
88 /// [`NadeoRequest`]: NadeoRequest
89 /// [`NadeoClient`]: NadeoClient
90 pub async fn execute(&mut self, request: NadeoRequest) -> Result<Response> {
91 match request.auth_type {
92 AuthType::NadeoServices => {
93 if let Some(auth) = &mut self.normal_auth {
94 auth.execute(request, &self.meta_data, &self.client).await
95 } else {
96 Err(Error::from(ClientError::MissingNadeoAuth))
97 }
98 }
99 AuthType::NadeoLiveServices => {
100 if let Some(auth) = &mut self.live_auth {
101 auth.execute(request, &self.meta_data, &self.client).await
102 } else {
103 Err(Error::from(ClientError::MissingNadeoAuth))
104 }
105 }
106 AuthType::OAuth => {
107 if let Some(auth) = &mut self.o_auth {
108 auth.execute(request, &self.meta_data, &self.client).await
109 } else {
110 Err(Error::from(ClientError::MissingOAuth))
111 }
112 }
113 }
114 }
115}
116
117#[derive(Error, Debug)]
118pub enum ClientError {
119 #[error("Client does not have credentials for NadeoServices or NadeoLiveServices")]
120 MissingNadeoAuth,
121 #[error("Client does not have OAuth credentials")]
122 MissingOAuth,
123}