square_api_client/api/
customer_groups_api.rs

1//! Create and manage customer groups to streamline and automate workflows and help personalize
2//! customer interactions.
3//!
4//! The Customer Groups API lets you create and manage customer groups to provide targeted
5//! promotions or take other customized actions based on group membership. For example, you can
6//! create Weekly, Monthly, and Quarterly customer groups and add customers to them based on their
7//! preferences to receive marketing promotions on a weekly, monthly, and quarterly basis. You can
8//! then use the information to manage your marketing email schedule.
9//!
10//! You can use the Customer Groups API to retrieve and manage customer groups. You can use the
11//! Customers API to add customers to and remove customers from groups and search for customers
12//! based on group membership.
13
14use crate::{
15    config::Configuration,
16    http::client::HttpClient,
17    models::{
18        errors::ApiError, CreateCustomerGroupRequest, CreateCustomerGroupResponse,
19        DeleteCustomerGroupResponse, ListCustomerGroupsParameters, ListCustomerGroupsResponse,
20        RetrieveCustomerGroupResponse, UpdateCustomerGroupRequest, UpdateCustomerGroupResponse,
21    },
22};
23
24const DEFAULT_URI: &str = "/customers/groups";
25
26/// Create and manage customer groups to streamline and automate workflows and help personalize
27/// customer interactions.
28pub struct CustomerGroupsApi {
29    /// App config information
30    config: Configuration,
31    /// HTTP Client for requests to the Customer Groups API endpoints
32    client: HttpClient,
33}
34
35impl CustomerGroupsApi {
36    pub fn new(config: Configuration, client: HttpClient) -> Self {
37        Self { config, client }
38    }
39
40    /// Retrieves the list of customer groups of a business.
41    pub async fn list_customer_groups(
42        &self,
43        params: &ListCustomerGroupsParameters,
44    ) -> Result<ListCustomerGroupsResponse, ApiError> {
45        let url = format!("{}{}", &self.url(), params.to_query_string());
46        let response = self.client.get(&url).await?;
47
48        response.deserialize().await
49    }
50
51    /// Creates a new customer group for a business.
52    ///
53    /// The request must include the `name` value of the group.
54    pub async fn create_customer_group(
55        &self,
56        body: &CreateCustomerGroupRequest,
57    ) -> Result<CreateCustomerGroupResponse, ApiError> {
58        let response = self.client.post(&self.url(), body).await?;
59
60        response.deserialize().await
61    }
62
63    /// Deletes a customer group as identified by the `group_id` value.
64    pub async fn delete_customer_group(
65        &self,
66        group_id: &str,
67    ) -> Result<DeleteCustomerGroupResponse, ApiError> {
68        let url = format!("{}/{}", &self.url(), group_id);
69        let response = self.client.delete(&url).await?;
70
71        response.deserialize().await
72    }
73
74    /// Retrieves a specific customer group as identified by the `group_id` value.
75    pub async fn retrieve_customer_group(
76        &self,
77        group_id: &str,
78    ) -> Result<RetrieveCustomerGroupResponse, ApiError> {
79        let url = format!("{}/{}", &self.url(), group_id);
80        let response = self.client.get(&url).await?;
81
82        response.deserialize().await
83    }
84
85    pub async fn update_customer_group(
86        &self,
87        group_id: &str,
88        body: &UpdateCustomerGroupRequest,
89    ) -> Result<UpdateCustomerGroupResponse, ApiError> {
90        let url = format!("{}/{}", &self.url(), group_id);
91        let response = self.client.put(&url, body).await?;
92
93        response.deserialize().await
94    }
95
96    fn url(&self) -> String {
97        format!("{}{}", &self.config.get_base_url(), DEFAULT_URI)
98    }
99}