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 authentication is functioning correctly.
23    pub async fn ping(&self) -> Result<PingResponse, error::Error> {
24        let url = reqwest::Url::parse(&format!("{}/util/ping", BASE_URL)).map_err(error::Error::UrlParse)?;
25
26        let res = reqwest::Client::new()
27            .get(url)
28            .header("Authorization", self.auth_header())
29            .send()
30            .await
31            .map_err(error::Error::Request)?;
32
33        match res.status() {
34            reqwest::StatusCode::OK => {
35                let body = res.text().await.map_err(error::Error::BodyRead)?;
36                println!("{}", body);
37                let ping_response : PingResponse = serde_json::from_str(&body).map_err(error::Error::Json)?;
38
39                Ok(ping_response)
40            },
41            _ => {
42                let body = res.text().await.map_err(error::Error::BodyRead)?;
43                let error : error::ErrorResponse = serde_json::from_str(&body).map_err(error::Error::Json)?;
44
45                Err(error::Error::Api(error))
46            }
47        }
48    }
49}