forked from Echo-Mirror-Butler/echomirror-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.rs
More file actions
100 lines (89 loc) · 2.94 KB
/
Copy pathclient.rs
File metadata and controls
100 lines (89 loc) · 2.94 KB
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
use std::time::Duration;
use echomirror_core::config::StellarNetwork as CoreNetwork;
use echomirror_core::{EchoMirrorClient as CoreClient, EchoMirrorConfig as CoreConfig};
use pyo3::prelude::*;
use pyo3::types::PyAny;
use crate::errors::to_py_err;
/// Stellar network to connect to.
#[pyclass(name = "StellarNetwork", eq, eq_int)]
#[derive(Clone, Copy, PartialEq)]
pub enum PyStellarNetwork {
Mainnet,
Testnet,
}
impl From<PyStellarNetwork> for CoreNetwork {
fn from(n: PyStellarNetwork) -> Self {
match n {
PyStellarNetwork::Mainnet => CoreNetwork::Mainnet,
PyStellarNetwork::Testnet => CoreNetwork::Testnet,
}
}
}
impl From<CoreNetwork> for PyStellarNetwork {
fn from(n: CoreNetwork) -> Self {
match n {
CoreNetwork::Mainnet => PyStellarNetwork::Mainnet,
CoreNetwork::Testnet => PyStellarNetwork::Testnet,
}
}
}
/// Low-level API client shared by `MoodClient`, `StellarClient`, and `SocialClient`.
///
/// Most users should reach for the `EchoMirror` convenience class instead, which
/// constructs one of these plus all three sub-clients together.
#[pyclass(name = "EchoMirrorClient")]
#[derive(Clone)]
pub struct PyEchoMirrorClient {
pub(crate) inner: CoreClient,
}
#[pymethods]
impl PyEchoMirrorClient {
#[new]
#[pyo3(signature = (api_key, base_url=None, network=PyStellarNetwork::Mainnet, timeout_secs=10, horizon_url=None, friendbot_url=None))]
pub fn new(
api_key: String,
base_url: Option<String>,
network: PyStellarNetwork,
timeout_secs: u64,
horizon_url: Option<String>,
friendbot_url: Option<String>,
) -> PyResult<Self> {
let mut config = CoreConfig::new(api_key);
config.network = network.into();
if let Some(url) = base_url {
config = config.with_base_url(url);
}
config = config.with_timeout(Duration::from_secs(timeout_secs));
if let Some(url) = horizon_url {
config = config.with_horizon_url(url);
}
if let Some(url) = friendbot_url {
config = config.with_friendbot_url(url);
}
let inner = CoreClient::new(config).map_err(to_py_err)?;
Ok(Self { inner })
}
/// Set (or clear, with `None`) the bearer auth token used for authenticated requests.
#[pyo3(signature = (token=None))]
pub fn set_auth_token<'py>(
&self,
py: Python<'py>,
token: Option<String>,
) -> PyResult<Bound<'py, PyAny>> {
let client = self.inner.clone();
pyo3_async_runtimes::tokio::future_into_py(py, async move {
client.set_auth_token(token).await;
Ok(())
})
}
#[getter]
fn network(&self) -> PyStellarNetwork {
self.inner.config().network.into()
}
fn __repr__(&self) -> String {
format!(
"EchoMirrorClient(network={:?})",
self.inner.config().network
)
}
}