use crate::{
config::Configuration,
http::client::HttpClient,
models::{
errors::SquareApiError, BatchRetrieveOrdersRequest, BatchRetrieveOrdersResponse,
CalculateOrderRequest, CalculateOrderResponse, CloneOrderRequest, CloneOrderResponse,
CreateOrderRequest, CreateOrderResponse, PayOrderRequest, PayOrderResponse,
RetrieveOrderResponse, SearchOrdersRequest, SearchOrdersResponse, UpdateOrderRequest,
UpdateOrderResponse,
},
SquareClient,
};
const DEFAULT_URI: &str = "/orders";
pub struct OrdersApi {
config: Configuration,
http_client: HttpClient,
}
impl OrdersApi {
pub fn new(square_client: SquareClient) -> OrdersApi {
OrdersApi {
config: square_client.config,
http_client: square_client.http_client,
}
}
pub async fn create_order(
&self,
body: &CreateOrderRequest,
) -> Result<CreateOrderResponse, SquareApiError> {
let response = self.http_client.post(&self.url(), body).await?;
response.deserialize().await
}
pub async fn batch_retrieve_orders(
&self,
body: &BatchRetrieveOrdersRequest,
) -> Result<BatchRetrieveOrdersResponse, SquareApiError> {
let url = format!("{}/batch-retrieve", &self.url());
let response = self.http_client.post(&url, body).await?;
response.deserialize().await
}
pub async fn calculate_order(
&self,
body: &CalculateOrderRequest,
) -> Result<CalculateOrderResponse, SquareApiError> {
let url = format!("{}/calculate", &self.url());
let response = self.http_client.post(&url, body).await?;
response.deserialize().await
}
pub async fn clone_order(
&self,
body: &CloneOrderRequest,
) -> Result<CloneOrderResponse, SquareApiError> {
let url = format!("{}/clone", &self.url());
let response = self.http_client.post(&url, body).await?;
response.deserialize().await
}
pub async fn search_orders(
&self,
body: &SearchOrdersRequest,
) -> Result<SearchOrdersResponse, SquareApiError> {
let url = format!("{}/search", &self.url());
let response = self.http_client.post(&url, body).await?;
response.deserialize().await
}
pub async fn retrieve_order(
&self,
order_id: &str,
) -> Result<RetrieveOrderResponse, SquareApiError> {
let url = format!("{}/{}", &self.url(), order_id);
let response = self.http_client.get(&url).await?;
response.deserialize().await
}
pub async fn update_order(
&self,
order_id: &str,
body: &UpdateOrderRequest,
) -> Result<UpdateOrderResponse, SquareApiError> {
let url = format!("{}/{}", &self.url(), order_id);
let response = self.http_client.put(&url, body).await?;
response.deserialize().await
}
pub async fn pay_order(
&self,
order_id: &str,
body: &PayOrderRequest,
) -> Result<PayOrderResponse, SquareApiError> {
let url = format!("{}/{}/pay", &self.url(), order_id);
let response = self.http_client.post(&url, body).await?;
response.deserialize().await
}
fn url(&self) -> String {
format!("{}{}", &self.config.get_base_url(), DEFAULT_URI)
}
}