ctrader-connector 0.3.10

Client library for the cTrader Open API and the cTrader FIX 4.4 API
Documentation
//! Connects to the Open API and authorises the application and the first account of the
//! configured environment, a demo one unless the guard permits another.
//!
//! The steps are spelled out here; the other examples, `open_api_token_refresh` apart, take
//! them from the shared `session` helper. The configuration comes through the guard of the
//! examples, which refuses anything
//! but the demo environment on its default host before the connection is opened unless
//! `BROKER_EXAMPLE_ALLOW_LIVE` is `read-only` or `yes`. Needs `BROKER_CLIENT_ID`,
//! `BROKER_CLIENT_SECRET` and `BROKER_ACCESS_TOKEN`.
//!
//! ```sh
//! cargo run --example open_api_connect --features open-api
//! ```

#[path = "../support/mod.rs"]
mod support;

use ctrader_connector::common::config::Environment;

use support::guard::Access;
use support::open_api::{configuration, open};
use support::{Failure, veil};

#[tokio::main]
async fn main() -> Result<(), Failure> {
    let checked = configuration(Access::ReadOnly)?;
    let environment = checked.get().environment();
    let client = open(&checked).await?;
    println!("connected to the {environment} environment");
    let version = client.version().await?;
    println!("the server speaks version {version} of the protocol");
    client.authorise_application().await?;
    println!("application authorised");
    let accounts = client.accounts().await?;
    for account in &accounts {
        veil(account.id);
    }
    let live = environment == Environment::Live;
    let own: Vec<_> = accounts
        .iter()
        .filter(|account| account.is_live == live)
        .collect();
    println!(
        "the access token grants access to {} accounts, {} of them {environment}",
        accounts.len(),
        own.len()
    );
    let Some(account) = own.first() else {
        client.shutdown().await;
        return Err(Failure::new(format!(
            "the access token grants access to no {environment} account"
        )));
    };
    client.authorise_account(account.id).await?;
    println!("{environment} account authorised");
    client.shutdown().await;
    println!("connection shut down");
    Ok(())
}