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
use crate::{Client, Result};
use serde::{de::DeserializeOwned, Deserialize};

/// - https://www.kraken.com/features/api#get-server-time
/// - https://api.kraken.com/0/public/Time
#[must_use = "Does nothing until you send or execute it"]
pub struct GetServerTimeRequest {
    client: Client,
}

impl GetServerTimeRequest {
    pub async fn execute<T: DeserializeOwned>(self) -> Result<T> {
        self.client.send_public("/0/public/Time").await
    }

    pub async fn send(self) -> Result<GetServerTimeResponse> {
        self.execute().await
    }
}

#[derive(Debug, Deserialize)]
pub struct GetServerTimeResponse {
    pub unixtime: i64,
    pub rfc1123: String,
}

impl Client {
    pub fn get_server_time(&self) -> GetServerTimeRequest {
        GetServerTimeRequest {
            client: self.clone(),
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::{Client, JsonValue, Result};

    #[test]
    fn get_server_time() {
        let rt = tokio::runtime::Runtime::new().unwrap();

        rt.block_on(async {
            let client = Client::default();

            let resp = client.get_server_time().send().await;

            match resp {
                Ok(resp) => println!("{}", resp.unixtime),
                Err(error) => eprintln!("{:?}", error),
            }

            let resp: Result<JsonValue> = client.get_server_time().execute().await;

            match resp {
                Ok(resp) => println!("{}", resp),
                Err(error) => eprintln!("{:?}", error),
            }
        });
    }
}