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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#![cfg_attr(docsrs, feature(doc_cfg))]

use generic_api_client::{http::{self, *}, websocket::*};
use serde::Serialize;
use traits::*;

pub use generic_api_client;
pub use exchanges::*;

mod exchanges;
pub mod traits;

// very long type, make it a macro
macro_rules! request_return_type {
    ($lt:lifetime, $Response:ty, $Options:ty,  $Body:ty) => {
        Result<
            <<$Options as HttpOption<$lt, $Response, $Body>>::RequestHandler as RequestHandler<$Body>>::Successful,
            RequestError<
                <<$Options as HttpOption<$lt, $Response, $Body>>::RequestHandler as RequestHandler<$Body>>::BuildError,
                <<$Options as HttpOption<$lt, $Response, $Body>>::RequestHandler as RequestHandler<$Body>>::Unsuccessful,
            >,
        >
    };
}

#[derive(Default, Debug, Clone)]
pub struct Client {
    client: http::Client,
    #[cfg(feature = "binance")]
    binance: binance::BinanceOptions,
    #[cfg(feature = "bitflyer")]
    bitflyer: bitflyer::BitFlyerOptions,
    #[cfg(feature = "bybit")]
    bybit: bybit::BybitOptions,
    #[cfg(feature = "coincheck")]
    coincheck: coincheck::CoincheckOptions,
}

impl Client {
    /// Creates a new [Client].
    pub fn new() -> Self {
        Self::default()
    }

    /// Update the default options for this [Client]
    #[inline(always)]
    pub fn update_default_option<O>(&mut self, option: O)
    where
        O: HandlerOption,
        Self: GetOptions<O::Options>,
    {
        self.default_options_mut().update(option);
    }

    #[inline]
    fn merged_options<O>(&self, options: impl IntoIterator<Item=O>) -> O::Options
    where
        O: HandlerOption,
        Self:GetOptions<O::Options>,
    {
        let mut default_options = self.default_options().clone();
        for option in options {
            default_options.update(option);
        }
        default_options
    }

    /// see [http::Client::request()]
    #[inline(always)]
    pub async fn request<'a, R, O, Q, B>(&self, method: Method, url: &str, query: Option<&Q>, body: Option<B>, options: impl IntoIterator<Item=O>)
        -> request_return_type!('a, R, O, B)
    where
        O: HttpOption<'a, R, B>,
        O::RequestHandler: RequestHandler<B>,
        Self: GetOptions<O::Options>,
        Q: Serialize + ?Sized,
    {
        self.client.request(method, url, query, body, &O::request_handler(self.merged_options(options))).await
    }

    /// see [http::Client::get()]
    #[inline(always)]
    pub async fn get<'a, R, O, Q>(&self, url: &str, query: Option<&Q>, options: impl IntoIterator<Item=O>) -> request_return_type!('a, R, O, ())
    where
        O: HttpOption<'a, R, ()>,
        O::RequestHandler: RequestHandler<()>,
        Self: GetOptions<O::Options>,
        Q: Serialize + ?Sized,
    {
        self.client.get(url, query, &O::request_handler(self.merged_options(options))).await
    }

