Skip to main content

autogen_squareup/apis/
mod.rs

1use std::error;
2use std::fmt;
3
4#[derive(Debug, Clone)]
5pub struct ResponseContent<T> {
6    pub status: reqwest::StatusCode,
7    pub content: String,
8    pub entity: Option<T>,
9}
10
11#[derive(Debug)]
12pub enum Error<T> {
13    Reqwest(reqwest::Error),
14    Serde(serde_json::Error),
15    Io(std::io::Error),
16    ResponseError(ResponseContent<T>),
17}
18
19impl <T> fmt::Display for Error<T> {
20    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21        let (module, e) = match self {
22            Error::Reqwest(e) => ("reqwest", e.to_string()),
23            Error::Serde(e) => ("serde", e.to_string()),
24            Error::Io(e) => ("IO", e.to_string()),
25            Error::ResponseError(e) => ("response", format!("status code {}", e.status)),
26        };
27        write!(f, "error in {}: {}", module, e)
28    }
29}
30
31impl <T: fmt::Debug> error::Error for Error<T> {
32    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
33        Some(match self {
34            Error::Reqwest(e) => e,
35            Error::Serde(e) => e,
36            Error::Io(e) => e,
37            Error::ResponseError(_) => return None,
38        })
39    }
40}
41
42impl <T> From<reqwest::Error> for Error<T> {
43    fn from(e: reqwest::Error) -> Self {
44        Error::Reqwest(e)
45    }
46}
47
48impl <T> From<serde_json::Error> for Error<T> {
49    fn from(e: serde_json::Error) -> Self {
50        Error::Serde(e)
51    }
52}
53
54impl <T> From<std::io::Error> for Error<T> {
55    fn from(e: std::io::Error) -> Self {
56        Error::Io(e)
57    }
58}
59
60pub fn urlencode<T: AsRef<str>>(s: T) -> String {
61    ::url::form_urlencoded::byte_serialize(s.as_ref().as_bytes()).collect()
62}
63
64pub fn parse_deep_object(prefix: &str, value: &serde_json::Value) -> Vec<(String, String)> {
65    if let serde_json::Value::Object(object) = value {
66        let mut params = vec![];
67
68        for (key, value) in object {
69            match value {
70                serde_json::Value::Object(_) => params.append(&mut parse_deep_object(
71                    &format!("{}[{}]", prefix, key),
72                    value,
73                )),
74                serde_json::Value::Array(array) => {
75                    for (i, value) in array.iter().enumerate() {
76                        params.append(&mut parse_deep_object(
77                            &format!("{}[{}][{}]", prefix, key, i),
78                            value,
79                        ));
80                    }
81                },
82                serde_json::Value::String(s) => params.push((format!("{}[{}]", prefix, key), s.clone())),
83                _ => params.push((format!("{}[{}]", prefix, key), value.to_string())),
84            }
85        }
86
87        return params;
88    }
89
90    unimplemented!("Only objects are supported with style=deepObject")
91}
92
93/// Internal use only
94/// A content type supported by this client.
95#[allow(dead_code)]
96enum ContentType {
97    Json,
98    Text,
99    Unsupported(String)
100}
101
102impl From<&str> for ContentType {
103    fn from(content_type: &str) -> Self {
104        if content_type.starts_with("application") && content_type.contains("json") {
105            return Self::Json;
106        } else if content_type.starts_with("text/plain") {
107            return Self::Text;
108        } else {
109            return Self::Unsupported(content_type.to_string());
110        }
111    }
112}
113
114#[cfg(feature = "apple-pay")]
115pub mod apple_pay_api;
116#[cfg(feature = "bank-accounts")]
117pub mod bank_accounts_api;
118#[cfg(feature = "booking-custom-attributes")]
119pub mod booking_custom_attributes_api;
120#[cfg(feature = "bookings")]
121pub mod bookings_api;
122#[cfg(feature = "cards")]
123pub mod cards_api;
124#[cfg(feature = "cash-drawers")]
125pub mod cash_drawers_api;
126#[cfg(feature = "catalog")]
127pub mod catalog_api;
128#[cfg(feature = "channels")]
129pub mod channels_api;
130#[cfg(feature = "checkout")]
131pub mod checkout_api;
132#[cfg(feature = "customer-custom-attributes")]
133pub mod customer_custom_attributes_api;
134#[cfg(feature = "customer-groups")]
135pub mod customer_groups_api;
136#[cfg(feature = "customer-segments")]
137pub mod customer_segments_api;
138#[cfg(feature = "customers")]
139pub mod customers_api;
140#[cfg(feature = "devices")]
141pub mod devices_api;
142#[cfg(feature = "disputes")]
143pub mod disputes_api;
144#[cfg(feature = "employees")]
145pub mod employees_api;
146#[cfg(feature = "events")]
147pub mod events_api;
148#[cfg(feature = "gift-card-activities")]
149pub mod gift_card_activities_api;
150#[cfg(feature = "gift-cards")]
151pub mod gift_cards_api;
152#[cfg(feature = "inventory")]
153pub mod inventory_api;
154#[cfg(feature = "invoices")]
155pub mod invoices_api;
156#[cfg(feature = "labor")]
157pub mod labor_api;
158#[cfg(feature = "location-custom-attributes")]
159pub mod location_custom_attributes_api;
160#[cfg(feature = "locations")]
161pub mod locations_api;
162#[cfg(feature = "loyalty")]
163pub mod loyalty_api;
164#[cfg(feature = "merchant-custom-attributes")]
165pub mod merchant_custom_attributes_api;
166#[cfg(feature = "merchants")]
167pub mod merchants_api;
168#[cfg(feature = "oauth")]
169pub mod o_auth_api;
170#[cfg(feature = "order-custom-attributes")]
171pub mod order_custom_attributes_api;
172#[cfg(feature = "orders")]
173pub mod orders_api;
174#[cfg(feature = "payments")]
175pub mod payments_api;
176#[cfg(feature = "payouts")]
177pub mod payouts_api;
178#[cfg(feature = "refunds")]
179pub mod refunds_api;
180#[cfg(feature = "sites")]
181pub mod sites_api;
182#[cfg(feature = "snippets")]
183pub mod snippets_api;
184#[cfg(feature = "subscriptions")]
185pub mod subscriptions_api;
186#[cfg(feature = "team")]
187pub mod team_api;
188#[cfg(feature = "terminal")]
189pub mod terminal_api;
190#[cfg(feature = "transactions")]
191pub mod transactions_api;
192#[cfg(feature = "transfer-order")]
193pub mod transfer_order_api;
194#[cfg(feature = "v1-transactions")]
195pub mod v1_transactions_api;
196#[cfg(feature = "vendors")]
197pub mod vendors_api;
198#[cfg(feature = "webhook-subscriptions")]
199pub mod webhook_subscriptions_api;
200
201pub mod configuration;