use crate::{
config::Configuration,
http::client::HttpClient,
models::{
errors::SquareApiError, CreateCardRequest, CreateCardResponse, DisableCardResponse,
ListCardsParameters, ListCardsResponse, RetrieveCardResponse,
},
SquareClient,
};
const DEFAULT_URI: &str = "/cards";
pub struct CardsApi {
config: Configuration,
http_client: HttpClient,
}
impl CardsApi {
pub fn new(square_client: SquareClient) -> CardsApi {
CardsApi {
config: square_client.config,
http_client: square_client.http_client,
}
}
pub async fn create_card(
&self,
body: &CreateCardRequest,
) -> Result<CreateCardResponse, SquareApiError> {
let response = self.http_client.post(&self.url(), body).await?;
response.deserialize().await
}
pub async fn disable_card(&self, card_id: &str) -> Result<DisableCardResponse, SquareApiError> {
let url = format!("{}/{}/disable", &self.url(), card_id);
let response = self.http_client.empty_post(&url).await?;
response.deserialize().await
}
pub async fn list_cards(
&self,
params: &ListCardsParameters,
) -> Result<ListCardsResponse, SquareApiError> {
let url = format!("{}{}", &self.url(), params.to_query_string());
let response = self.http_client.get(&url).await?;
response.deserialize().await
}
pub async fn retrieve_card(
&self,
card_id: &str,
) -> Result<RetrieveCardResponse, SquareApiError> {
let url = format!("{}/{}", &self.url(), card_id);
let response = self.http_client.get(&url).await?;
response.deserialize().await
}
fn url(&self) -> String {
format!("{}{}", &self.config.get_base_url(), DEFAULT_URI)
}
}