ib-flex 0.1.7

Pure Rust parser for Interactive Brokers FLEX XML statements
Documentation
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
//! FLEX Web Service API client implementation

use reqwest::Client;
use std::time::Duration;
use thiserror::Error;

/// Base URL for IB FLEX Web Service API
const FLEX_BASE_URL: &str = "https://gdcdyn.interactivebrokers.com/Universal/servlet";

/// FLEX Web Service API errors
#[derive(Debug, Error)]
pub enum FlexApiError {
    /// HTTP request failed
    #[error("HTTP request failed: {0}")]
    RequestFailed(#[from] reqwest::Error),

    /// IB API returned an error
    #[error("IB API error: {0}")]
    ApiError(String),

    /// XML parsing error
    #[error("XML parsing error: {0}")]
    XmlError(String),

    /// Statement not ready yet
    #[error("Statement not ready (try again later)")]
    StatementNotReady,

    /// Invalid response format
    #[error("Invalid response format: {0}")]
    InvalidResponse(String),
}

/// Result type for FLEX API operations
pub type Result<T> = std::result::Result<T, FlexApiError>;

/// FLEX Web Service API client
///
/// Provides async programmatic access to Interactive Brokers FLEX statements
/// using the FLEX Web Service API.
///
/// # Authentication
///
/// Requires a FLEX Web Service token from IB Account Management:
/// 1. Log in to IB Account Management
/// 2. Navigate to: Reports → Settings → FlexWeb Service
/// 3. Generate a token (keep it secure!)
///
/// # Example
///
/// ```rust,no_run
/// use ib_flex::api::FlexApiClient;
/// use std::time::Duration;
///
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = FlexApiClient::new("YOUR_TOKEN");
///
/// // Send request
/// let ref_code = client.send_request("QUERY_ID").await?;
///
/// // Wait for report generation
/// tokio::time::sleep(Duration::from_secs(5)).await;
///
/// // Get statement
/// let xml = client.get_statement(&ref_code).await?;
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone)]
pub struct FlexApiClient {
    /// FLEX Web Service token
    token: String,
    /// Base URL for API endpoints
    base_url: String,
    /// HTTP client
    client: Client,
}

impl FlexApiClient {
    /// Create a new FLEX API client with the given token
    ///
    /// # Arguments
    ///
    /// * `token` - Your FLEX Web Service token from IB Account Management
    ///
    /// # Example
    ///
    /// ```rust
    /// use ib_flex::api::FlexApiClient;
    ///
    /// let client = FlexApiClient::new("YOUR_TOKEN_HERE");
    /// ```
    pub fn new(token: impl Into<String>) -> Self {
        Self {
            token: token.into(),
            base_url: FLEX_BASE_URL.to_string(),
            client: Client::builder()
                .timeout(Duration::from_secs(30))
                .build()
                .expect("Failed to build HTTP client"),
        }
    }

    /// Create a client with a custom base URL (for testing)
    ///
    /// # Arguments
    ///
    /// * `token` - Your FLEX Web Service token
    /// * `base_url` - Custom base URL for the API
    pub fn with_base_url(token: impl Into<String>, base_url: impl Into<String>) -> Self {
        Self {
            token: token.into(),
            base_url: base_url.into(),
            client: Client::builder()
                .timeout(Duration::from_secs(30))
                .build()
                .expect("Failed to build HTTP client"),
        }
    }

    /// Send a FLEX query request
    ///
    /// Initiates a FLEX query execution on IB servers. Returns a reference code
    /// that can be used to retrieve the generated statement.
    ///
    /// # Arguments
    ///
    /// * `query_id` - Your FLEX query ID from IB Account Management
    ///
    /// # Returns
    ///
    /// * `Ok(String)` - Reference code for retrieving the statement
    /// * `Err(FlexApiError)` - If the request fails or IB returns an error
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use ib_flex::api::FlexApiClient;
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let client = FlexApiClient::new("YOUR_TOKEN");
    /// let reference_code = client.send_request("123456").await?;
    /// println!("Reference code: {}", reference_code);
    /// # Ok(())
    /// # }
    /// ```
    pub async fn send_request(&self, query_id: &str) -> Result<String> {
        let url = format!(
            "{}/FlexStatementService.SendRequest?t={}&q={}&v=3",
            self.base_url, self.token, query_id
        );

        let response = self.client.get(&url).send().await?;
        let body = response.text().await?;

        // Parse XML response
        self.parse_send_request_response(&body)
    }

