1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//! Get a list of all a seller's locations.
//!
//! Many sellers use multiple locations to track where they make sales. The Locations API allows you
//! to get data about those locations, such as their addresses, names, and business hours.

use crate::{
    config::Configuration,
    http::client::HttpClient,
    models::{
        errors::ApiError, CreateLocationRequest, CreateLocationResponse, ListLocationsResponse,
        RetrieveLocationResponse, UpdateLocationRequest, UpdateLocationResponse,
    },
};

const DEFAULT_URI: &str = "/locations";

/// Get a list of all a seller's locations.
pub struct LocationsApi {
    /// App config information
    config: Configuration,
    /// HTTP Client for requests to the Locations API endpoints
    client: HttpClient,
}

impl LocationsApi {
    /// Instantiates a new `LocationsApi`
    pub fn new(config: Configuration, client: HttpClient) -> Self {
        Self { config, client }
    }

    /// Provides details about all of the seller's
    /// [locations](https://developer.squareup.com/docs/locations-api), including those
    /// with an inactive status.
    pub async fn list_locations(&self) -> Result<ListLocationsResponse, ApiError> {
        let response = self.client.get(&self.url()).await?;

        response.deserialize().await
    }

    /// Creates a [location](https://developer.squareup.com/docs/locations-api).
    ///
    /// Creating new locations allows for separate configuration of receipt layouts, item prices,
    /// and sales reports. Developers can use locations to separate sales activity through
    /// applications that integrate with Square from sales activity elsewhere in a seller's account.
    /// Locations created programmatically with the Locations API last forever and are visible to
    /// the seller for their own management. Therefore, ensure that each location has a sensible and
    /// unique name.
    pub async fn create_location(
        &self,
        body: &CreateLocationRequest,
    ) -> Result<CreateLocationResponse, ApiError> {
        let response = self.client.post(&self.url(), body).await?;

        response.deserialize().await
    }

    /// Retrieves details of a single location.
    ///
    /// Specify "main" as the location ID to retrieve details of the [main
    /// location](https://developer.squareup.com/docs/locations-api#about-the-main-location).
    pub async fn retrieve_location(
        &self,
        location_id: &str,
    ) -> Result<RetrieveLocationResponse, ApiError> {
        let url = format!("{}/{}", &self.url(), location_id);
        let response = self.client.get(&url).await?;

        response.deserialize().await
    }

    /// Updates a [location](https://developer.squareup.com/docs/locations-api).
    pub async fn update_location(
        &self,
        location_id: &str,
        body: &UpdateLocationRequest,
    ) -> Result<UpdateLocationResponse, ApiError> {
        let url = format!("{}/{}", &self.url(), location_id);
        let response = self.client.post(&url, body).await?;

        response.deserialize().await
    }

    /// Constructs the basic entity URL including domain and entity path. Any additional path
    /// elements (e.g. path parameters) will need to be appended to this URL.
    fn url(&self) -> String {
        format!("{}{}", &self.config.get_base_url(), DEFAULT_URI)
    }
}