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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
use crate::common::*;
use crate::rest_client::*;
use crate::rest_models::*;
use serde_json::Value;
use std::error::Error;
/// Struct to encapsulate the API, including the REST HTTP client, the API configuration
/// and all the methods to interact with the IG REST API.
#[derive(Clone, Debug)]
pub struct RestApi {
/// The HTTP client for making requests.
pub client: RestClient,
/// The API configuration.
pub config: ApiConfig,
}
/// Provide an implementation for the Api struct with all the methods to interact with the IG REST API.
impl RestApi {
/// Create a new instance of the RestApi struct based on the provided configuration.
pub async fn new(config: ApiConfig) -> Result<Self, Box<dyn Error>> {
Ok(Self {
client: RestClient::new(config.clone()).await?,
config,
})
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// ACCOUNT METHODS.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Returns a list of the logged-in client's accounts.
pub async fn accounts_get(&self) -> Result<(Value, AccountsGetResponse), Box<dyn Error>> {
// Send the request to the REST client.
let (header_map, response_value) = self
.client
.get("accounts".to_string(), Some(1), &None::<Empty>)
.await?;
// Convert header_map to json.
let headers: Value = headers_to_json(&header_map)?;
// Convert the serde_json::Value response to Session model.
let accounts = AccountsGetResponse::from_value(&response_value)?;
Ok((headers, accounts))
}
/// Returns account preferences.
pub async fn accounts_preferences_get(
&self,
) -> Result<(Value, AccountsPreferencesGetResponse), Box<dyn Error>> {
// Send the request to the REST client.
let (header_map, response_value) = self
.client
.get("accounts/preferences".to_string(), Some(1), &None::<Empty>)
.await?;
// Convert header_map to json.
let headers: Value = headers_to_json(&header_map)?;
// Convert the serde_json::Value response to Session model.
let accounts_preferences = AccountsPreferencesGetResponse::from_value(&response_value)?;
Ok((headers, accounts_preferences))
}
/// Updates account preferences.
pub async fn accounts_preferences_put(
&self,
body: &AccountsPreferencesPutRequest,
) -> Result<(Value, AccountsPreferencesStatusPutResponse), Box<dyn Error>> {
// Validate the body.
body.validate()?;
// Send the request to the REST client.
let (header_map, response_value) = self
.client
.put("accounts/preferences".to_string(), Some(1), body)
.await?;
// Convert header_map to json.
let headers: Value = headers_to_json(&header_map)?;
// Convert the serde_json::Value response to Session model.
let status = AccountsPreferencesStatusPutResponse::from_value(&response_value)?;
Ok((headers, status))
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// CONFIRMS METHODS.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Returns a deal confirmation for the given deal reference. Please note, this
/// should only be used if the deal confirmation isn't received via the streaming API.
pub async fn confirms_get(
&self,
params: ConfirmsGetRequest,
) -> Result<(Value, ConfirmsGetResponse), Box<dyn Error>> {
let url = format!("confirms/{}", params.deal_reference);
// Send the request to the REST client.
let (header_map, response_value) = self.client.get(url, Some(1), &None::<Empty>).await?;
// Convert header_map to json.
let headers: Value = headers_to_json(&header_map)?;
// Convert the serde_json::Value response to Session model.
let confirmations = ConfirmsGetResponse::from_value(&response_value)?;
Ok((headers, confirmations))
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// HISTORY METHODS.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Returns the account activity history.
pub async fn history_activity_get(
&self,
params: ActivityHistoryGetRequest,
) -> Result<(Value, ActivityHistoryGetResponse), Box<dyn Error>> {
// Send the request to the REST client.
let (header_map, response_value) = self
.client
.get("history/activity".to_string(), Some(3), &Some(params))
.await?;
// Convert header_map to json.
let headers: Value = headers_to_json(&header_map)?;
// Convert the serde_json::Value response to Session model.
let history_activity = ActivityHistoryGetResponse::from_value(&response_value)?;
Ok((headers, history_activity))
}
/// Returns the transaction history. Returns the minute prices within the last 10 minutes by default.
pub async fn history_transactions_get(
&self,
params: TransactionHistoryGetRequest,
) -> Result<(Value, TransactionHistoryGetResponse), Box<dyn Error>> {
// Send the request to the REST client.
let (header_map, response_value) = self
.client
.get("history/transactions".to_string(), Some(2), &Some(params))
.await?;
// Convert header_map to json.
let headers: Value = headers_to_json(&header_map)?;
// Convert the serde_json::Value response to Session model.
let history_activity = TransactionHistoryGetResponse::from_value(&response_value)?;
Ok((headers, history_activity))
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// MARKETS METHODS.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Returns all top-level nodes (market categories) in the market navigation hierarchy if no
/// node is specified. Returns the specified node's children if a node is specified.
pub async fn marketnavigation_get(
&self,
node_id: Option<String>,
) -> Result<(Value, MarketNavigationGetResponse), Box<dyn Error>> {
let url = match node_id {
Some(node_id) => format!("marketnavigation/{}", node_id),
None => "marketnavigation".to_string(),
};
// Send the request to the REST client.
let (header_map, response_value) = self.client.get(url, Some(1), &None::<Empty>).await?;
// Convert header_map to json.
let headers: Value = headers_to_json(&header_map)?;
// Convert the serde_json::Value response to MarketsGetResponse model.
let markets = MarketNavigationGetResponse::from_value(&response_value)?;
Ok((headers, markets))
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// POSITIONS METHODS.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Returns a specific open position for the active account.
pub async fn position_delete(
&self,
body: PositionDeleteRequest,
) -> Result<(Value, PositionDeleteResponse), Box<dyn Error>> {
// Send the request to the REST client.
let (header_map, response_value) = self
.client
.delete("positions/otc".to_string(), Some(1), &Some(body))
.await?;
// Convert header_map to json.
let headers: Value = headers_to_json(&header_map)?;
// Convert the serde_json::Value response to Session model.
let position_delete_response = PositionDeleteResponse::from_value(&response_value)?;
Ok((headers, position_delete_response))
}
/// Returns a specific open position for the active account.
pub async fn position_get(
&self,
params: PositionGetRequest,
) -> Result<(Value, PositionGetResponse), Box<dyn Error>> {
// Create the url based on the params.
let url = format!("positions/{}", params.deal_id);
// Send the request to the REST client.
let (header_map, response_value) = self.client.get(url, Some(2), &None::<Empty>).await?;
// Convert header_map to json.
let headers: Value = headers_to_json(&header_map)?;
// Convert the serde_json::Value response to Session model.
let positions = PositionGetResponse::from_value(&response_value)?;
Ok((headers, positions))
}
/// Returns a specific open position for the active account.
pub async fn position_post(
&self,
body: PositionPostRequest,
) -> Result<(Value, PositionPostResponse), Box<dyn Error>> {
// Send the request to the REST client.
let (header_map, response_value) = self
.client
.post("positions/otc".to_string(), Some(2), &body)
.await?;
// Convert header_map to json.
let headers: Value = headers_to_json(&header_map)?;
// Convert the serde_json::Value response to Session model.
let position_post_response = PositionPostResponse::from_value(&response_value)?;
Ok((headers, position_post_response))
}
/// Updates a specific open position for the active account.
pub async fn position_put(
&self,
body: PositionPutRequest,
deal_id: String,
) -> Result<(Value, PositionPutResponse), Box<dyn Error>> {
let url = format!("positions/otc/{}", deal_id);
// Send the request to the REST client.
let (header_map, response_value) = self.client.put(url, Some(2), &body).await?;
// Convert header_map to json.
let headers: Value = headers_to_json(&header_map)?;
// Convert the serde_json::Value response to Session model.
let position_post_response = PositionPutResponse::from_value(&response_value)?;
Ok((headers, position_post_response))
}
/// Returns all open positions for the active account.
pub async fn positions_get(&self) -> Result<(Value, PositionsGetResponse), Box<dyn Error>> {
// Send the request to the REST client.
let (header_map, response_value) = self
.client
.get("positions".to_string(), Some(2), &None::<Empty>)
.await?;
// Convert header_map to json.
let headers: Value = headers_to_json(&header_map)?;
// Convert the serde_json::Value response to Session model.
let positions = PositionsGetResponse::from_value(&response_value)?;
Ok((headers, positions))
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// SPRINT MARKET METHODS.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Returns all open sprint market positions for the active account.
pub async fn positions_sprintmarkets_get(
&self,
) -> Result<(Value, SprintMarketPositionsGetResponse), Box<dyn Error>> {
// Send the request to the REST client.
let (header_map, response_value) = self
.client
.get(
"positions/sprintmarkets".to_string(),
Some(2),
&None::<Empty>,
)
.await?;
// Convert header_map to json.
let headers: Value = headers_to_json(&header_map)?;
// Convert the serde_json::Value response to Session model.
let positions = SprintMarketPositionsGetResponse::from_value(&response_value)?;
Ok((headers, positions))
}
/// Creates a sprint market position.
pub async fn positions_sprintmarkets_post(
&self,
body: SprintMarketPositionsPostRequest,
) -> Result<(Value, SprintMarketPositionsPostResponse), Box<dyn Error>> {
// Send the request to the REST client.
let (header_map, response_value) = self
.client
.delete("positions/sprintmarkets".to_string(), Some(1), &Some(body))
.await?;
// Convert header_map to json.
let headers: Value = headers_to_json(&header_map)?;
// Convert the serde_json::Value response to Session model.
let position_post_response =
SprintMarketPositionsPostResponse::from_value(&response_value)?;
Ok((headers, position_post_response))
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// SESSION METHODS.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Log out of the IG API by deleting the current session.
pub async fn session_delete(&self) -> Result<(Value, ()), Box<dyn Error>> {
// Send the request to the REST client.
let (header_map, _) = self
.client
.delete("session".to_string(), Some(1), &None::<Empty>)
.await?;
// Convert header_map to json.
let headers: Value = headers_to_json(&header_map)?;
Ok((headers, ()))
}
/// Get session details for the current session.
pub async fn session_get(
&self,
params: Option<SessionDetailsGetRequest>,
) -> Result<(Value, SessionDetailsGetResponse), Box<dyn Error>> {
// Send the request to the REST client.
let (header_map, response_value) = self
.client
.get("session".to_string(), Some(1), ¶ms)
.await?;
// Convert header_map to json.
let headers: Value = headers_to_json(&header_map)?;
// Convert the serde_json::Value response to Session model.
let session = SessionDetailsGetResponse::from_value(&response_value)?;
Ok((headers, session))
}
/// This method is not implemented as the login process is handled by the rest_client module.
pub async fn session_post() {
unimplemented!("This method will not be implemented as the login process is handled by the rest_client module.");
}
/// Switch to a different account by updating the current session.
pub async fn session_put(
&self,
body: &AccountSwitchPutRequest,
) -> Result<(Value, AccountSwitchPutResponse), Box<dyn Error>> {
// Validate the body.
body.validate()?;
// Send the request to the REST client.
let (header_map, response_value) = self
.client
.put("session".to_string(), Some(1), body)
.await?;
// Convert header_map to json.
let headers: Value = headers_to_json(&header_map)?;
// Deserialize the response_value to AccountSwitchResponse.
let account_switch_response = AccountSwitchPutResponse::from_value(&response_value)?;
Ok((headers, account_switch_response))
}
/// Creates a trading session, obtaining session tokens for subsequent API access.
/// Please note, region-specific login restrictions may apply.
pub async fn session_encryption_key_get(
&self,
) -> Result<(Value, SessionEncryptionKeyGetResponse), Box<dyn Error>> {
// Send the request to the REST client.
let (header_map, response_value) = self
.client
.get("session/encryptionKey".to_string(), Some(1), &None::<Empty>)
.await?;
// Convert header_map to json.
let headers: Value = headers_to_json(&header_map)?;
// Deserialize the response_value to EncryptionKeyResponse.
let encryption_key_response = SessionEncryptionKeyGetResponse::from_value(&response_value)?;
Ok((headers, encryption_key_response))
}
/// Refresh the current session, obtaining new session tokens for subsequent API access.
pub async fn session_refresh_token_post(
&self,
body: &SessionRefreshTokenPostRequest,
) -> Result<(Value, SessionRefreshTokenPostResponse), Box<dyn Error>> {
// Validate the body.
body.validate()?;
// Send the request to the REST client.
let (headers, response_value) = self
.client
.post("session/refresh-token".to_string(), Some(1), body)
.await?;
// Convert header_map to json.
let headers: Value = headers_to_json(&headers)?;
// Deserialize the response_value to SessionRefreshTokenResponse.
let session_refresh_token_response =
SessionRefreshTokenPostResponse::from_value(&response_value)?;
Ok((headers, session_refresh_token_response))
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// WORKINGORDERS ENDPOINTS.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////
/// Deletes a working order for the active account.
pub async fn workingorders_delete(
&self,
deal_id: String,
) -> Result<(Value, WorkingOrderDeleteResponse), Box<dyn Error>> {
let params = WorkingOrderDeleteRequest {
deal_id: deal_id.clone(),
};
// Validate the params.
params.validate()?;
let url = format!("workingorders/otc/{}", params.deal_id);
// Send the request to the REST client.
let (header_map, response_value) = self.client.delete(url, Some(2), &None::<Empty>).await?;
// Convert header_map to json.
let headers: Value = headers_to_json(&header_map)?;
// Deserialize the response_value to WorkingOrdersDeleteResponse.
let working_orders = WorkingOrderDeleteResponse::from_value(&response_value)?;
Ok((headers, working_orders))
}
/// Get list of working orders.
pub async fn workingorders_get(
&self,
) -> Result<(Value, WorkingOrdersGetResponse), Box<dyn Error>> {
// Send the request to the REST client.
let (header_map, response_value) = self
.client
.get("workingorders".to_string(), Some(2), &None::<Empty>)
.await?;
// Convert header_map to json.
let headers: Value = headers_to_json(&header_map)?;
// Deserialize the response_value to WorkingOrdersGetResponse.
let working_orders = WorkingOrdersGetResponse::from_value(&response_value)?;
Ok((headers, working_orders))
}
/// Create a new working order.
pub async fn workingorders_post(
&self,
body: &WorkingOrderPostRequest,
) -> Result<(Value, WorkingOrderPostResponse), Box<dyn Error>> {
// Validate the body.
body.validate()?;
// Send the request to the REST client.
let (header_map, response_value) = self
.client
.post("workingorders/otc".to_string(), Some(2), body)
.await?;
// Convert header_map to json.
let headers: Value = headers_to_json(&header_map)?;
// Deserialize the response_value to WorkingOrdersPostResponse.
let working_orders = WorkingOrderPostResponse::from_value(&response_value)?;
Ok((headers, working_orders))
}
/// Update a working order for the active account.
pub async fn workingorders_put(
&self,
body: &WorkingOrderPutRequest,
deal_id: String,
) -> Result<(Value, WorkingOrderPutResponse), Box<dyn Error>> {
// Validate the body.
body.validate()?;
let url = format!("workingorders/otc/{}", deal_id);
// Send the request to the REST client.
let (header_map, response_value) = self.client.put(url, Some(2), body).await?;
// Convert header_map to json.
let headers: Value = headers_to_json(&header_map)?;
// Deserialize the response_value to WorkingOrdersPutResponse.
let working_orders = WorkingOrderPutResponse::from_value(&response_value)?;
Ok((headers, working_orders))
}
}