    /// Get a FLEX statement by reference code
    ///
    /// Retrieves the XML statement for a previously submitted query.
    /// The statement must be ready (typically takes a few seconds).
    ///
    /// # Arguments
    ///
    /// * `reference_code` - Reference code from `send_request()`
    ///
    /// # Returns
    ///
    /// * `Ok(String)` - XML statement content
    /// * `Err(FlexApiError::StatementNotReady)` - If statement is not ready yet
    /// * `Err(FlexApiError)` - If the request fails or IB returns an error
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use ib_flex::api::FlexApiClient;
    /// # use std::time::Duration;
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let client = FlexApiClient::new("YOUR_TOKEN");
    /// let ref_code = client.send_request("123456").await?;
    ///
    /// // Wait for statement generation
    /// tokio::time::sleep(Duration::from_secs(5)).await;
    ///
    /// let xml = client.get_statement(&ref_code).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn get_statement(&self, reference_code: &str) -> Result<String> {
        let url = format!(
            "{}/FlexStatementService.GetStatement?t={}&q={}&v=3",
            self.base_url, self.token, reference_code
        );

        let response = self.client.get(&url).send().await?;
        let body = response.text().await?;

        // Check if this is an error response
        if body.contains("<Status>") {
            self.parse_get_statement_response(&body)
        } else {
            // This is the actual XML statement
            Ok(body)
        }
    }

    /// Get a FLEX statement with automatic retry
    ///
    /// Like `get_statement()` but automatically retries if the statement is not ready yet.
    ///
    /// # Arguments
    ///
    /// * `reference_code` - Reference code from `send_request()`
    /// * `max_retries` - Maximum number of retry attempts
    /// * `retry_delay` - Delay between retries
    ///
    /// # Returns
    ///
    /// * `Ok(String)` - XML statement content
    /// * `Err(FlexApiError)` - If max retries exceeded or other error
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// # use ib_flex::api::FlexApiClient;
    /// # use std::time::Duration;
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let client = FlexApiClient::new("YOUR_TOKEN");
    /// let ref_code = client.send_request("123456").await?;
    ///
    /// // Automatically retry up to 10 times with 2-second delays
    /// let xml = client.get_statement_with_retry(&ref_code, 10, Duration::from_secs(2)).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn get_statement_with_retry(
        &self,
        reference_code: &str,
        max_retries: usize,
        retry_delay: Duration,
    ) -> Result<String> {
        // Always make at least one attempt (0..=max_retries ensures this)
        for attempt in 0..=max_retries {
            match self.get_statement(reference_code).await {
                Ok(xml) => return Ok(xml),
                Err(FlexApiError::StatementNotReady) => {
                    if attempt < max_retries {
                        tokio::time::sleep(retry_delay).await;
                        continue;
                    } else {
                        return Err(FlexApiError::StatementNotReady);
                    }
                }
                Err(e) => return Err(e),
            }
        }

        unreachable!("Loop should always return within the iteration")
    }

    /// Parse SendRequest XML response
    ///
    /// Expected format:
    /// ```xml
    /// <FlexStatementResponse timestamp='01 January, 2025 12:00 AM EDT'>
    ///   <Status>Success</Status>
    ///   <ReferenceCode>1234567890</ReferenceCode>
    ///   <Url>https://...</Url>
    /// </FlexStatementResponse>
    /// ```
    fn parse_send_request_response(&self, xml: &str) -> Result<String> {
        // Simple XML parsing - look for ReferenceCode
        if let Some(start) = xml.find("<ReferenceCode>") {
            if let Some(end) = xml[start..].find("</ReferenceCode>") {
                let ref_code = &xml[start + 15..start + end];
                return Ok(ref_code.to_string());
            }
        }

        // Check for error status
        if xml.contains("<Status>Fail</Status>") || xml.contains("<Status>Warn</Status>") {
            if let Some(start) = xml.find("<ErrorMessage>") {
                if let Some(end) = xml[start..].find("</ErrorMessage>") {
                    let error = &xml[start + 14..start + end];
                    return Err(FlexApiError::ApiError(error.to_string()));
                }
            }
            return Err(FlexApiError::ApiError("Unknown error".to_string()));
        }

        Err(FlexApiError::InvalidResponse(
            "Could not parse reference code".to_string(),
        ))
    }

    /// Parse GetStatement status response
    ///
    /// Expected format when not ready:
    /// ```xml
    /// <FlexStatementResponse timestamp='01 January, 2025 12:00 AM EDT'>
    ///   <Status>Warn</Status>
    ///   <ErrorCode>1019</ErrorCode>
    ///   <ErrorMessage>Statement is being generated; please try again shortly</ErrorMessage>
    /// </FlexStatementResponse>
    /// ```
    fn parse_get_statement_response(&self, xml: &str) -> Result<String> {
        // Check for "statement not ready" error (code 1019)
        if xml.contains("<ErrorCode>1019</ErrorCode>") {
            return Err(FlexApiError::StatementNotReady);
        }

        // Check for other errors
        if xml.contains("<Status>Fail</Status>") || xml.contains("<Status>Warn</Status>") {
            if let Some(start) = xml.find("<ErrorMessage>") {
                if let Some(end) = xml[start..].find("</ErrorMessage>") {
                    let error = &xml[start + 14..start + end];
                    return Err(FlexApiError::ApiError(error.to_string()));
                }
            }
            return Err(FlexApiError::ApiError("Unknown error".to_string()));
        }

        Err(FlexApiError::InvalidResponse(
            "Unexpected response format".to_string(),
        ))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_send_request_success() {
        let client = FlexApiClient::new("test_token");
        let xml = r#"
            <FlexStatementResponse timestamp='01 January, 2025 12:00 AM EDT'>
                <Status>Success</Status>
                <ReferenceCode>1234567890</ReferenceCode>
                <Url>https://example.com</Url>
            </FlexStatementResponse>
        "#;

        let result = client.parse_send_request_response(xml);
        assert!(result.is_ok());
        assert_eq!(result.unwrap(), "1234567890");
    }

    #[test]
    fn test_parse_send_request_error() {
        let client = FlexApiClient::new("test_token");
        let xml = r#"
            <FlexStatementResponse timestamp='01 January, 2025 12:00 AM EDT'>
                <Status>Fail</Status>
                <ErrorCode>1003</ErrorCode>
                <ErrorMessage>Invalid token</ErrorMessage>
            </FlexStatementResponse>
        "#;

        let result = client.parse_send_request_response(xml);
        assert!(result.is_err());
        match result {
            Err(FlexApiError::ApiError(msg)) => assert_eq!(msg, "Invalid token"),
            _ => panic!("Expected ApiError"),
        }
    }

    #[test]
    fn test_parse_get_statement_not_ready() {
        let client = FlexApiClient::new("test_token");
        let xml = r#"
            <FlexStatementResponse timestamp='01 January, 2025 12:00 AM EDT'>
                <Status>Warn</Status>
                <ErrorCode>1019</ErrorCode>
                <ErrorMessage>Statement is being generated; please try again shortly</ErrorMessage>
            </FlexStatementResponse>
        "#;

        let result = client.parse_get_statement_response(xml);
        assert!(result.is_err());
        match result {
            Err(FlexApiError::StatementNotReady) => (),
            _ => panic!("Expected StatementNotReady"),
        }
    }

    #[test]
    fn test_client_creation() {
        let client = FlexApiClient::new("my_token");
        assert_eq!(client.token, "my_token");
        assert_eq!(client.base_url, FLEX_BASE_URL);
    }

    #[test]
    fn test_client_with_custom_url() {
        let client = FlexApiClient::with_base_url("my_token", "https://custom.url");
        assert_eq!(client.token, "my_token");
        assert_eq!(client.base_url, "https://custom.url");
    }
}