up_api/v1/
utilities.rs

1use crate::v1::{Client, error, BASE_URL};
2
3use serde::Deserialize;
4
5// ----------------- Request Objects -----------------
6
7#[derive(Deserialize, Debug)]
8pub struct PingResponse {
9    pub meta: Meta,
10}
11
12#[derive(Deserialize, Debug)]
13pub struct Meta {
14    /// The unique identifier of the authenticated customer.
15    pub id: String,
16    #[serde(rename = "statusEmoji")]
17    /// A cute emoji that represents the response status.
18    pub status_emoji: String,
19}
20
21impl Client {
22    /// Make a basic ping request to the API. This is useful to verify that
23    /// authentication is functioning correctly.
24    pub async fn ping(&self) -> Result<PingResponse, error::Error> {
25        let url = reqwest::Url::parse(
26            &format!("{}/util/ping", BASE_URL)
27        ).map_err(error::Error::UrlParse)?;
28
29        let res = reqwest::Client::new()
30            .get(url)
31            .header("Authorization", self.auth_header())
32            .send()
33            .await
34            .map_err(error::Error::Request)?;
35
36        match res.status() {
37            reqwest::StatusCode::OK => {
38                let body =
39                    res.text()
40                    .await
41                    .map_err(error::Error::BodyRead)?;
42                println!("{}", body);
43                let ping_response: PingResponse =
44                    serde_json::from_str(&body)
45                    .map_err(error::Error::Json)?;
46
47                Ok(ping_response)
48            },
49            _ => {
50                let body =
51                    res.text()
52                    .await
53                    .map_err(error::Error::BodyRead)?;
54                let error: error::ErrorResponse =
55                    serde_json::from_str(&body)
56                    .map_err(error::Error::Json)?;
57
58                Err(error::Error::Api(error))
59            }
60        }
61    }
62}