Skip to main content

autogen_squareup/apis/
devices_api.rs

1/*
2 * Square
3 *
4 * Use Square APIs to manage and run business including payment, customer, product, inventory, and employee management.
5 *
6 * The version of the OpenAPI document: 2.0
7 * Contact: developers@squareup.com
8 * Generated by: https://openapi-generator.tech
9 */
10
11
12use reqwest;
13use serde::{Deserialize, Serialize, de::Error as _};
14use crate::{apis::ResponseContent, models};
15use super::{Error, configuration, ContentType};
16
17
18/// struct for typed errors of method [`create_device_code`]
19#[derive(Debug, Clone, Serialize, Deserialize)]
20#[serde(untagged)]
21pub enum CreateDeviceCodeError {
22    UnknownValue(serde_json::Value),
23}
24
25/// struct for typed errors of method [`get_device`]
26#[derive(Debug, Clone, Serialize, Deserialize)]
27#[serde(untagged)]
28pub enum GetDeviceError {
29    UnknownValue(serde_json::Value),
30}
31
32/// struct for typed errors of method [`get_device_code`]
33#[derive(Debug, Clone, Serialize, Deserialize)]
34#[serde(untagged)]
35pub enum GetDeviceCodeError {
36    UnknownValue(serde_json::Value),
37}
38
39/// struct for typed errors of method [`list_device_codes`]
40#[derive(Debug, Clone, Serialize, Deserialize)]
41#[serde(untagged)]
42pub enum ListDeviceCodesError {
43    UnknownValue(serde_json::Value),
44}
45
46/// struct for typed errors of method [`list_devices`]
47#[derive(Debug, Clone, Serialize, Deserialize)]
48#[serde(untagged)]
49pub enum ListDevicesError {
50    UnknownValue(serde_json::Value),
51}
52
53
54/// Creates a DeviceCode that can be used to login to a Square Terminal device to enter the connected terminal mode.
55pub async fn create_device_code(configuration: &configuration::Configuration, create_device_code_request: models::CreateDeviceCodeRequest) -> Result<models::CreateDeviceCodeResponse, Error<CreateDeviceCodeError>> {
56    // add a prefix to parameters to efficiently prevent name collisions
57    let p_create_device_code_request = create_device_code_request;
58
59    let uri_str = format!("{}/v2/devices/codes", configuration.base_path);
60    let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);
61
62    if let Some(ref user_agent) = configuration.user_agent {
63        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
64    }
65    if let Some(ref token) = configuration.oauth_access_token {
66        req_builder = req_builder.bearer_auth(token.to_owned());
67    };
68    req_builder = req_builder.json(&p_create_device_code_request);
69
70    let req = req_builder.build()?;
71    let resp = configuration.client.execute(req).await?;
72
73    let status = resp.status();
74    let content_type = resp
75        .headers()
76        .get("content-type")
77        .and_then(|v| v.to_str().ok())
78        .unwrap_or("application/octet-stream");
79    let content_type = super::ContentType::from(content_type);
80
81    if !status.is_client_error() && !status.is_server_error() {
82        let content = resp.text().await?;
83        match content_type {
84            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
85            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::CreateDeviceCodeResponse`"))),
86            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::CreateDeviceCodeResponse`")))),
87        }
88    } else {
89        let content = resp.text().await?;
90        let entity: Option<CreateDeviceCodeError> = serde_json::from_str(&content).ok();
91        Err(Error::ResponseError(ResponseContent { status, content, entity }))
92    }
93}
94
95/// Retrieves Device with the associated `device_id`.
96pub async fn get_device(configuration: &configuration::Configuration, device_id: &str) -> Result<models::GetDeviceResponse, Error<GetDeviceError>> {
97    // add a prefix to parameters to efficiently prevent name collisions
98    let p_device_id = device_id;
99
100    let uri_str = format!("{}/v2/devices/{device_id}", configuration.base_path, device_id=crate::apis::urlencode(p_device_id));
101    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
102
103    if let Some(ref user_agent) = configuration.user_agent {
104        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
105    }
106    if let Some(ref token) = configuration.oauth_access_token {
107        req_builder = req_builder.bearer_auth(token.to_owned());
108    };
109
110    let req = req_builder.build()?;
111    let resp = configuration.client.execute(req).await?;
112
113    let status = resp.status();
114    let content_type = resp
115        .headers()
116        .get("content-type")
117        .and_then(|v| v.to_str().ok())
118        .unwrap_or("application/octet-stream");
119    let content_type = super::ContentType::from(content_type);
120
121    if !status.is_client_error() && !status.is_server_error() {
122        let content = resp.text().await?;
123        match content_type {
124            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
125            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::GetDeviceResponse`"))),
126            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::GetDeviceResponse`")))),
127        }
128    } else {
129        let content = resp.text().await?;
130        let entity: Option<GetDeviceError> = serde_json::from_str(&content).ok();
131        Err(Error::ResponseError(ResponseContent { status, content, entity }))
132    }
133}
134
135/// Retrieves DeviceCode with the associated ID.
136pub async fn get_device_code(configuration: &configuration::Configuration, id: &str) -> Result<models::GetDeviceCodeResponse, Error<GetDeviceCodeError>> {
137    // add a prefix to parameters to efficiently prevent name collisions
138    let p_id = id;
139
140    let uri_str = format!("{}/v2/devices/codes/{id}", configuration.base_path, id=crate::apis::urlencode(p_id));
141    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
142
143    if let Some(ref user_agent) = configuration.user_agent {
144        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
145    }
146    if let Some(ref token) = configuration.oauth_access_token {
147        req_builder = req_builder.bearer_auth(token.to_owned());
148    };
149
150    let req = req_builder.build()?;
151    let resp = configuration.client.execute(req).await?;
152
153    let status = resp.status();
154    let content_type = resp
155        .headers()
156        .get("content-type")
157        .and_then(|v| v.to_str().ok())
158        .unwrap_or("application/octet-stream");
159    let content_type = super::ContentType::from(content_type);
160
161    if !status.is_client_error() && !status.is_server_error() {
162        let content = resp.text().await?;
163        match content_type {
164            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
165            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::GetDeviceCodeResponse`"))),
166            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::GetDeviceCodeResponse`")))),
167        }
168    } else {
169        let content = resp.text().await?;
170        let entity: Option<GetDeviceCodeError> = serde_json::from_str(&content).ok();
171        Err(Error::ResponseError(ResponseContent { status, content, entity }))
172    }
173}
174
175/// Lists all DeviceCodes associated with the merchant.
176pub async fn list_device_codes(configuration: &configuration::Configuration, cursor: Option<&str>, location_id: Option<&str>, product_type: Option<models::ProductType>, status: Option<models::DeviceCodeStatus>) -> Result<models::ListDeviceCodesResponse, Error<ListDeviceCodesError>> {
177    // add a prefix to parameters to efficiently prevent name collisions
178    let p_cursor = cursor;
179    let p_location_id = location_id;
180    let p_product_type = product_type;
181    let p_status = status;
182
183    let uri_str = format!("{}/v2/devices/codes", configuration.base_path);
184    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
185
186    if let Some(ref param_value) = p_cursor {
187        req_builder = req_builder.query(&[("cursor", &param_value.to_string())]);
188    }
189    if let Some(ref param_value) = p_location_id {
190        req_builder = req_builder.query(&[("location_id", &param_value.to_string())]);
191    }
192    if let Some(ref param_value) = p_product_type {
193        req_builder = req_builder.query(&[("product_type", &param_value.to_string())]);
194    }
195    if let Some(ref param_value) = p_status {
196        req_builder = req_builder.query(&[("status", &param_value.to_string())]);
197    }
198    if let Some(ref user_agent) = configuration.user_agent {
199        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
200    }
201    if let Some(ref token) = configuration.oauth_access_token {
202        req_builder = req_builder.bearer_auth(token.to_owned());
203    };
204
205    let req = req_builder.build()?;
206    let resp = configuration.client.execute(req).await?;
207
208    let status = resp.status();
209    let content_type = resp
210        .headers()
211        .get("content-type")
212        .and_then(|v| v.to_str().ok())
213        .unwrap_or("application/octet-stream");
214    let content_type = super::ContentType::from(content_type);
215
216    if !status.is_client_error() && !status.is_server_error() {
217        let content = resp.text().await?;
218        match content_type {
219            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
220            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::ListDeviceCodesResponse`"))),
221            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::ListDeviceCodesResponse`")))),
222        }
223    } else {
224        let content = resp.text().await?;
225        let entity: Option<ListDeviceCodesError> = serde_json::from_str(&content).ok();
226        Err(Error::ResponseError(ResponseContent { status, content, entity }))
227    }
228}
229
230/// List devices associated with the merchant. Currently, only Terminal API devices are supported.
231pub async fn list_devices(configuration: &configuration::Configuration, cursor: Option<&str>, sort_order: Option<models::SortOrder>, limit: Option<i32>, location_id: Option<&str>) -> Result<models::ListDevicesResponse, Error<ListDevicesError>> {
232    // add a prefix to parameters to efficiently prevent name collisions
233    let p_cursor = cursor;
234    let p_sort_order = sort_order;
235    let p_limit = limit;
236    let p_location_id = location_id;
237
238    let uri_str = format!("{}/v2/devices", configuration.base_path);
239    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
240
241    if let Some(ref param_value) = p_cursor {
242        req_builder = req_builder.query(&[("cursor", &param_value.to_string())]);
243    }
244    if let Some(ref param_value) = p_sort_order {
245        req_builder = req_builder.query(&[("sort_order", &param_value.to_string())]);
246    }
247    if let Some(ref param_value) = p_limit {
248        req_builder = req_builder.query(&[("limit", &param_value.to_string())]);
249    }
250    if let Some(ref param_value) = p_location_id {
251        req_builder = req_builder.query(&[("location_id", &param_value.to_string())]);
252    }
253    if let Some(ref user_agent) = configuration.user_agent {
254        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
255    }
256    if let Some(ref token) = configuration.oauth_access_token {
257        req_builder = req_builder.bearer_auth(token.to_owned());
258    };
259
260    let req = req_builder.build()?;
261    let resp = configuration.client.execute(req).await?;
262
263    let status = resp.status();
264    let content_type = resp
265        .headers()
266        .get("content-type")
267        .and_then(|v| v.to_str().ok())
268        .unwrap_or("application/octet-stream");
269    let content_type = super::ContentType::from(content_type);
270
271    if !status.is_client_error() && !status.is_server_error() {
272        let content = resp.text().await?;
273        match content_type {
274            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
275            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::ListDevicesResponse`"))),
276            ContentType::Unsupported(unknown_type) => return Err(Error::from(serde_json::Error::custom(format!("Received `{unknown_type}` content type response that cannot be converted to `models::ListDevicesResponse`")))),
277        }
278    } else {
279        let content = resp.text().await?;
280        let entity: Option<ListDevicesError> = serde_json::from_str(&content).ok();
281        Err(Error::ResponseError(ResponseContent { status, content, entity }))
282    }
283}
284