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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#![allow(clippy::pedantic)]
#![warn(missing_docs)]
#![doc = include_str!("../README.md")]
#[macro_use]
mod request;
pub mod methods;
pub mod responses;
pub mod user;
pub use crate::{responses::*, user::*};
use chrono::prelude::*;
use reqwest::Client;
use serde::Deserialize;
use std::fmt;
#[derive(Clone, Debug, Deserialize)]
pub struct CCashSessionProperties {
pub version: u32,
pub max_log: u32,
pub return_on_del: Option<String>,
}
#[derive(Debug, Deserialize)]
pub struct TransactionLog {
pub to: String,
pub from: String,
pub amount: u32,
pub time: i64,
}
impl fmt::Display for TransactionLog {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}: {} ({} CSH) -> {}",
DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(self.time, 0), Utc),
&self.from,
self.amount,
&self.to,
)
}
}
#[doc = include_str!("../examples/get_balance.rs")]
#[derive(Debug, Clone)]
pub struct CCashSession {
session_url: String,
is_connected: bool,
client: Option<Client>,
properties: Option<CCashSessionProperties>,
}
impl CCashSession {
pub fn new(base_url: &str) -> CCashSession {
Self {
session_url: format!("{}/api", base_url),
is_connected: false,
client: None,
properties: None,
}
}
pub async fn establish_connection(&mut self) -> Result<(), CCashError> {
if self.is_connected {
return Ok(());
}
let client = Client::builder().build()?;
let request = client
.get(format!("{}/properties", self.session_url))
.header("Accept", "application/json")
.build()?;
let response = client.execute(request).await?;
if let Ok(v) = response.json::<CCashSessionProperties>().await {
self.properties = Some(v.clone());
self.session_url = format!("{}/v{}", self.session_url, v.version);
self.is_connected = true;
self.client = Some(client);
Ok(())
} else {
Err(CCashError::CouldNotParsePropertiesResponse)
}
}
pub fn get_client(&self) -> &Option<Client> { &self.client }
pub fn is_connected(&self) -> bool { self.is_connected }
pub fn get_properties(&self) -> &Option<CCashSessionProperties> { &self.properties }
}