    /// see [http::Client::get_no_query()]
    #[inline(always)]
    pub async fn get_no_query<'a, R, O>(&self, url: &str, options: impl IntoIterator<Item=O>) -> request_return_type!('a, R, O, ())
    where
        O: HttpOption<'a, R, ()>,
        O::RequestHandler: RequestHandler<()>,
        Self: GetOptions<O::Options>,
    {
        self.client.get_no_query(url, &O::request_handler(self.merged_options(options))).await
    }

    /// see [http::Client::post()]
    #[inline(always)]
    pub async fn post<'a, R, O, B>(&self, url: &str, body: Option<B>, options: impl IntoIterator<Item=O>)
        -> request_return_type!('a, R, O, B)
    where
        O: HttpOption<'a, R, B>,
        O::RequestHandler: RequestHandler<B>,
        Self: GetOptions<O::Options>,
    {
        self.client.post(url, body, &O::request_handler(self.merged_options(options))).await
    }

    /// see [http::Client::post_no_body()]
    #[inline(always)]
    pub async fn post_no_body<'a, R, O>(&self, url: &str, options: impl IntoIterator<Item=O>)
        -> request_return_type!('a, R, O, ())
    where
        O: HttpOption<'a, R, ()>,
        O::RequestHandler: RequestHandler<()>,
        Self: GetOptions<O::Options>,
    {
        self.client.post_no_body(url, &O::request_handler(self.merged_options(options))).await
    }

    /// see [http::Client::put()]
    #[inline(always)]
    pub async fn put<'a, R, O, B>(&self, url: &str, body: Option<B>, options: impl IntoIterator<Item=O>)
        -> request_return_type!('a, R, O, B)
    where
        O: HttpOption<'a, R, B>,
        O::RequestHandler: RequestHandler<B>,
        Self: GetOptions<O::Options>,
    {
        self.client.put(url, body, &O::request_handler(self.merged_options(options))).await
    }

    /// see [http::Client::put_no_body()]
    #[inline(always)]
    pub async fn put_no_body<'a, R, O>(&self, url: &str, options: impl IntoIterator<Item=O>)
        -> request_return_type!('a, R, O, ())
    where
        O: HttpOption<'a, R, ()>,
        O::RequestHandler: RequestHandler<()>,
        Self: GetOptions<O::Options>,
    {
        self.client.put_no_body(url, &O::request_handler(self.merged_options(options))).await
    }

    /// see [http::Client::delete()]
    #[inline(always)]
    pub async fn delete<'a, R, O, Q>(&self, url: &str, query: Option<&Q>, options: impl IntoIterator<Item=O>) -> request_return_type!('a, R, O, ())
    where
        O: HttpOption<'a, R, ()>,
        O::RequestHandler: RequestHandler<()>,
        Self: GetOptions<O::Options>,
        Q: Serialize + ?Sized,
    {
        self.client.delete(url, query, &O::request_handler(self.merged_options(options))).await
    }

    /// see [http::Client::delete_no_query()]
    #[inline(always)]
    pub async fn delete_no_query<'a, R, O>(&self, url: &str, options: impl IntoIterator<Item=O>) -> request_return_type!('a, R, O, ())
    where
        O: HttpOption<'a, R, ()>,
        O::RequestHandler: RequestHandler<()>,
        Self: GetOptions<O::Options>,
    {
        self.client.delete_no_query(url, &O::request_handler(self.merged_options(options))).await
    }

    #[inline(always)]
    pub async fn websocket<O, H>(&self, url: &str, handler: H, options: impl IntoIterator<Item=O>) -> Result<WebSocketConnection<O::WebSocketHandler>, TungsteniteError>
    where
        O: WebSocketOption<H>,
        O::WebSocketHandler: WebSocketHandler,
        Self: GetOptions<O::Options>,
    {
        WebSocketConnection::new(url, O::websocket_handler(handler, self.merged_options(options))).await
    }
}

pub trait GetOptions<O: HandlerOptions> {
    fn default_options(&self) -> &O;
    fn default_options_mut(&mut self) -> &mut O;
}

#[cfg(feature = "binance")]
#[cfg_attr(docsrs, doc(cfg(feature = "binance")))]
impl GetOptions<binance::BinanceOptions> for Client {
    #[inline(always)]
    fn default_options(&self) -> &binance::BinanceOptions {
        &self.binance
    }

    #[inline(always)]
    fn default_options_mut(&mut self) -> &mut binance::BinanceOptions {
        &mut self.binance
    }
}

#[cfg(feature = "bitflyer")]
#[cfg_attr(docsrs, doc(cfg(feature = "bitflyer")))]
impl GetOptions<bitflyer::BitFlyerOptions> for Client {
    #[inline(always)]
    fn default_options(&self) -> &bitflyer::BitFlyerOptions {
        &self.bitflyer
    }

    #[inline(always)]
    fn default_options_mut(&mut self) -> &mut bitflyer::BitFlyerOptions {
        &mut self.bitflyer
    }
}

#[cfg(feature = "bybit")]
#[cfg_attr(docsrs, doc(cfg(feature = "bybit")))]
impl GetOptions<bybit::BybitOptions> for Client {
    #[inline(always)]
    fn default_options(&self) -> &bybit::BybitOptions {
        &self.bybit
    }

    #[inline(always)]
    fn default_options_mut(&mut self) -> &mut bybit::BybitOptions {
        &mut self.bybit
    }
}

#[cfg(feature = "coincheck")]
#[cfg_attr(docsrs, doc(cfg(feature = "coincheck")))]
impl GetOptions<coincheck::CoincheckOptions> for Client {
    #[inline(always)]
    fn default_options(&self) -> &coincheck::CoincheckOptions {
        &self.coincheck
    }

    #[inline(always)]
    fn default_options_mut(&mut self) -> &mut coincheck::CoincheckOptions {
        &mut self.coincheck
    }
}