square_api_client/api/
customer_segments_api.rs

1//! Retrieve customer segments (also called smart groups) in a business account.
2//!
3//! The Customer Segments API lets you retrieve information about the segments defined for a
4//! business. Square sellers can create customer segments in the Seller Dashboard or Point of Sale
5//! by defining filters for the segment. For example, a segment can include customers who have
6//! visited more than 10 times. Customers are automatically added to and removed from the segment
7//! over time based on this criterion.
8//!
9//! You can inspect the customer's `segment_ids` property to determine which segments a customer
10//! belongs to. Then, you can use the Customer Segments API to retrieve basic details about each
11//! segment, such as the segment name and the time when it was created.
12
13use crate::{
14    config::Configuration,
15    http::client::HttpClient,
16    models::{
17        errors::ApiError, ListCustomerSegmentsParameters, ListCustomerSegmentsResponse,
18        RetrieveCustomerSegmentResponse,
19    },
20};
21
22const DEFAULT_URI: &str = "/customers/segments";
23
24/// Retrieve customer segments (also called smart groups) in a business account.
25pub struct CustomerSegmentsApi {
26    /// App config information
27    config: Configuration,
28    /// HTTP Client for requests to the Customer Segments API endpoints
29    client: HttpClient,
30}
31
32impl CustomerSegmentsApi {
33    pub fn new(config: Configuration, client: HttpClient) -> Self {
34        Self { config, client }
35    }
36
37    /// Retrieves the list of customer segments of a business.
38    pub async fn list_customer_segments(
39        &self,
40        params: &ListCustomerSegmentsParameters,
41    ) -> Result<ListCustomerSegmentsResponse, ApiError> {
42        let url = format!("{}{}", &self.url(), params.to_query_string());
43        let response = self.client.get(&url).await?;
44
45        response.deserialize().await
46    }
47
48    /// Retrieves a specific customer segment as identified by the `segment_id` value.
49    pub async fn retrieve_customer_segment(
50        &self,
51        segment_id: &str,
52    ) -> Result<RetrieveCustomerSegmentResponse, ApiError> {
53        let url = format!("{}/{}", &self.url(), segment_id);
54        let response = self.client.get(&url).await?;
55
56        response.deserialize().await
57    }
58
59    fn url(&self) -> String {
60        format!("{}{}", &self.config.get_base_url(), DEFAULT_URI)
61    }
62}