ib 1.0.0

A Rust client to the Interactive Brokers HTTP REST API
Documentation
/* 
 * Client Portal Web API
 *
 * Production version of the Client Portal Web API
 *
 * OpenAPI spec version: 1.0.0
 * 
 * Generated by: https://github.com/swagger-api/swagger-codegen.git
 */

use std::rc::Rc;
use std::borrow::Borrow;

use hyper;
use serde_json;
use futures;
use futures::{Future, Stream};

use super::{Error, configuration};

pub struct MarketDataApiClient<C: hyper::client::Connect> {
    configuration: Rc<configuration::Configuration<C>>,
}

impl<C: hyper::client::Connect> MarketDataApiClient<C> {
    pub fn new(configuration: Rc<configuration::Configuration<C>>) -> MarketDataApiClient<C> {
        MarketDataApiClient {
            configuration: configuration,
        }
    }
}

pub trait MarketDataApi {
    fn iserver_marketdata_history_get(&self, conid: &str, period: &str, bar: &str) -> Box<Future<Item = ::models::HistoryData, Error = Error>>;
    fn iserver_marketdata_snapshot_get(&self, conids: &str, since: i32, fields: &str) -> Box<Future<Item = Vec<::models::InlineResponse2007>, Error = Error>>;
}


impl<C: hyper::client::Connect>MarketDataApi for MarketDataApiClient<C> {
    fn iserver_marketdata_history_get(&self, conid: &str, period: &str, bar: &str) -> Box<Future<Item = ::models::HistoryData, Error = Error>> {
        let configuration: &configuration::Configuration<C> = self.configuration.borrow();

        let method = hyper::Method::Get;

        let query = ::url::form_urlencoded::Serializer::new(String::new())
            .append_pair("conid", &conid.to_string())
            .append_pair("period", &period.to_string())
            .append_pair("bar", &bar.to_string())
            .finish();
        let uri_str = format!("{}/iserver/marketdata/history{}", configuration.base_path, query);

        let uri = uri_str.parse();
        // TODO(farcaller): handle error
        // if let Err(e) = uri {
        //     return Box::new(futures::future::err(e));
        // }
        let mut req = hyper::Request::new(method, uri.unwrap());



        // send request
        Box::new(
            configuration.client.request(req).and_then(|res| { res.body().concat2() })
            .map_err(|e| Error::from(e))
            .and_then(|body| {
                let parsed: Result<::models::HistoryData, _> = serde_json::from_slice(&body);
                parsed.map_err(|e| Error::from(e))
            }).map_err(|e| Error::from(e))
        )
    }

    fn iserver_marketdata_snapshot_get(&self, conids: &str, since: i32, fields: &str) -> Box<Future<Item = Vec<::models::InlineResponse2007>, Error = Error>> {
        let configuration: &configuration::Configuration<C> = self.configuration.borrow();

        let method = hyper::Method::Get;

        let query = ::url::form_urlencoded::Serializer::new(String::new())
            .append_pair("conids", &conids.to_string())
            .append_pair("since", &since.to_string())
            .append_pair("fields", &fields.to_string())
            .finish();
        let uri_str = format!("{}/iserver/marketdata/snapshot{}", configuration.base_path, query);

        let uri = uri_str.parse();
        // TODO(farcaller): handle error
        // if let Err(e) = uri {
        //     return Box::new(futures::future::err(e));
        // }
        let mut req = hyper::Request::new(method, uri.unwrap());



        // send request
        Box::new(
            configuration.client.request(req).and_then(|res| { res.body().concat2() })
            .map_err(|e| Error::from(e))
            .and_then(|body| {
                let parsed: Result<Vec<::models::InlineResponse2007>, _> = serde_json::from_slice(&body);
                parsed.map_err(|e| Error::from(e))
            }).map_err(|e| Error::from(e))
        )
    }

}