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
use std::future::Future;
use std::pin::Pin;

use crate::endpoints::{
    BaseClient, PrivateClient, PrivateEndpoints, PublicClient, PublicEndpoints,
};
use crate::models::{ApiCredentials, ApiParams};

/// Client for the public API. It implements all the methods under the
/// [`PublicEndpoints`] trait.
pub type PublicApiClient = ApiClient<PublicClient>;
/// Client for the private and public APIs. It implements all the methods under
/// the [`PublicEndpoints`] and [`PrivateApiClient`] traits.
pub type PrivateApiClient = ApiClient<PrivateClient>;

/// Base client class for the API. It shouldn't be used directly, rather prefer
/// the type synonyms [`PublicApiClient`] and [`PrivateApiClient`].
pub struct ApiClient<C: BaseClient> {
    client: C,
}

impl PublicApiClient {
    pub fn new(http: reqwest::Client, api_params: ApiParams) -> Self {
        Self {
            client: PublicClient::new(http, api_params),
        }
    }

    pub fn default() -> Self {
        Self {
            client: PublicClient::default(),
        }
    }

    /// Consume the public client to get a private one, by supplying the API
    /// credentials.
    pub fn set_credentials(
        self,
        api_credentials: ApiCredentials,
    ) -> PrivateApiClient {
        ApiClient {
            client: PrivateClient::new(
                self.client.http,
                self.client.api_params,
                api_credentials,
            ),
        }
    }
}

impl PrivateApiClient {
    pub fn new(
        http: reqwest::Client,
        api_params: ApiParams,
        api_credentials: ApiCredentials,
    ) -> Self {
        Self {
            client: PrivateClient::new(http, api_params, api_credentials),
        }
    }

    pub fn default_with_credentials(api_credentials: ApiCredentials) -> Self {
        Self {
            client: PrivateClient::new(
                reqwest::Client::default(),
                ApiParams::default(),
                api_credentials,
            ),
        }
    }
}

macro_rules! nullary_method_impls {
    ( $api_request:ident, $($func:ident: $name:expr),* $(,)? ) => {
        $(
            fn $func<'a, 'async_trait>(&'a self) ->
                Pin<Box<dyn Future<Output = reqwest::Result<String>> + Send + 'async_trait>>
                where 'a: 'async_trait, Self: Sync + 'async_trait {
                Box::pin(self.client.$api_request($name, "".into()))
            }
        )*
    };
}

macro_rules! unary_method_impls {
    ( $api_request:ident, $($func:ident: $name:expr),* $(,)? ) => {
        $(
            fn $func<'a, 'async_trait>(&'a self, params: String) ->
                Pin<Box<dyn Future<Output = reqwest::Result<String>> + Send + 'async_trait>>
                where 'a: 'async_trait, Self: Sync + 'async_trait {
                Box::pin(self.client.$api_request($name, params))
            }
        )*
    };
}

macro_rules! nullary_public_method_impls {
    ( $($func:ident: $name:expr),* $(,)? ) => {
        nullary_method_impls!(public_api_request, $($func: $name,)*);
    };
}

macro_rules! unary_public_method_impls {
    ( $($func:ident: $name:expr),* $(,)? ) => {
        unary_method_impls!(public_api_request, $($func: $name,)*);
    };
}

macro_rules! nullary_private_method_impls {
    ( $($func:ident: $name:expr),* $(,)? ) => {
        nullary_method_impls!(private_api_request, $($func: $name,)*);
    };
}

macro_rules! unary_private_method_impls {
    ( $($func:ident: $name:expr),* $(,)? ) => {
        unary_method_impls!(private_api_request, $($func: $name,)*);
    };
}

impl PublicEndpoints for PublicApiClient {
    nullary_public_method_impls! {
        time: "Time",
        system_status: "SystemStatus",
    }

    unary_public_method_impls! {
        assets: "Assets",
        asset_pairs: "AssetPairs",
        ticker: "Ticker",
        ohlc: "OHLC",
        depth: "Depth",
        trades: "Trades",
        spread: "Spread",
    }
}

impl PublicEndpoints for PrivateApiClient {
    nullary_public_method_impls! {
        time: "Time",
        system_status: "SystemStatus",
    }

    unary_public_method_impls! {
        assets: "Assets",
        asset_pairs: "AssetPairs",
        ticker: "Ticker",
        depth: "Depth",
        trades: "Trades",
        spread: "Spread",
        ohlc: "OHLC",
    }
}

impl PrivateEndpoints for PrivateApiClient {
    nullary_private_method_impls! {
        balance: "Balance",
    }

    unary_private_method_impls! {
        trade_balance: "TradeBalance",
        open_orders: "OpenOrders",
        closed_orders: "ClosedOrders",
        query_orders: "QueryOrders",
        trades_history: "TradesHistory",
        query_trades: "QueryTrades",
        open_positions: "OpenPositions",
        ledgers: "Ledgers",
        query_ledgers: "QueryLedgers",
        trade_volume: "TradeVolume",
        add_export: "AddExport",
        export_status: "ExportStatus",
        retrieve_export: "RetrieveExport",
        remove_export: "RemoveExport",
        add_order: "AddOrder",
        cancel_order: "CancelOrder",
        cancel_all: "CancelAll",
        cancel_all_after: "CancelAllOrdersAfter",
        deposit_methods: "DepositMethods",
        deposit_addresses: "DepositAddresses",
        deposit_status: "DepositStatus",
        withdraw_info: "WithdrawInfo",
        withdraw: "Withdraw",
        withdraw_status: "WithdrawStatus",
        withdraw_cancel: "WithdrawCancel",
        wallet_transfer: "WalletTransfer",
        get_websockets_token: "GetWebsocketsToken",
    }
}