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
pub mod error;
pub mod private_client;
pub mod public_client;
use self::error::{Error, ErrorKind, ErrorMessage, StatusError};
use chrono::{DateTime, TimeZone, Utc};
use serde::{self, de};
use serde::{Deserialize, Deserializer};
use std::fmt;
pub(crate) const COINBASE_API_URL: &'static str = "https://api.pro.coinbase.com";
pub(crate) const COINBASE_SANDBOX_API_URL: &'static str =
"https://api-public.sandbox.pro.coinbase.com";
pub(crate) async fn deserialize_response<T>(response: reqwest::Response) -> Result<T, Error>
where
T: serde::de::DeserializeOwned,
{
let status = response.status();
if !status.is_success() {
let error_message = response.json::<ErrorMessage>().await?;
return Err(Error::new(ErrorKind::Status(StatusError::new(
status.as_u16(),
error_message.message,
))));
}
Ok(response.json::<T>().await?)
}
pub(crate) fn deserialize_to_f64<'de, D>(deserializer: D) -> Result<f64, D::Error>
where
D: serde::Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
s.parse::<f64>().map_err(serde::de::Error::custom)
}
pub(crate) fn deserialize_to_date<'de, D>(deserializer: D) -> Result<DateTime<Utc>, D::Error>
where
D: Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
Utc.datetime_from_str(&s, "%+")
.map_err(serde::de::Error::custom)
}
pub(crate) fn deserialize_option_to_date<'de, D>(
deserializer: D,
) -> Result<Option<DateTime<Utc>>, D::Error>
where
D: Deserializer<'de>,
{
#[derive(Deserialize)]
struct Wrapper(#[serde(deserialize_with = "deserialize_to_date")] DateTime<Utc>);
let v = Option::deserialize(deserializer)?;
Ok(v.map(|Wrapper(a)| a))
}