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

use crate::models::{ApiCredentials, ApiParams, Endpoint};
use crate::request::api_request;

macro_rules! nullary_method_defs {
    ( $($func:ident),* $(,)? ) => {
        $(
            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;
        )*
    };
}

macro_rules! unary_method_defs {
    ( $($func:ident),* $(,)? ) => {
        $(
            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;
        )*
    };
}

/// A collection of all the public endpoints ([Kraken API
/// documentation](https://docs.kraken.com/rest/#tag/Market-Data)).
pub trait PublicEndpoints {
    nullary_method_defs! {
        time,
        system_status,
    }

    unary_method_defs! {
        assets,
        asset_pairs,
        ticker,
        ohlc,
        depth,
        trades,
        spread,
    }
}

/// A collection of all the private endpoints ([Kraken API
/// documentation](https://docs.kraken.com/rest/#tag/User-Data)).
pub trait PrivateEndpoints {
    nullary_method_defs! {
        balance,
    }

    unary_method_defs! {
        trade_balance,
        open_orders,
        closed_orders,
        query_orders,
        trades_history,
        query_trades,
        open_positions,
        ledgers,
        query_ledgers,
        trade_volume,
        add_export,
        export_status,
        retrieve_export,
        remove_export,
        add_order,
        cancel_order,
        cancel_all,
        cancel_all_after,
        deposit_methods,
        deposit_addresses,
        deposit_status,
        withdraw_info,
        withdraw,
        withdraw_status,
        withdraw_cancel,
        wallet_transfer,
        get_websockets_token,
    }
}

pub trait BaseClient {}

#[derive(Debug, Default)]
pub struct PublicClient {
    pub(crate) http: reqwest::Client,
    pub(crate) api_params: ApiParams,
}

impl BaseClient for PublicClient {}

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

    pub async fn public_api_request(
        &self,
        endpoint: &'static str,
        body: String,
    ) -> reqwest::Result<String> {
        api_request(
            &self.http,
            &self.api_params,
            Endpoint::Public(endpoint),
            &body,
        )
        .await
    }
}

pub struct PrivateClient {
    pub(crate) http: reqwest::Client,
    pub(crate) api_params: ApiParams,
    pub(crate) api_credentials: ApiCredentials,
}

impl BaseClient for PrivateClient {}

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

    pub async fn public_api_request(
        &self,
        endpoint: &'static str,
        body: String,
    ) -> reqwest::Result<String> {
        api_request(
            &self.http,
            &self.api_params,
            Endpoint::Public(endpoint),
            &body,
        )
        .await
    }

    pub async fn private_api_request(
        &self,
        endpoint: &'static str,
        body: String,
    ) -> reqwest::Result<String> {
        api_request(
            &self.http,
            &self.api_params,
            Endpoint::Private(endpoint, self.api_credentials.clone()),
            &body,
        )
        .await
    }
}