Skip to main content

ctrader_rs/
auth.rs

1///
2///
3///
4///
5///
6///
7///
8///
9///
10///
11///
12///
13///
14///
15///
16use crate::payload;
17use crate::proto::common::*;
18use crate::{client::Client, error::Error};
19
20impl Client {
21    /// Application auth
22    ///
23    ///
24    ///
25    ///
26    ///
27    ///
28    ///
29    ///
30    ///
31    ///
32    pub async fn application_auth(&self) -> Result<(), Error> {
33        let req = ProtoOaApplicationAuthReq {
34            payload_type: Some(payload::OA_APPLICATION_AUTH_REQ as i32),
35            client_id: self.config.client_id.clone(),
36            client_secret: self.config.client_secret.clone(),
37        };
38        let _: ProtoOaApplicationAuthRes = self
39            .command(
40                payload::OA_APPLICATION_AUTH_REQ,
41                req,
42                payload::OA_APPLICATION_AUTH_RES,
43            )
44            .await?;
45        Ok(())
46    }
47
48    /// Authenticate a trader account.  Must be called before any account-scoped
49    /// request (symbols, deals, orders, …).
50    ///
51    ///
52    ///
53    ///
54    ///
55    ///
56    ///
57    ///
58    ///
59    /// Mirrors `ProtoOAAccountAuthReq` in the Go integration test.
60    pub async fn account_auth(
61        &self,
62        ctid_trader_account_id: i64,
63        access_token: &str,
64    ) -> Result<ProtoOaAccountAuthRes, Error> {
65        let req = ProtoOaAccountAuthReq {
66            payload_type: Some(payload::OA_ACCOUNT_AUTH_REQ as i32),
67            ctid_trader_account_id,
68            access_token: access_token.to_string(),
69        };
70        self.command(
71            payload::OA_ACCOUNT_AUTH_REQ,
72            req,
73            payload::OA_ACCOUNT_AUTH_RES,
74        )
75        .await
76    }
77
78    /// Refresh an expired access token.
79    ///
80    ///
81    ///
82    ///
83    ///
84    ///
85    ///
86    ///
87    ///
88    pub async fn refresh_token(
89        &self,
90        refresh_token: &str,
91    ) -> Result<ProtoOaRefreshTokenRes, Error> {
92        let req = ProtoOaRefreshTokenReq {
93            payload_type: Some(payload::OA_REFRESH_TOKEN_REQ as i32),
94            refresh_token: refresh_token.to_string(),
95        };
96        self.command(
97            payload::OA_REFRESH_TOKEN_REQ,
98            req,
99            payload::OA_REFRESH_TOKEN_RES,
100        )
101        .await
102    }
103}
104
105#[cfg(test)]
106mod tests {
107
108    #[async_std::test]
109    async fn test() {}
110}