1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
//! Use the Customer Custom Attributes API to create and manage custom attributes for customer profiles. Custom attributes can be used to store properties or metadata that simplify integration, synchronization, and personalization workflows. After a custom attribute definition is created in a Square seller account, the custom attribute value can be set for customer profiles in the seller's Customer Directory.
//!
//! For more information, see the following guides:
//! - Customer Custom Attributes
//! - Square Webhooks Overview
use crate::models::errors::SquareApiError;
use crate::models::{
BulkUpsertCustomerCustomAttributesRequest, BulkUpsertCustomerCustomAttributesResponse,
CreateCustomerCustomAttributeDefinitionRequest,
CreateCustomerCustomAttributeDefinitionResponse,
DeleteCustomerCustomAttributeDefinitionResponse, DeleteCustomerCustomAttributeResponse,
ListCustomerCustomAttributeDefinitionsParameters,
ListCustomerCustomAttributeDefinitionsResponse, ListCustomerCustomAttributesParameters,
ListCustomerCustomAttributesResponse, RetrieveCustomerCustomAttributeDefinitionParameters,
RetrieveCustomerCustomAttributeDefinitionResponse, RetrieveCustomerCustomAttributeParameters,
RetrieveCustomerCustomAttributeResponse, UpdateCustomerCustomAttributeDefinitionRequest,
UpdateCustomerCustomAttributeDefinitionResponse, UpsertCustomerCustomAttributeRequest,
UpsertCustomerCustomAttributeResponse,
};
use crate::{SquareClient, config::Configuration, http::client::HttpClient};
const DEFAULT_URI: &str = "/customers";
/// Create and manage customer-related custom attribute definitions and custom attributes.
pub struct CustomerCustomAttributesApi {
/// App config information
config: Configuration,
/// HTTP Client for requests to the Customers Custom Attributes API endpoints
http_client: HttpClient,
}
impl CustomerCustomAttributesApi {
/// Instantiates a new `CustomerCustomAttributesApi`
pub fn new(square_client: SquareClient) -> CustomerCustomAttributesApi {
CustomerCustomAttributesApi {
config: square_client.config,
http_client: square_client.http_client,
}
}
/// Lists the customer-related custom attribute definitions that belong to a Square seller account.
///
/// When all response pages are retrieved, the results include all custom attribute definitions
/// that are visible to the requesting application, including those created by other applications
/// and set to `VISIBILITY_READ_ONLY` or `VISIBILITY_READ_WRITE_VALUES`. Seller-defined custom
/// attributes (also known as custom fields) are always set to `VISIBILITY_READ_WRITE_VALUES`.
///
/// Permissions: CUSTOMERS_READ
pub async fn list_customer_custom_attribute_definitions(
&self,
params: &ListCustomerCustomAttributeDefinitionsParameters,
) -> Result<ListCustomerCustomAttributeDefinitionsResponse, SquareApiError> {
let url = format!(
"{}/custom-attribute-definitions{}",
self.url(),
params.to_query_string()
);
let response = self.http_client.get(&url).await?;
response.deserialize().await
}
/// Creates a customer-related custom attribute definition for a Square seller account.
///
/// Use this endpoint to define a custom attribute that can be associated with customer profiles.
/// A custom attribute definition specifies the key, visibility, schema, and other properties for a custom attribute.
/// After the definition is created, you can call `UpsertCustomerCustomAttribute` or `BulkUpsertCustomerCustomAttributes`
/// to set the custom attribute for customer profiles in the seller's Customer Directory.
///
/// Permissions: CUSTOMERS_WRITE
pub async fn create_customer_custom_attribute_definition(
&self,
request: &CreateCustomerCustomAttributeDefinitionRequest,
) -> Result<CreateCustomerCustomAttributeDefinitionResponse, SquareApiError> {
let url = format!("{}/custom-attribute-definitions", self.url());
let response = self.http_client.post(&url, request).await?;
response.deserialize().await
}
/// Deletes a customer-related custom attribute definition from a Square seller account.
///
/// Deleting a custom attribute definition also deletes the corresponding custom attribute from
/// all customer profiles in the seller's Customer Directory. Only the definition owner can delete
/// a custom attribute definition.
///
/// Permissions: CUSTOMERS_WRITE
pub async fn delete_customer_custom_attribute_definition(
&self,
key: impl AsRef<str>,
) -> Result<DeleteCustomerCustomAttributeDefinitionResponse, SquareApiError> {
let url = format!(
"{}/custom-attribute-definitions/{}",
self.url(),
key.as_ref()
);
let response = self.http_client.delete(&url).await?;
response.deserialize().await
}
/// Retrieves a customer-related custom attribute definition from a Square seller account.
///
/// To retrieve a custom attribute definition created by another application, the visibility setting
/// must be `VISIBILITY_READ_ONLY` or `VISIBILITY_READ_WRITE_VALUES`. Seller-defined custom attributes
/// are always set to `VISIBILITY_READ_WRITE_VALUES`.
///
/// Permissions: CUSTOMERS_READ
pub async fn retrieve_customer_custom_attribute_definition(
&self,
key: impl AsRef<str>,
params: &RetrieveCustomerCustomAttributeDefinitionParameters,
) -> Result<RetrieveCustomerCustomAttributeDefinitionResponse, SquareApiError> {
let url = format!(
"{}/custom-attribute-definitions/{}{}",
self.url(),
key.as_ref(),
params.to_query_string()
);
let response = self.http_client.get(&url).await?;
response.deserialize().await
}
/// Updates a customer-related custom attribute definition for a Square seller account.
///
/// Use this endpoint to update the following fields: `name`, `description`, `visibility`, or the `schema`
/// for a Selection data type. Only changes to the named options or the maximum number of allowed selections
/// are supported.
///
/// Only the definition owner can update a custom attribute definition. Sellers can view all custom attributes
/// in exported customer data, including those set to `VISIBILITY_HIDDEN`.
///
/// Permissions: CUSTOMERS_WRITE
pub async fn update_customer_custom_attribute_definition(
&self,
key: impl AsRef<str>,
request: &UpdateCustomerCustomAttributeDefinitionRequest,
) -> Result<UpdateCustomerCustomAttributeDefinitionResponse, SquareApiError> {
let url = format!(
"{}/custom-attribute-definitions/{}",
self.url(),
key.as_ref()
);
let response = self.http_client.put(&url, request).await?;
response.deserialize().await
}
/// Creates or updates custom attributes for customer profiles as a bulk operation.
///
/// Use this endpoint to set the value of one or more custom attributes for one or more customer profiles.
/// Permissions: CUSTOMERS_WRITE
pub async fn bulk_upsert_customer_custom_attributes(
&self,
request: &BulkUpsertCustomerCustomAttributesRequest,
) -> Result<BulkUpsertCustomerCustomAttributesResponse, SquareApiError> {
let url = format!("{}//custom-attributes/bulk-upsert", self.url());
let response = self.http_client.post(&url, request).await?;
response.deserialize().await
}
/// Lists the custom attributes associated with a customer profile.
///
/// Use this endpoint to retrieve all custom attributes visible to the requesting application,
/// including those owned by other applications and set to `VISIBILITY_READ_ONLY` or
/// `VISIBILITY_READ_WRITE_VALUES`.
///
/// Permissions: CUSTOMERS_READ
pub async fn list_customer_custom_attributes(
&self,
customer_id: impl AsRef<str>,
params: &ListCustomerCustomAttributesParameters,
) -> Result<ListCustomerCustomAttributesResponse, SquareApiError> {
let url = format!(
"{}/{}/custom-attributes{}",
self.url(),
customer_id.as_ref(),
params.to_query_string()
);
let response = self.http_client.get(&url).await?;
response.deserialize().await
}
/// Deletes a custom attribute associated with a customer profile.
///
/// To delete a custom attribute owned by another application, the visibility setting must be
/// `VISIBILITY_READ_WRITE_VALUES`. Note that seller-defined custom attributes are always set to
/// `VISIBILITY_READ_WRITE_VALUES`.
///
/// Permissions: CUSTOMERS_WRITE
pub async fn delete_customer_custom_attribute(
&self,
customer_id: impl AsRef<str>,
key: impl AsRef<str>,
) -> Result<DeleteCustomerCustomAttributeResponse, SquareApiError> {
let url = format!(
"{}/{}/custom-attributes/{}",
self.url(),
customer_id.as_ref(),
key.as_ref()
);
let response = self.http_client.delete(&url).await?;
response.deserialize().await
}
/// Retrieves a custom attribute associated with a customer profile.
///
/// Use the `with_definition` query parameter to also retrieve the custom attribute definition
/// in the same call.
///
/// To retrieve a custom attribute owned by another application, the visibility setting must be
/// `VISIBILITY_READ_ONLY` or `VISIBILITY_READ_WRITE_VALUES`.
///
/// Permissions: CUSTOMERS_READ
pub async fn retrieve_customer_custom_attribute(
&self,
customer_id: impl AsRef<str>,
key: impl AsRef<str>,
params: &RetrieveCustomerCustomAttributeParameters,
) -> Result<RetrieveCustomerCustomAttributeResponse, SquareApiError> {
let url = format!(
"{}/{}/custom-attributes/{}{}",
self.url(),
customer_id.as_ref(),
key.as_ref(),
params.to_query_string()
);
let response = self.http_client.get(&url).await?;
response.deserialize().await
}
/// Creates or updates a custom attribute for a customer profile.
///
/// Use this endpoint to set the value of a custom attribute for a specified customer profile.
/// A custom attribute is based on a custom attribute definition in a Square seller account.
///
/// Permissions: CUSTOMERS_WRITE
pub async fn upsert_customer_custom_attribute(
&self,
customer_id: impl AsRef<str>,
key: impl AsRef<str>,
request: &UpsertCustomerCustomAttributeRequest,
) -> Result<UpsertCustomerCustomAttributeResponse, SquareApiError> {
let url = format!(
"{}/{}/custom-attributes/{}",
self.url(),
customer_id.as_ref(),
key.as_ref()
);
let response = self.http_client.post(&url, request).await?;
response.deserialize().await
}
fn url(&self) -> String {
format!("{}{}", &self.config.get_base_url(), DEFAULT_URI)
}
}