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) => {
83 params.push((format!("{}[{}]", prefix, key), s.clone()))
84 }
85 _ => params.push((format!("{}[{}]", prefix, key), value.to_string())),
86 }
87 }
88
89 return params;
90 }
91
92 unimplemented!("Only objects are supported with style=deepObject")
93}
94
95#[allow(dead_code)]
98enum ContentType {
99 Json,
100 Text,
101 Unsupported(String),
102}
103
104impl From<&str> for ContentType {
105 fn from(content_type: &str) -> Self {
106 if content_type.starts_with("application") && content_type.contains("json") {
107 return Self::Json;
108 } else if content_type.starts_with("text/plain") {
109 return Self::Text;
110 } else {
111 return Self::Unsupported(content_type.to_string());
112 }
113 }
114}
115
116pub mod configuration;
117
118pub mod aplus_content_2020_11_01;
119pub mod app_integrations_2024_04_01;
120pub mod application_2023_11_30;
121pub mod awd_2024_05_09;
122pub mod catalog_items_2020_12_01;
123pub mod catalog_items_2022_04_01;
124pub mod catalog_items_v0;
125pub mod customer_feedback_2024_06_01;
126pub mod data_kiosk_2023_11_15;
127pub mod product_type_definitions_2020_09_01;
128pub mod easy_ship_2022_03_23;
129pub mod fba_inbound_eligibility_v1;
130pub mod fba_inventory_v1;
131pub mod feeds_2021_06_30;
132pub mod finances_2024_06_19;
133pub mod finances_v0;
134pub mod fulfillment_inbound_2024_03_20;
135pub mod fulfillment_inbound_v0;
136pub mod fulfillment_outbound_2020_07_01;
137pub mod invoices_2024_06_19;
138pub mod listings_items_2020_09_01;
139pub mod listings_items_2021_08_01;
140pub mod listings_restrictions_2021_08_01;
141pub mod merchant_fulfillment_v0;
142pub mod messaging_v1;
143pub mod notifications_v1;
144pub mod orders_v0;
145pub mod product_fees_v0;
146pub mod product_pricing_2022_05_01;
147pub mod product_pricing_v0;
148pub mod replenishment_2022_11_07;
149pub mod reports_2021_06_30;
150pub mod sales_v1;
151pub mod seller_wallet_2024_03_01;
152pub mod sellers_v1;
153pub mod service_v1;
154pub mod shipment_invoicing_v0;
155pub mod shipping_v1;
156pub mod shipping_v2;
157pub mod solicitations_v1;
158pub mod supply_sources_2020_07_01;
159pub mod tokens_2021_03_01;
160pub mod transfers_2024_06_01;
161pub mod uploads_2020_11_01;
162pub mod vehicles_2024_11_01;
163pub mod vendor_direct_fulfillment_inventory_v1;
164pub mod vendor_direct_fulfillment_orders_2021_12_28;
165pub mod vendor_direct_fulfillment_orders_v1;
166pub mod vendor_direct_fulfillment_payments_v1;
167pub mod vendor_direct_fulfillment_sandbox_data_2021_10_28;
168pub mod vendor_direct_fulfillment_shipping_2021_12_28;
169pub mod vendor_direct_fulfillment_shipping_v1;
170pub mod vendor_direct_fulfillment_transactions_2021_12_28;
171pub mod vendor_direct_fulfillment_transactions_v1;
172pub mod vendor_invoices_v1;
173pub mod vendor_orders_v1;
174pub mod vendor_shipments_v1;
175pub mod vendor_transaction_status_v1;