use crate::{
config::Configuration,
http::client::HttpClient,
models::{
errors::SquareApiError, CreateCustomerGroupRequest, CreateCustomerGroupResponse,
DeleteCustomerGroupResponse, ListCustomerGroupsParameters, ListCustomerGroupsResponse,
RetrieveCustomerGroupResponse, UpdateCustomerGroupRequest, UpdateCustomerGroupResponse,
},
SquareClient,
};
const DEFAULT_URI: &str = "/customers/groups";
pub struct CustomerGroupsApi {
config: Configuration,
http_client: HttpClient,
}
impl CustomerGroupsApi {
pub fn new(square_client: SquareClient) -> CustomerGroupsApi {
CustomerGroupsApi {
config: square_client.config,
http_client: square_client.http_client,
}
}
pub async fn list_customer_groups(
&self,
params: &ListCustomerGroupsParameters,
) -> Result<ListCustomerGroupsResponse, SquareApiError> {
let url = format!("{}{}", &self.url(), params.to_query_string());
let response = self.http_client.get(&url).await?;
response.deserialize().await
}
pub async fn create_customer_group(
&self,
body: &CreateCustomerGroupRequest,
) -> Result<CreateCustomerGroupResponse, SquareApiError> {
let response = self.http_client.post(&self.url(), body).await?;
response.deserialize().await
}
pub async fn delete_customer_group(
&self,
group_id: &str,
) -> Result<DeleteCustomerGroupResponse, SquareApiError> {
let url = format!("{}/{}", &self.url(), group_id);
let response = self.http_client.delete(&url).await?;
response.deserialize().await
}
pub async fn retrieve_customer_group(
&self,
group_id: &str,
) -> Result<RetrieveCustomerGroupResponse, SquareApiError> {
let url = format!("{}/{}", &self.url(), group_id);
let response = self.http_client.get(&url).await?;
response.deserialize().await
}
pub async fn update_customer_group(
&self,
group_id: &str,
body: &UpdateCustomerGroupRequest,
) -> Result<UpdateCustomerGroupResponse, SquareApiError> {
let url = format!("{}/{}", &self.url(), group_id);
let response = self.http_client.put(&url, body).await?;
response.deserialize().await
}
fn url(&self) -> String {
format!("{}{}", &self.config.get_base_url(), DEFAULT_URI)
}
}