Skip to main content

autogen_squareup/apis/
webhook_subscriptions_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_webhook_subscription`]
19#[derive(Debug, Clone, Serialize, Deserialize)]
20#[serde(untagged)]
21pub enum CreateWebhookSubscriptionError {
22    UnknownValue(serde_json::Value),
23}
24
25/// struct for typed errors of method [`delete_webhook_subscription`]
26#[derive(Debug, Clone, Serialize, Deserialize)]
27#[serde(untagged)]
28pub enum DeleteWebhookSubscriptionError {
29    UnknownValue(serde_json::Value),
30}
31
32/// struct for typed errors of method [`list_webhook_event_types`]
33#[derive(Debug, Clone, Serialize, Deserialize)]
34#[serde(untagged)]
35pub enum ListWebhookEventTypesError {
36    UnknownValue(serde_json::Value),
37}
38
39/// struct for typed errors of method [`list_webhook_subscriptions`]
40#[derive(Debug, Clone, Serialize, Deserialize)]
41#[serde(untagged)]
42pub enum ListWebhookSubscriptionsError {
43    UnknownValue(serde_json::Value),
44}
45
46/// struct for typed errors of method [`retrieve_webhook_subscription`]
47#[derive(Debug, Clone, Serialize, Deserialize)]
48#[serde(untagged)]
49pub enum RetrieveWebhookSubscriptionError {
50    UnknownValue(serde_json::Value),
51}
52
53/// struct for typed errors of method [`test_webhook_subscription`]
54#[derive(Debug, Clone, Serialize, Deserialize)]
55#[serde(untagged)]
56pub enum TestWebhookSubscriptionError {
57    UnknownValue(serde_json::Value),
58}
59
60/// struct for typed errors of method [`update_webhook_subscription`]
61#[derive(Debug, Clone, Serialize, Deserialize)]
62#[serde(untagged)]
63pub enum UpdateWebhookSubscriptionError {
64    UnknownValue(serde_json::Value),
65}
66
67/// struct for typed errors of method [`update_webhook_subscription_signature_key`]
68#[derive(Debug, Clone, Serialize, Deserialize)]
69#[serde(untagged)]
70pub enum UpdateWebhookSubscriptionSignatureKeyError {
71    UnknownValue(serde_json::Value),
72}
73
74
75/// Creates a webhook subscription.
76pub async fn create_webhook_subscription(configuration: &configuration::Configuration, create_webhook_subscription_request: models::CreateWebhookSubscriptionRequest) -> Result<models::CreateWebhookSubscriptionResponse, Error<CreateWebhookSubscriptionError>> {
77    // add a prefix to parameters to efficiently prevent name collisions
78    let p_create_webhook_subscription_request = create_webhook_subscription_request;
79
80    let uri_str = format!("{}/v2/webhooks/subscriptions", configuration.base_path);
81    let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);
82
83    if let Some(ref user_agent) = configuration.user_agent {
84        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
85    }
86    if let Some(ref token) = configuration.oauth_access_token {
87        req_builder = req_builder.bearer_auth(token.to_owned());
88    };
89    req_builder = req_builder.json(&p_create_webhook_subscription_request);
90
91    let req = req_builder.build()?;
92    let resp = configuration.client.execute(req).await?;
93
94    let status = resp.status();
95    let content_type = resp
96        .headers()
97        .get("content-type")
98        .and_then(|v| v.to_str().ok())
99        .unwrap_or("application/octet-stream");
100    let content_type = super::ContentType::from(content_type);
101
102    if !status.is_client_error() && !status.is_server_error() {
103        let content = resp.text().await?;
104        match content_type {
105            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
106            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::CreateWebhookSubscriptionResponse`"))),
107            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::CreateWebhookSubscriptionResponse`")))),
108        }
109    } else {
110        let content = resp.text().await?;
111        let entity: Option<CreateWebhookSubscriptionError> = serde_json::from_str(&content).ok();
112        Err(Error::ResponseError(ResponseContent { status, content, entity }))
113    }
114}
115
116/// Deletes a webhook subscription.
117pub async fn delete_webhook_subscription(configuration: &configuration::Configuration, subscription_id: &str) -> Result<models::DeleteWebhookSubscriptionResponse, Error<DeleteWebhookSubscriptionError>> {
118    // add a prefix to parameters to efficiently prevent name collisions
119    let p_subscription_id = subscription_id;
120
121    let uri_str = format!("{}/v2/webhooks/subscriptions/{subscription_id}", configuration.base_path, subscription_id=crate::apis::urlencode(p_subscription_id));
122    let mut req_builder = configuration.client.request(reqwest::Method::DELETE, &uri_str);
123
124    if let Some(ref user_agent) = configuration.user_agent {
125        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
126    }
127    if let Some(ref token) = configuration.oauth_access_token {
128        req_builder = req_builder.bearer_auth(token.to_owned());
129    };
130
131    let req = req_builder.build()?;
132    let resp = configuration.client.execute(req).await?;
133
134    let status = resp.status();
135    let content_type = resp
136        .headers()
137        .get("content-type")
138        .and_then(|v| v.to_str().ok())
139        .unwrap_or("application/octet-stream");
140    let content_type = super::ContentType::from(content_type);
141
142    if !status.is_client_error() && !status.is_server_error() {
143        let content = resp.text().await?;
144        match content_type {
145            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
146            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::DeleteWebhookSubscriptionResponse`"))),
147            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::DeleteWebhookSubscriptionResponse`")))),
148        }
149    } else {
150        let content = resp.text().await?;
151        let entity: Option<DeleteWebhookSubscriptionError> = serde_json::from_str(&content).ok();
152        Err(Error::ResponseError(ResponseContent { status, content, entity }))
153    }
154}
155
156/// Lists all webhook event types that can be subscribed to.
157pub async fn list_webhook_event_types(configuration: &configuration::Configuration, api_version: Option<&str>) -> Result<models::ListWebhookEventTypesResponse, Error<ListWebhookEventTypesError>> {
158    // add a prefix to parameters to efficiently prevent name collisions
159    let p_api_version = api_version;
160
161    let uri_str = format!("{}/v2/webhooks/event-types", configuration.base_path);
162    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
163
164    if let Some(ref param_value) = p_api_version {
165        req_builder = req_builder.query(&[("api_version", &param_value.to_string())]);
166    }
167    if let Some(ref user_agent) = configuration.user_agent {
168        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
169    }
170    if let Some(ref token) = configuration.oauth_access_token {
171        req_builder = req_builder.bearer_auth(token.to_owned());
172    };
173
174    let req = req_builder.build()?;
175    let resp = configuration.client.execute(req).await?;
176
177    let status = resp.status();
178    let content_type = resp
179        .headers()
180        .get("content-type")
181        .and_then(|v| v.to_str().ok())
182        .unwrap_or("application/octet-stream");
183    let content_type = super::ContentType::from(content_type);
184
185    if !status.is_client_error() && !status.is_server_error() {
186        let content = resp.text().await?;
187        match content_type {
188            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
189            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::ListWebhookEventTypesResponse`"))),
190            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::ListWebhookEventTypesResponse`")))),
191        }
192    } else {
193        let content = resp.text().await?;
194        let entity: Option<ListWebhookEventTypesError> = serde_json::from_str(&content).ok();
195        Err(Error::ResponseError(ResponseContent { status, content, entity }))
196    }
197}
198
199/// Lists all webhook subscriptions owned by your application.
200pub async fn list_webhook_subscriptions(configuration: &configuration::Configuration, cursor: Option<&str>, include_disabled: Option<bool>, sort_order: Option<models::SortOrder>, limit: Option<i32>) -> Result<models::ListWebhookSubscriptionsResponse, Error<ListWebhookSubscriptionsError>> {
201    // add a prefix to parameters to efficiently prevent name collisions
202    let p_cursor = cursor;
203    let p_include_disabled = include_disabled;
204    let p_sort_order = sort_order;
205    let p_limit = limit;
206
207    let uri_str = format!("{}/v2/webhooks/subscriptions", configuration.base_path);
208    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
209
210    if let Some(ref param_value) = p_cursor {
211        req_builder = req_builder.query(&[("cursor", &param_value.to_string())]);
212    }
213    if let Some(ref param_value) = p_include_disabled {
214        req_builder = req_builder.query(&[("include_disabled", &param_value.to_string())]);
215    }
216    if let Some(ref param_value) = p_sort_order {
217        req_builder = req_builder.query(&[("sort_order", &param_value.to_string())]);
218    }
219    if let Some(ref param_value) = p_limit {
220        req_builder = req_builder.query(&[("limit", &param_value.to_string())]);
221    }
222    if let Some(ref user_agent) = configuration.user_agent {
223        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
224    }
225    if let Some(ref token) = configuration.oauth_access_token {
226        req_builder = req_builder.bearer_auth(token.to_owned());
227    };
228
229    let req = req_builder.build()?;
230    let resp = configuration.client.execute(req).await?;
231
232    let status = resp.status();
233    let content_type = resp
234        .headers()
235        .get("content-type")
236        .and_then(|v| v.to_str().ok())
237        .unwrap_or("application/octet-stream");
238    let content_type = super::ContentType::from(content_type);
239
240    if !status.is_client_error() && !status.is_server_error() {
241        let content = resp.text().await?;
242        match content_type {
243            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
244            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::ListWebhookSubscriptionsResponse`"))),
245            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::ListWebhookSubscriptionsResponse`")))),
246        }
247    } else {
248        let content = resp.text().await?;
249        let entity: Option<ListWebhookSubscriptionsError> = serde_json::from_str(&content).ok();
250        Err(Error::ResponseError(ResponseContent { status, content, entity }))
251    }
252}
253
254/// Retrieves a webhook subscription identified by its ID.
255pub async fn retrieve_webhook_subscription(configuration: &configuration::Configuration, subscription_id: &str) -> Result<models::RetrieveWebhookSubscriptionResponse, Error<RetrieveWebhookSubscriptionError>> {
256    // add a prefix to parameters to efficiently prevent name collisions
257    let p_subscription_id = subscription_id;
258
259    let uri_str = format!("{}/v2/webhooks/subscriptions/{subscription_id}", configuration.base_path, subscription_id=crate::apis::urlencode(p_subscription_id));
260    let mut req_builder = configuration.client.request(reqwest::Method::GET, &uri_str);
261
262    if let Some(ref user_agent) = configuration.user_agent {
263        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
264    }
265    if let Some(ref token) = configuration.oauth_access_token {
266        req_builder = req_builder.bearer_auth(token.to_owned());
267    };
268
269    let req = req_builder.build()?;
270    let resp = configuration.client.execute(req).await?;
271
272    let status = resp.status();
273    let content_type = resp
274        .headers()
275        .get("content-type")
276        .and_then(|v| v.to_str().ok())
277        .unwrap_or("application/octet-stream");
278    let content_type = super::ContentType::from(content_type);
279
280    if !status.is_client_error() && !status.is_server_error() {
281        let content = resp.text().await?;
282        match content_type {
283            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
284            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::RetrieveWebhookSubscriptionResponse`"))),
285            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::RetrieveWebhookSubscriptionResponse`")))),
286        }
287    } else {
288        let content = resp.text().await?;
289        let entity: Option<RetrieveWebhookSubscriptionError> = serde_json::from_str(&content).ok();
290        Err(Error::ResponseError(ResponseContent { status, content, entity }))
291    }
292}
293
294/// Tests a webhook subscription by sending a test event to the notification URL.
295pub async fn test_webhook_subscription(configuration: &configuration::Configuration, subscription_id: &str, test_webhook_subscription_request: models::TestWebhookSubscriptionRequest) -> Result<models::TestWebhookSubscriptionResponse, Error<TestWebhookSubscriptionError>> {
296    // add a prefix to parameters to efficiently prevent name collisions
297    let p_subscription_id = subscription_id;
298    let p_test_webhook_subscription_request = test_webhook_subscription_request;
299
300    let uri_str = format!("{}/v2/webhooks/subscriptions/{subscription_id}/test", configuration.base_path, subscription_id=crate::apis::urlencode(p_subscription_id));
301    let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);
302
303    if let Some(ref user_agent) = configuration.user_agent {
304        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
305    }
306    if let Some(ref token) = configuration.oauth_access_token {
307        req_builder = req_builder.bearer_auth(token.to_owned());
308    };
309    req_builder = req_builder.json(&p_test_webhook_subscription_request);
310
311    let req = req_builder.build()?;
312    let resp = configuration.client.execute(req).await?;
313
314    let status = resp.status();
315    let content_type = resp
316        .headers()
317        .get("content-type")
318        .and_then(|v| v.to_str().ok())
319        .unwrap_or("application/octet-stream");
320    let content_type = super::ContentType::from(content_type);
321
322    if !status.is_client_error() && !status.is_server_error() {
323        let content = resp.text().await?;
324        match content_type {
325            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
326            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::TestWebhookSubscriptionResponse`"))),
327            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::TestWebhookSubscriptionResponse`")))),
328        }
329    } else {
330        let content = resp.text().await?;
331        let entity: Option<TestWebhookSubscriptionError> = serde_json::from_str(&content).ok();
332        Err(Error::ResponseError(ResponseContent { status, content, entity }))
333    }
334}
335
336/// Updates a webhook subscription.
337pub async fn update_webhook_subscription(configuration: &configuration::Configuration, subscription_id: &str, update_webhook_subscription_request: models::UpdateWebhookSubscriptionRequest) -> Result<models::UpdateWebhookSubscriptionResponse, Error<UpdateWebhookSubscriptionError>> {
338    // add a prefix to parameters to efficiently prevent name collisions
339    let p_subscription_id = subscription_id;
340    let p_update_webhook_subscription_request = update_webhook_subscription_request;
341
342    let uri_str = format!("{}/v2/webhooks/subscriptions/{subscription_id}", configuration.base_path, subscription_id=crate::apis::urlencode(p_subscription_id));
343    let mut req_builder = configuration.client.request(reqwest::Method::PUT, &uri_str);
344
345    if let Some(ref user_agent) = configuration.user_agent {
346        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
347    }
348    if let Some(ref token) = configuration.oauth_access_token {
349        req_builder = req_builder.bearer_auth(token.to_owned());
350    };
351    req_builder = req_builder.json(&p_update_webhook_subscription_request);
352
353    let req = req_builder.build()?;
354    let resp = configuration.client.execute(req).await?;
355
356    let status = resp.status();
357    let content_type = resp
358        .headers()
359        .get("content-type")
360        .and_then(|v| v.to_str().ok())
361        .unwrap_or("application/octet-stream");
362    let content_type = super::ContentType::from(content_type);
363
364    if !status.is_client_error() && !status.is_server_error() {
365        let content = resp.text().await?;
366        match content_type {
367            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
368            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::UpdateWebhookSubscriptionResponse`"))),
369            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::UpdateWebhookSubscriptionResponse`")))),
370        }
371    } else {
372        let content = resp.text().await?;
373        let entity: Option<UpdateWebhookSubscriptionError> = serde_json::from_str(&content).ok();
374        Err(Error::ResponseError(ResponseContent { status, content, entity }))
375    }
376}
377
378/// Updates a webhook subscription by replacing the existing signature key with a new one.
379pub async fn update_webhook_subscription_signature_key(configuration: &configuration::Configuration, subscription_id: &str, update_webhook_subscription_signature_key_request: models::UpdateWebhookSubscriptionSignatureKeyRequest) -> Result<models::UpdateWebhookSubscriptionSignatureKeyResponse, Error<UpdateWebhookSubscriptionSignatureKeyError>> {
380    // add a prefix to parameters to efficiently prevent name collisions
381    let p_subscription_id = subscription_id;
382    let p_update_webhook_subscription_signature_key_request = update_webhook_subscription_signature_key_request;
383
384    let uri_str = format!("{}/v2/webhooks/subscriptions/{subscription_id}/signature-key", configuration.base_path, subscription_id=crate::apis::urlencode(p_subscription_id));
385    let mut req_builder = configuration.client.request(reqwest::Method::POST, &uri_str);
386
387    if let Some(ref user_agent) = configuration.user_agent {
388        req_builder = req_builder.header(reqwest::header::USER_AGENT, user_agent.clone());
389    }
390    if let Some(ref token) = configuration.oauth_access_token {
391        req_builder = req_builder.bearer_auth(token.to_owned());
392    };
393    req_builder = req_builder.json(&p_update_webhook_subscription_signature_key_request);
394
395    let req = req_builder.build()?;
396    let resp = configuration.client.execute(req).await?;
397
398    let status = resp.status();
399    let content_type = resp
400        .headers()
401        .get("content-type")
402        .and_then(|v| v.to_str().ok())
403        .unwrap_or("application/octet-stream");
404    let content_type = super::ContentType::from(content_type);
405
406    if !status.is_client_error() && !status.is_server_error() {
407        let content = resp.text().await?;
408        match content_type {
409            ContentType::Json => serde_json::from_str(&content).map_err(Error::from),
410            ContentType::Text => return Err(Error::from(serde_json::Error::custom("Received `text/plain` content type response that cannot be converted to `models::UpdateWebhookSubscriptionSignatureKeyResponse`"))),
411            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::UpdateWebhookSubscriptionSignatureKeyResponse`")))),
412        }
413    } else {
414        let content = resp.text().await?;
415        let entity: Option<UpdateWebhookSubscriptionSignatureKeyError> = serde_json::from_str(&content).ok();
416        Err(Error::ResponseError(ResponseContent { status, content, entity }))
417    }
418}
419