square_api_client/api/
inventory_api.rs

1//!The Inventory API exposes a set of objects to represent inventory adjustments
2//! and physical counts for quantities of products (as item variations) and
3//! transitions of stocked products to the relevant inventory state.
4//!
5//! hese include the following key data types for the Inventory API:
6
7//! InventoryCount - computed quantity of an item variation at a specific location.
8//! InventoryAdjustment - quantity of an item variation transitioning from one state to another.
9//! InventoryPhysicalCount - verified quantity of an item variation at a specific location.
10//! SourceApplication - application that makes an inventory change and helps trace sources of inventory changes.
11
12use crate::{
13    config::Configuration,
14    http::client::HttpClient,
15    models::{
16        errors::ApiError, BatchChangeInventoryRequest, BatchChangeInventoryResponse,
17        BatchRetrieveInventoryChangesRequest, BatchRetrieveInventoryChangesResponse,
18        BatchRetrieveInventoryCountsRequest, BatchRetrieveInventoryCountsResponse,
19        RetrieveInventoryAdjustmentResponse, RetrieveInventoryCountParams,
20        RetrieveInventoryCountResponse, RetrieveInventoryPhysicalCount,
21        RetrieveInventoryTransferResponse,
22    },
23};
24
25const DEFAULT_URI: &str = "/inventory";
26
27pub struct InventoryApi {
28    /// App config information
29    config: Configuration,
30    /// HTTP Client for requests to the Inventory API endpoints
31    client: HttpClient,
32}
33
34impl InventoryApi {
35    pub fn new(config: Configuration, client: HttpClient) -> Self {
36        Self { config, client }
37    }
38
39    /// Retrieves the current calculated stock count for a given CatalogObject at a given set of Locations.
40    pub async fn retrieve_inventory_count(
41        &self,
42        catalog_object_id: &str,
43        params: RetrieveInventoryCountParams,
44    ) -> Result<RetrieveInventoryCountResponse, ApiError> {
45        let url = format!("{}/{}{}", &self.url(), catalog_object_id, params.to_query_string());
46        let response = self.client.get(&url).await?;
47
48        response.deserialize().await
49    }
50
51    /// Returns the InventoryTransfer object with the provided transfer_id.
52    pub async fn retrieve_inventory_transfer(
53        &self,
54        transfer_id: &str,
55    ) -> Result<RetrieveInventoryTransferResponse, ApiError> {
56        let url = format!("{}/{}", &self.url(), transfer_id);
57        let response = self.client.get(&url).await?;
58
59        response.deserialize().await
60    }
61
62    /// Returns the InventoryAdjustment object with the provided adjustment id.
63    pub async fn retrieve_inventory_adjustment(
64        &self,
65        adjustment_id: &str,
66    ) -> Result<RetrieveInventoryAdjustmentResponse, ApiError> {
67        let url = format!("{}/{}", &self.url(), adjustment_id);
68        let response = self.client.get(&url).await?;
69
70        response.deserialize().await
71    }
72
73    /// Returns the InventoryPhysicalCount object with the provided physical_count_id.
74    pub async fn retrieve_inventory_physical_count(
75        &self,
76        physical_count_id: &str,
77    ) -> Result<RetrieveInventoryPhysicalCount, ApiError> {
78        let url = format!("{}/{}", &self.url(), physical_count_id);
79        let response = self.client.get(&url).await?;
80
81        response.deserialize().await
82    }
83
84    /// Applies adjustments and counts to the provided item quantities.
85    /// On success: returns the current calculated counts for all objects referenced in the request.
86    /// On failure: returns a list of related errors.
87    pub async fn batch_change(
88        &self,
89        body: &BatchChangeInventoryRequest,
90    ) -> Result<BatchChangeInventoryResponse, ApiError> {
91        let url = format!("{}/changes/batch-create", &self.url());
92        let response = self.client.post(&url, body).await?;
93
94        response.deserialize().await
95    }
96
97    /// Returns current counts for the provided CatalogObjects at the requested Locations.
98    /// Results are paginated and sorted in descending order according to their calculated_at timestamp.
99    pub async fn batch_retrieve_count(
100        &self,
101        body: &BatchRetrieveInventoryCountsRequest,
102    ) -> Result<BatchRetrieveInventoryCountsResponse, ApiError> {
103        let url = format!("{}/counts/batch-retrieve", &self.url());
104        let response = self.client.post(&url, body).await?;
105
106        response.deserialize().await
107    }
108
109    /// Returns historical physical counts and adjustments based on the provided filter criteria.
110    /// Results are paginated and sorted in ascending order according their occurred_at timestamp.
111    pub async fn batch_retrieve_changes(
112        &self,
113        body: &BatchRetrieveInventoryChangesRequest,
114    ) -> Result<BatchRetrieveInventoryChangesResponse, ApiError> {
115        let url = format!("{}/changes/batch-retrieve", &self.url());
116        let response = self.client.post(&url, body).await?;
117
118        response.deserialize().await
119    }
120
121    fn url(&self) -> String {
122        format!("{}{}", &self.config.get_base_url(), DEFAULT_URI)
123    }
124}