squareup/api/
webhook_subscriptions_api.rs

1//! Create and manage webhook subscriptions.
2//!
3//! The Webhook Subscriptions API allows you to create, retrieve, update, and delete webhook
4//! subscriptions. Because Webhook subscriptions are owned by the application and not any one
5//! seller, you cannot use OAuth Access Tokens with the Webhook Subscriptions API. You must use
6//! the application’s personal access token
7
8use crate::models::{
9    CreateWebhookSubscriptionRequest, CreateWebhookSubscriptionResponse,
10    DeleteWebhookSubscriptionResponse, ListWebhookEventTypesResponse,
11    ListWebhookSubscriptionsParams, ListWebhookSubscriptionsResponse,
12    RetrieveWebhookSubscriptionResponse, TestWebhookSubscriptionRequest,
13    TestWebhookSubscriptionResponse, UpdateWebhookSubscriptionRequest,
14    UpdateWebhookSubscriptionResponse, UpdateWebhookSubscriptionSignatureKeyRequest,
15    UpdateWebhookSubscriptionSignatureKeyResponse,
16};
17use crate::{
18    SquareClient, config::Configuration, http::client::HttpClient, models::errors::SquareApiError,
19};
20
21const DEFAULT_URI: &str = "/webhooks";
22
23pub struct WebhookSubscriptionsApi {
24    /// App config information
25    config: Configuration,
26    /// HTTP Client for requests to the Team API endpoints
27    http_client: HttpClient,
28}
29
30impl WebhookSubscriptionsApi {
31    /// Instantiates a new `WebhookSubscriptionsApi`
32    pub fn new(square_client: SquareClient) -> WebhookSubscriptionsApi {
33        WebhookSubscriptionsApi {
34            config: square_client.config,
35            http_client: square_client.http_client,
36        }
37    }
38
39    /// Lists all webhook event types that can be subscribed to.
40    pub async fn list_webhook_event_types(
41        &self,
42        api_version: &Option<String>,
43    ) -> Result<ListWebhookEventTypesResponse, SquareApiError> {
44        let url = match api_version {
45            None => {
46                format!("{}/event-types", self.url())
47            }
48            Some(version) => {
49                format!("{}/event-types?api_version={}", self.url(), version)
50            }
51        };
52        let response = self.http_client.get(&url).await?;
53
54        response.deserialize().await
55    }
56
57    /// Lists all webhook subscriptions owned by your application.
58    pub async fn list_webhook_subscriptions(
59        &self,
60        params: &ListWebhookSubscriptionsParams,
61    ) -> Result<ListWebhookSubscriptionsResponse, SquareApiError> {
62        let url = format!("{}/subscriptions{}", self.url(), params.to_query_string());
63        let response = self.http_client.get(&url).await?;
64
65        response.deserialize().await
66    }
67
68    /// Creates a webhook subscription.
69    pub async fn create_webhook_subscription(
70        &self,
71        body: &CreateWebhookSubscriptionRequest,
72    ) -> Result<CreateWebhookSubscriptionResponse, SquareApiError> {
73        let url = format!("{}/subscriptions", self.url());
74        let response = self.http_client.post(&url, body).await?;
75
76        response.deserialize().await
77    }
78
79    /// Deletes a webhook subscription identified by its ID.
80    pub async fn delete_webhook_subscription(
81        &self,
82        subscription_id: impl AsRef<str>,
83    ) -> Result<DeleteWebhookSubscriptionResponse, SquareApiError> {
84        let url = format!("{}/subscriptions/{}", self.url(), subscription_id.as_ref());
85        let response = self.http_client.delete(&url).await?;
86
87        response.deserialize().await
88    }
89
90    /// Retrieves a webhook subscription identified by its ID.
91    pub async fn retrieve_webhook_subscription(
92        &self,
93        subscription_id: impl AsRef<str>,
94    ) -> Result<RetrieveWebhookSubscriptionResponse, SquareApiError> {
95        let url = format!("{}/subscriptions/{}", self.url(), subscription_id.as_ref());
96        let response = self.http_client.get(&url).await?;
97
98        response.deserialize().await
99    }
100
101    /// Updates a webhook subscription.
102    pub async fn update_webhook_subscription(
103        &self,
104        subscription_id: impl AsRef<str>,
105        body: &UpdateWebhookSubscriptionRequest,
106    ) -> Result<UpdateWebhookSubscriptionResponse, SquareApiError> {
107        let url = format!("{}/subscriptions/{}", self.url(), subscription_id.as_ref());
108        let response = self.http_client.put(&url, body).await?;
109
110        response.deserialize().await
111    }
112
113    /// Updates a webhook subscription by replacing the existing signature key with a new one.
114    pub async fn update_webhook_subscription_signature_key(
115        &self,
116        subscription_id: impl AsRef<str>,
117        body: &UpdateWebhookSubscriptionSignatureKeyRequest,
118    ) -> Result<UpdateWebhookSubscriptionSignatureKeyResponse, SquareApiError> {
119        let url = format!("{}/subscriptions/{}", self.url(), subscription_id.as_ref());
120        let response = self.http_client.post(&url, body).await?;
121
122        response.deserialize().await
123    }
124
125    /// Tests a webhook subscription by sending a test event to the notification URL.
126    pub async fn test_webhook_subscription(
127        &self,
128        subscription_id: impl AsRef<str>,
129        body: &TestWebhookSubscriptionRequest,
130    ) -> Result<TestWebhookSubscriptionResponse, SquareApiError> {
131        let url = format!(
132            "{}/subscriptions/{}/test",
133            self.url(),
134            subscription_id.as_ref()
135        );
136        let response = self.http_client.post(&url, body).await?;
137
138        response.deserialize().await
139    }
140
141    /// Constructs the basic entity URL including domain and entity path. Any additional path
142    /// elements (e.g. path parameters) will need to be appended to this URL.
143    fn url(&self) -> String {
144        format!("{}{}", &self.config.get_base_url(), DEFAULT_URI)
145    }
146}