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
//! Crypto-bank exchanges API clients.
#![feature(async_closure)]

extern crate cxmr_api;
extern crate cxmr_api_binance as binance;
extern crate cxmr_api_clients_errors;
extern crate cxmr_api_poloniex as poloniex;
extern crate cxmr_http_client;

mod helpers;

pub use self::helpers::*;

use std::sync::Arc;

use cxmr_api::{Account, SharedPrivateClient, SharedPublicClient};
pub use cxmr_api_clients_errors::Error;
use cxmr_exchanges::Exchange;
use cxmr_http_client::HttpsClient;

/// Creates new public clients for all exchanges.
pub fn new_public_clients(
    client: HttpsClient,
    exchanges: &Vec<Exchange>,
) -> Result<Vec<SharedPublicClient<Error>>, Error> {
    exchanges
        .iter()
        .map(|exchange| new_public_client(client.clone(), exchange.clone()))
        .collect::<Result<Vec<SharedPublicClient<Error>>, _>>()
}

/// Creates new exchange private API client with credentials.
pub fn new_public_client(
    client: HttpsClient,
    exchange: Exchange,
) -> Result<SharedPublicClient<Error>, Error> {
    match exchange {
        Exchange::Binance => Ok(Arc::new(binance::PublicClient::new(client))),
        Exchange::Poloniex => Ok(Arc::new(poloniex::PublicClient::new(client))),
        _ => Err(Error::ExchangeNotSupported(exchange)),
    }
}

/// Creates new exchange private API client with credentials.
pub fn new_private_client(
    client: HttpsClient,
    account: Account,
) -> Result<SharedPrivateClient<Error>, Error> {
    match account.exchange {
        Exchange::Binance => Ok(Arc::new(binance::PrivateClient::new(account, client))),
        _ => Err(Error::ExchangeNotSupported(account.exchange)),
    }
}

/// Creates new private clients for all accounts.
pub fn new_private_clients(
    client: HttpsClient,
    accounts: &Vec<Account>,
) -> Result<Vec<SharedPrivateClient<Error>>, Error> {
    accounts
        .iter()
        .map(|account| new_private_client(client.clone(), account.clone()))
        .collect::<Result<Vec<SharedPrivateClient<Error>>, _>>()
}