1use std::env;
4
5use log::warn;
6
7use crate::api::models::enums::api_version::SquareApiVersion;
8use crate::environment::Environment;
9use crate::http::client::config::SquareHttpClientConfig;
10
11const DEFAULT_BASE_URL: &str = "/v2";
12pub(crate) const DEFAULT_API_VERSION: SquareApiVersion = SquareApiVersion::V20230925;
13
14#[derive(Default)]
16pub struct SquareApiConfigBuilder {
17 environment: Option<Environment>,
18 api_version: Option<SquareApiVersion>,
19 http_client_config: Option<SquareHttpClientConfig>,
20 access_token: Option<String>,
21 base_url: Option<String>,
22}
23
24impl SquareApiConfigBuilder {
26 pub fn environment(&mut self, environment: Environment) -> &mut Self {
28 self.environment = Some(environment);
29 self
30 }
31
32 pub fn api_version(&mut self, api_version: SquareApiVersion) -> &mut Self {
34 self.api_version = Some(api_version);
35 self
36 }
37
38 pub fn http_client_config(&mut self, http_client_config: SquareHttpClientConfig) -> &mut Self {
40 self.http_client_config = Some(http_client_config);
41 self
42 }
43
44 pub fn access_token(&mut self, access_token: String) -> &mut Self {
46 self.access_token = Some(access_token);
47 self
48 }
49
50 pub fn base_url(&mut self, base_url: String) -> &mut Self {
52 self.base_url = Some(base_url);
53 self
54 }
55
56 pub fn build(&self) -> SquareApiConfig {
58 SquareApiConfig {
59 environment: self.environment.clone().unwrap_or_default(),
60 api_version: self.api_version.clone().unwrap_or(DEFAULT_API_VERSION),
61 http_client_config: self.http_client_config.clone().unwrap_or_default(),
62 access_token: self
63 .access_token
64 .clone()
65 .unwrap_or_else(get_default_authorization_token),
66 base_url: self.base_url.clone().unwrap_or_else(|| DEFAULT_BASE_URL.to_owned()),
67 }
68 }
69}
70
71#[derive(Clone)]
73pub struct SquareApiConfig {
74 pub environment: Environment,
75 pub api_version: SquareApiVersion,
76 pub http_client_config: SquareHttpClientConfig,
77 pub access_token: String,
78 pub base_url: String,
79}
80
81impl SquareApiConfig {
82 pub fn builder() -> SquareApiConfigBuilder {
84 SquareApiConfigBuilder::default()
85 }
86
87 pub fn get_dns(&self) -> String {
89 match self.environment {
90 Environment::Mock => "mock".to_string(),
91 Environment::Sandbox => "https://connect.squareupsandbox.com".to_string(),
92 Environment::Production => "https://connect.squareup.com".to_string(),
93 }
94 }
95}
96
97pub(crate) fn get_default_authorization_token() -> String {
99 format!(
100 "Bearer {}",
101 env::var("SQUARE_API_ACCESS_TOKEN").unwrap_or_else(|_| {
102 warn!("Environment variable SQUARE_API_ACCESS_TOKEN not found");
103 String::new()
104 })
105 )
106}
107
108#[cfg(test)]
109mod tests {
110 use super::*;
111
112 #[test]
113 fn test_default_config() {
114 let config = SquareApiConfig::builder().build();
115 assert_eq!(config.environment, Environment::Sandbox);
116 assert_eq!(config.base_url, DEFAULT_BASE_URL);
117 }
118
119 #[test]
120 fn test_get_dns() {
121 let config = SquareApiConfig::builder().build();
122 let dns = config.get_dns();
123 assert_eq!(dns, "https://connect.squareupsandbox.com");
124 }
125}