Skip to main content

trading_ig/operations/
api.rs

1//! Operations API endpoints — manage API application keys.
2
3use http::Method;
4use tracing::instrument;
5
6use crate::Result;
7use crate::client::IgClient;
8
9use super::models::{Application, UpdateApplicationRequest};
10
11/// Typed accessor for the `/operations/application` endpoints.
12///
13/// Obtain via [`IgClient::operations`].
14#[derive(Debug)]
15pub struct OperationsApi<'a> {
16    pub(crate) client: &'a IgClient,
17}
18
19impl OperationsApi<'_> {
20    /// List all API application keys associated with the current account.
21    ///
22    /// # Errors
23    ///
24    /// Returns [`crate::Error::Api`] with
25    /// `error.public-api.failure.not.an.administrator` if the account does
26    /// not have administrator privileges.
27    #[instrument(skip(self))]
28    pub async fn applications(&self) -> Result<Vec<Application>> {
29        self.client
30            .transport
31            .request(
32                Method::GET,
33                "operations/application",
34                Some(1),
35                None::<&()>,
36                &self.client.session,
37            )
38            .await
39    }
40
41    /// Update allowances or status for an API application key.
42    ///
43    /// # Errors
44    ///
45    /// Returns [`crate::Error::Api`] with
46    /// `error.public-api.failure.not.an.administrator` if the account does
47    /// not have administrator privileges.
48    #[instrument(skip(self, req), fields(api_key = %req.api_key))]
49    pub async fn update_application(&self, req: UpdateApplicationRequest) -> Result<Application> {
50        self.client
51            .transport
52            .request(
53                Method::PUT,
54                "operations/application",
55                Some(1),
56                Some(&req),
57                &self.client.session,
58            )
59            .await
60    }
61
62    /// Disable the API key currently in use.
63    ///
64    /// Re-enabling the key requires the IG web UI. Use with care.
65    ///
66    /// # Errors
67    ///
68    /// Returns [`crate::Error::Api`] with
69    /// `error.public-api.failure.not.an.administrator` if the account does
70    /// not have administrator privileges.
71    #[instrument(skip(self))]
72    pub async fn disable_current_key(&self) -> Result<Application> {
73        self.client
74            .transport
75            .request(
76                Method::PUT,
77                "operations/application/disable",
78                Some(1),
79                None::<&()>,
80                &self.client.session,
81            )
82            .await
83    }
84}