endpoints/
endpoints.rs

1extern crate fxoanda;
2use fxoanda::*;
3use std::env;
4
5#[tokio::main]
6async fn main() {
7    let api_key = env::var("OANDA_KEY").expect("expected OANDA_KEY environment variable to be set");
8    let api_host = env
9        ::var("OANDA_HOST")
10        .expect("expected OANDA_HOST environment variable to be set");
11
12    // only run example program against demo account!!
13    assert_eq!(api_host, "api-fxpractice.oanda.com");
14
15    let client = fxoanda::Client {
16        host: String::from(api_host),
17        reqwest: reqwest::Client::new(),
18        authentication: String::from(api_key),
19    };
20
21    match
22        fxoanda::GetInstrumentCandlesRequest
23            ::new()
24            .with_instrument("EUR_USD".to_string())
25            .with_granularity(CandlestickGranularity::H4)
26            .remote(&client).await
27    {
28        Ok(x) => println!("OK: {:#?}", x),
29        Err(e) => eprintln!("ERR: {:#?}", e),
30    }
31
32    match
33        fxoanda::GetPositionBookRequest
34            ::new()
35            .with_instrument("EUR_USD".to_string())
36            .remote(&client).await
37    {
38        Ok(x) => println!("OK: {:#?}", x),
39        Err(e) => println!("ERR: {:#?}", e),
40    }
41
42    match
43        fxoanda::GetOrderBookRequest
44            ::new()
45            .with_instrument("EUR_USD".to_string())
46            .remote(&client).await
47    {
48        Ok(x) => println!("OK: {:#?}", x),
49        Err(e) => println!("ERR: {:#?}", e),
50    }
51
52    match fxoanda::ListAccountsRequest::new().remote(&client).await {
53        Ok(x) => println!("OK: {:#?}", x),
54        Err(e) => println!("ERR: {:#?}", e),
55    }
56
57    let response = fxoanda::ListAccountsRequest::new().remote(&client).await.unwrap();
58    let accounts = response.accounts.expect("Did not find 'accounts' field in response");
59    let account = accounts.get(0).expect("Did not find an 'account' in the 'accounts' field");
60    let account_id = account.id
61        .as_ref()
62        .expect("Did not find an 'id' field in the 'account'")
63        .to_string();
64
65    match
66        fxoanda::GetAccountSummaryRequest
67            ::new()
68            .with_account_id(account_id.to_owned())
69            .remote(&client).await
70    {
71        Ok(x) => println!("OK: {:#?}", x),
72        Err(e) => println!("ERR: {:#?}", e),
73    }
74
75    match
76        fxoanda::GetAccountRequest
77            ::new()
78            .with_account_id(account_id.to_owned())
79            .remote(&client).await
80    {
81        Ok(x) => println!("OK: {:#?}", x),
82        Err(e) => println!("ERR: {:#?}", e),
83    }
84
85    match
86        fxoanda::GetAccountInstrumentsRequest
87            ::new()
88            .with_account_id(account_id.to_owned())
89            .remote(&client).await
90    {
91        Ok(x) => println!("OK: {:#?}", x),
92        Err(e) => println!("ERR: {:#?}", e),
93    }
94
95    match
96        fxoanda::ListOrdersRequest
97            ::new()
98            .with_account_id(account_id.to_owned())
99            .remote(&client).await
100    {
101        Ok(x) => println!("OK: {:#?}", x),
102        Err(e) => println!("ERR: {:#?}", e),
103    }
104
105    match
106        fxoanda::ListPendingOrdersRequest
107            ::new()
108            .with_account_id(account_id.to_owned())
109            .remote(&client).await
110    {
111        Ok(x) => println!("OK: {:#?}", x),
112        Err(e) => println!("ERR: {:#?}", e),
113    }
114
115    match
116        fxoanda::ListTradesRequest
117            ::new()
118            .with_account_id(account_id.to_owned())
119            .remote(&client).await
120    {
121        Ok(x) => println!("OK: {:#?}", x),
122        Err(e) => println!("ERR: {:#?}", e),
123    }
124
125    match
126        fxoanda::ListOpenTradesRequest
127            ::new()
128            .with_account_id(account_id.to_owned())
129            .remote(&client).await
130    {
131        Ok(x) => println!("OK: {:#?}", x),
132        Err(e) => println!("ERR: {:#?}", e),
133    }
134
135    match
136        fxoanda::ListPositionsRequest
137            ::new()
138            .with_account_id(account_id.to_owned())
139            .remote(&client).await
140    {
141        Ok(x) => println!("OK: {:#?}", x),
142        Err(e) => println!("ERR: {:#?}", e),
143    }
144
145    match
146        fxoanda::ListOpenPositionsRequest
147            ::new()
148            .with_account_id(account_id.to_owned())
149            .remote(&client).await
150    {
151        Ok(x) => println!("OK: {:#?}", x),
152        Err(e) => println!("ERR: {:#?}", e),
153    }
154
155    match
156        fxoanda::ListTransactionsRequest
157            ::new()
158            .with_account_id(account_id.to_owned())
159            .remote(&client).await
160    {
161        Ok(x) => println!("OK: {:#?}", x),
162        Err(e) => println!("ERR: {:#?}", e),
163    }
164}