square_api_client/config/
environment.rs

1//! Square environment in which to operate
2
3const PRODUCTION_URL: &str = "https://connect.squareup.com";
4const SANDBOX_URL: &str = "https://connect.squareupsandbox.com";
5
6/// Identifies the Square environment in which this app instance is operating
7#[derive(Clone, Debug, Eq, PartialEq)]
8pub enum Environment {
9    Production,
10    Sandbox,
11    Custom(String),
12}
13
14impl Environment {
15    /// Gets the base Square API URL for this environment
16    pub fn get_base_url(&self) -> String {
17        match self {
18            Environment::Production => String::from(PRODUCTION_URL),
19            Environment::Sandbox => String::from(SANDBOX_URL),
20            Environment::Custom(custom_url) => custom_url.clone(),
21        }
22    }
23}
24
25impl Default for Environment {
26    /// Sandbox is the default environment
27    fn default() -> Self {
28        Self::Sandbox
29    }
30}
31
32#[cfg(test)]
33mod tests {
34    use crate::config::Environment;
35
36    #[test]
37    fn get_base_url_default() {
38        let environment = Environment::default();
39        assert_eq!(Environment::Sandbox, environment);
40        assert_eq!(String::from("https://connect.squareupsandbox.com"), environment.get_base_url())
41    }
42
43    #[test]
44    fn get_base_url_custom() {
45        assert_eq!(
46            String::from("some_custom_url"),
47            Environment::Custom(String::from("some_custom_url")).get_base_url()
48        );
49    }
50
51    #[test]
52    fn get_base_url_production() {
53        assert_eq!(
54            String::from("https://connect.squareup.com"),
55            Environment::Production.get_base_url()
56        )
57    }
58
59    #[test]
60    fn get_base_url_sandbox() {
61        assert_eq!(
62            String::from("https://connect.squareupsandbox.com"),
63            Environment::Sandbox.get_base_url()
64        )
65    }
66}