riot_api/models/client/
client_builder.rs

1use reqwest::Client;
2use crate::models::{
3    routing::*,
4    client::{
5        ConversionError,
6        configuration::{AuthConfiguration, ApiConfiguration},
7    },
8};
9use crate::models::RiotApiClient;
10
11#[derive(Default)]
12pub struct RiotApiClientBuilder {
13    client: Option<Client>,
14    api_key: Option<String>,
15    riot_token: Option<String>,
16    retry_count: Option<u32>,
17    default_region: Option<RegionRouting>,
18    default_platform: Option<PlatformRouting>,
19}
20impl RiotApiClientBuilder {
21    pub fn new() -> Self {
22        Self::default()
23    }
24
25    pub fn api_key(mut self, key: String) -> Self {
26        if self.riot_token.is_some() { log::warn!("API key being set but Riot token is already set. Token will be preferred over key") }
27        self.api_key = Some(key);
28        self
29    }
30    pub fn riot_token(mut self, token: String) -> Self {
31        if self.api_key.is_some() { log::warn!("Riot token is being set but API key is already set. Token will be preferred over key") }
32        self.riot_token = Some(token);
33        self
34    }
35
36    pub fn with_client(mut self, client: Client) -> Self {
37        self.client = Some(client);
38        self
39    }
40
41    pub fn default_retry_count(mut self, default: u32) -> Self {
42        self.retry_count = Some(default);
43        self
44    }
45
46    pub fn default_region(mut self, default: RegionRouting) -> Self {
47        self.default_region = Some(default);
48        self
49    }
50    pub fn default_platform(mut self, default: PlatformRouting) -> Self {
51        self.default_platform = Some(default);
52        self
53    }
54
55    pub fn build(self) -> Result<RiotApiClient, ConversionError> {
56        let auth_config = match (self.riot_token, self.api_key) {
57            (Some(token), _) => AuthConfiguration::BearerToken(token),
58            (_, Some(key)) => AuthConfiguration::ApiKey(key),
59            _ => return Err(ConversionError::MissingDataError)
60        };
61        let config = ApiConfiguration::new(
62            auth_config, self.retry_count, self.default_region, self.default_platform
63        );
64
65        let client = self.client.ok_or(ConversionError::MissingDataError)?;
66
67        Ok(RiotApiClient::new(config, client))
68    }
69}