square_api_client/api/
locations_api.rs

1//! Get a list of all a seller's locations.
2//!
3//! Many sellers use multiple locations to track where they make sales. The Locations API allows you
4//! to get data about those locations, such as their addresses, names, and business hours.
5
6use crate::{
7    config::Configuration,
8    http::client::HttpClient,
9    models::{
10        errors::ApiError, CreateLocationRequest, CreateLocationResponse, ListLocationsResponse,
11        RetrieveLocationResponse, UpdateLocationRequest, UpdateLocationResponse,
12    },
13};
14
15const DEFAULT_URI: &str = "/locations";
16
17/// Get a list of all a seller's locations.
18pub struct LocationsApi {
19    /// App config information
20    config: Configuration,
21    /// HTTP Client for requests to the Locations API endpoints
22    client: HttpClient,
23}
24
25impl LocationsApi {
26    /// Instantiates a new `LocationsApi`
27    pub fn new(config: Configuration, client: HttpClient) -> Self {
28        Self { config, client }
29    }
30
31    /// Provides details about all of the seller's
32    /// [locations](https://developer.squareup.com/docs/locations-api), including those
33    /// with an inactive status.
34    pub async fn list_locations(&self) -> Result<ListLocationsResponse, ApiError> {
35        let response = self.client.get(&self.url()).await?;
36
37        response.deserialize().await
38    }
39
40    /// Creates a [location](https://developer.squareup.com/docs/locations-api).
41    ///
42    /// Creating new locations allows for separate configuration of receipt layouts, item prices,
43    /// and sales reports. Developers can use locations to separate sales activity through
44    /// applications that integrate with Square from sales activity elsewhere in a seller's account.
45    /// Locations created programmatically with the Locations API last forever and are visible to
46    /// the seller for their own management. Therefore, ensure that each location has a sensible and
47    /// unique name.
48    pub async fn create_location(
49        &self,
50        body: &CreateLocationRequest,
51    ) -> Result<CreateLocationResponse, ApiError> {
52        let response = self.client.post(&self.url(), body).await?;
53
54        response.deserialize().await
55    }
56
57    /// Retrieves details of a single location.
58    ///
59    /// Specify "main" as the location ID to retrieve details of the [main
60    /// location](https://developer.squareup.com/docs/locations-api#about-the-main-location).
61    pub async fn retrieve_location(
62        &self,
63        location_id: &str,
64    ) -> Result<RetrieveLocationResponse, ApiError> {
65        let url = format!("{}/{}", &self.url(), location_id);
66        let response = self.client.get(&url).await?;
67
68        response.deserialize().await
69    }
70
71    /// Updates a [location](https://developer.squareup.com/docs/locations-api).
72    pub async fn update_location(
73        &self,
74        location_id: &str,
75        body: &UpdateLocationRequest,
76    ) -> Result<UpdateLocationResponse, ApiError> {
77        let url = format!("{}/{}", &self.url(), location_id);
78        let response = self.client.post(&url, body).await?;
79
80        response.deserialize().await
81    }
82
83    /// Constructs the basic entity URL including domain and entity path. Any additional path
84    /// elements (e.g. path parameters) will need to be appended to this URL.
85    fn url(&self) -> String {
86        format!("{}{}", &self.config.get_base_url(), DEFAULT_URI)
87    }
88}