square_ox/api/
mod.rs

1/*!
2The endpoints of the [Square API](https://developer.squareup.com).
3
4To ensure the crate remains as extensible as possible, we are using
5the Display trait for the URL of all of the endpoints
6 */
7
8pub mod payment;
9pub mod bookings;
10pub mod locations;
11pub mod catalog;
12pub mod customers;
13pub mod cards;
14pub mod checkout;
15pub mod inventory;
16pub mod sites;
17pub mod terminal;
18pub mod orders;
19
20use crate::client::ClientMode;
21use crate::client::SquareClient;
22use std::fmt;
23
24/// All of the endpoints of the [Square API](https://developer.squareup.com)
25/// for which we have implemented some of the functionality.
26#[non_exhaustive]
27pub enum SquareAPI {
28    Payments(String),
29    Bookings(String),
30    Locations(String),
31    Catalog(String),
32    Customers(String),
33    Cards(String),
34    Checkout(String),
35    Inventory(String),
36    Sites(String),
37    Terminals(String),
38    Orders(String),
39}
40
41/// All of the HTTP verbs that have been implemented and are accepted by the different
42/// [Square API](https://developer.squareup.com) endpoints.
43pub enum Verb {
44    GET,
45    POST,
46    PUT,
47    PATCH,
48    DELETE,
49}
50
51/// Implement the Display trait for all of the endpoints we need this allows
52/// for them to be changed in the future without effecting the existing code
53/// base.
54impl fmt::Display for SquareAPI {
55    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56        match self {
57            SquareAPI::Payments(path) => write!(f, "payments{}", path),
58            SquareAPI::Bookings(path) => write!(f, "bookings{}", path),
59            SquareAPI::Locations(path) => write!(f, "locations{}", path),
60            SquareAPI::Catalog(path) => write!(f, "catalog{}", path),
61            SquareAPI::Customers(path) => write!(f, "customers{}", path),
62            SquareAPI::Cards(path) => write!(f, "cards{}", path),
63            SquareAPI::Checkout(path) => write!(f, "online-checkout{}", path),
64            SquareAPI::Inventory(path) => write!(f, "inventory{}", path),
65            SquareAPI::Sites(path) => write!(f, "sites{}", path),
66            SquareAPI::Terminals(path) => write!(f, "terminals{}", path),
67            SquareAPI::Orders(path) => write!(f, "orders{}", path),
68        }
69    }
70}
71
72impl SquareClient {
73    pub fn endpoint(&self, end_point: SquareAPI) -> String {
74        /// The main base URL for the Square API
75        const SQUARE_PRODUCTION_BASE: &str = "https://connect.squareup.com/v2/";
76        const SQUARE_SANDBOX_BASE: &str = "https://connect.squareupsandbox.com/v2/";
77
78        match self.client_mode {
79            ClientMode::Production => format!("{}{}", SQUARE_PRODUCTION_BASE, end_point),
80            ClientMode::Sandboxed => format!("{}{}", SQUARE_SANDBOX_BASE, end_point),
81        }
82    }
83}