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
51
52
53
54
55
56
57
58
59
60
61
use reqwest::Client;
use crate::models::{
    routing::*,
    client::{
        ConversionError,
        configuration::{AuthConfiguration, ApiConfiguration},
    },
};
use crate::models::RiotApiClient;

#[derive(Default)]
pub struct RiotApiClientBuilder {
    client: Option<Client>,
    api_key: Option<String>,
    riot_token: Option<String>,
    default_region: Option<RegionRouting>,
    default_platform: Option<PlatformRouting>,
}
impl RiotApiClientBuilder {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn api_key(mut self, key: String) -> Self {
        if self.riot_token.is_some() { log::warn!("API key being set but Riot token is already set. Token will be preferred over key") }
        self.api_key = Some(key);
        self
    }
    pub fn riot_token(mut self, token: String) -> Self {
        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") }
        self.riot_token = Some(token);
        self
    }

    pub fn with_client(mut self, client: Client) -> Self {
        self.client = Some(client);
        self
    }

    pub fn default_region(mut self, default: RegionRouting) -> Self {
        self.default_region = Some(default);
        self
    }
    pub fn default_platform(mut self, default: PlatformRouting) -> Self {
        self.default_platform = Some(default);
        self
    }

    pub fn build(self) -> Result<RiotApiClient, ConversionError> {
        let auth_config = match (self.riot_token, self.api_key) {
            (Some(token), _) => AuthConfiguration::BearerToken(token),
            (_, Some(key)) => AuthConfiguration::ApiKey(key),
            _ => return Err(ConversionError::MissingDataError)
        };
        let config = ApiConfiguration::new(auth_config, self.default_region, self.default_platform);

        let client = self.client.ok_or(ConversionError::MissingDataError)?;

        Ok(RiotApiClient::new(config, client))
    }
}