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
//! Crypto-bank RPC service HTTP client.

extern crate serde_json;

extern crate async_trait;
extern crate cxmr_api_clients_errors;
extern crate cxmr_exchanges;
extern crate cxmr_http_client;
extern crate cxmr_orderbook;
extern crate cxmr_rpc;

use async_trait::async_trait;
pub use cxmr_api_clients_errors::Error;
use cxmr_exchanges::Market;
use cxmr_http_client::{self as http, BufExt, HttpsClient};
use cxmr_orderbook::OrderBook;
use cxmr_rpc::Service;

/// Crypto market HTTP service client implementation.
#[derive(Clone)]
pub struct Client {
    client: HttpsClient,
    host: String,
}

impl Client {
    /// Creates new evolve service HTTP client.
    pub fn new(client: HttpsClient, host: String) -> Self {
        Client { client, host }
    }

    /// Creates new evolve service HTTP client.
    /// Default HTTPS client is created in-place.
    pub fn new_default(host: String) -> Result<Self, Error> {
        let client = http::build_https_client()?;
        Ok(Client { client, host })
    }
}

#[async_trait]
impl Service for Client {
    type Error = Error;

    async fn orderbooks(&self) -> Result<Vec<(Market, OrderBook)>, Self::Error> {
        let req = http::build_unsecured_request(&self.host, "/v1/broker/orderbooks", None)?;
        let body = http::send_request(&self.client, req).await?;
        Ok(serde_json::from_reader(body.reader())?)
    }
}