fmp-rs 0.1.1

Production-grade Rust client for Financial Modeling Prep API with intelligent caching, rate limiting, and comprehensive endpoint coverage
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
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
//! Transcripts & Communications endpoints

use crate::{client::FmpClient, error::Result, models::transcripts::*};
use serde::Serialize;

/// Transcripts & Communications API endpoints
pub struct Transcripts {
    client: FmpClient,
}

impl Transcripts {
    pub(crate) fn new(client: FmpClient) -> Self {
        Self { client }
    }

    /// Get earnings call transcripts list
    ///
    /// Returns a list of available earnings call transcripts for a company.
    /// Useful for discovering available transcripts before fetching full content.
    ///
    /// # Arguments
    /// * `symbol` - Company symbol (e.g., "AAPL")
    /// * `year` - Optional year filter (e.g., 2024)
    ///
    /// # Example
    /// ```no_run
    /// # use fmp_rs::FmpClient;
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let client = FmpClient::new()?;
    /// let transcripts = client.transcripts().get_transcript_list("AAPL", Some(2024)).await?;
    /// for transcript in transcripts.iter().take(5) {
    ///     println!("{} {} {}: {}",
    ///         transcript.symbol.as_deref().unwrap_or("N/A"),
    ///         transcript.quarter.as_deref().unwrap_or("N/A"),
    ///         transcript.year.unwrap_or(0),
    ///         transcript.date.as_deref().unwrap_or("N/A"));
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub async fn get_transcript_list(
        &self,
        symbol: &str,
        year: Option<i32>,
    ) -> Result<Vec<TranscriptSummary>> {
        #[derive(Serialize)]
        struct Query<'a> {
            #[serde(skip_serializing_if = "Option::is_none")]
            year: Option<i32>,
            apikey: &'a str,
        }

        let url = self
            .client
            .build_url(&format!("/earning_call_transcript/{}", symbol));
        self.client
            .get_with_query(
                &url,
                &Query {
                    year,
                    apikey: self.client.api_key(),
                },
            )
            .await
    }

    /// Get full earnings call transcript
    ///
    /// Returns the complete transcript content for a specific earnings call.
    /// Contains the full Q&A session with management and analysts.
    ///
    /// # Arguments
    /// * `symbol` - Company symbol (e.g., "AAPL")
    /// * `year` - Year of the earnings call
    /// * `quarter` - Quarter number (1, 2, 3, or 4)
    ///
    /// # Example
    /// ```no_run
    /// # use fmp_rs::FmpClient;
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let client = FmpClient::new()?;
    /// let transcript = client.transcripts().get_earnings_transcript("AAPL", 2024, 1).await?;
    /// if let Some(call) = transcript.first() {
    ///     println!("Transcript for {} {} {}:",
    ///         call.symbol.as_deref().unwrap_or("N/A"),
    ///         call.quarter.as_deref().unwrap_or("N/A"),
    ///         call.year.unwrap_or(0));
    ///     if let Some(content) = &call.content {
    ///         println!("Content length: {} characters", content.len());
    ///     }
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub async fn get_earnings_transcript(
        &self,
        symbol: &str,
        year: i32,
        quarter: i32,
    ) -> Result<Vec<EarningsTranscript>> {
        #[derive(Serialize)]
        struct Query<'a> {
            year: i32,
            quarter: i32,
            apikey: &'a str,
        }

        let url = self
            .client
            .build_url(&format!("/earning_call_transcript/{}", symbol));
        self.client
            .get_with_query(
                &url,
                &Query {
                    year,
                    quarter,
                    apikey: self.client.api_key(),
                },
            )
            .await
    }

    /// Get press releases for a company
    ///
    /// Returns recent press releases and corporate announcements.
    /// Useful for staying updated on company news and developments.
    ///
    /// # Arguments
    /// * `symbol` - Company symbol (e.g., "AAPL")
    /// * `limit` - Optional limit on number of results (default: 100)
    ///
    /// # Example
    /// ```no_run
    /// # use fmp_rs::FmpClient;
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let client = FmpClient::new()?;
    /// let releases = client.transcripts().get_press_releases("AAPL", Some(10)).await?;
    /// for release in releases.iter().take(5) {
    ///     println!("{}: {}",
    ///         release.date.as_deref().unwrap_or("N/A"),
    ///         release.title.as_deref().unwrap_or("No title"));
    ///     if let Some(summary) = &release.summary {
    ///         println!("  Summary: {}", summary);
    ///     }
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub async fn get_press_releases(
        &self,
        symbol: &str,
        limit: Option<i32>,
    ) -> Result<Vec<PressRelease>> {
        #[derive(Serialize)]
        struct Query<'a> {
            #[serde(skip_serializing_if = "Option::is_none")]
            limit: Option<i32>,
            apikey: &'a str,
        }

        let url = self
            .client
            .build_url(&format!("/press-releases/{}", symbol));
        self.client
            .get_with_query(
                &url,
                &Query {
                    limit,
                    apikey: self.client.api_key(),
                },
            )
            .await
    }

    /// Get conference call schedule
    ///
    /// Returns upcoming and recent conference calls across companies.
    /// Useful for tracking earnings dates and investor events.
    ///
    /// # Arguments
    /// * `from_date` - Optional start date (YYYY-MM-DD format)
    /// * `to_date` - Optional end date (YYYY-MM-DD format)
    ///
    /// # Example
    /// ```no_run
    /// # use fmp_rs::FmpClient;
    /// # #[tokio::main]
    /// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// let client = FmpClient::new()?;
    /// let calls = client.transcripts()
    ///     .get_conference_schedule(Some("2024-01-01"), Some("2024-01-31")).await?;
    /// for call in calls.iter().take(10) {
    ///     println!("{}: {} - {}",
    ///         call.date_time.as_deref().unwrap_or("N/A"),
    ///         call.symbol.as_deref().unwrap_or("N/A"),
    ///         call.title.as_deref().unwrap_or("No title"));
    /// }
    /// # Ok(())
    /// # }
    /// ```
    pub async fn get_conference_schedule(
        &self,
        from_date: Option<&str>,
        to_date: Option<&str>,
    ) -> Result<Vec<ConferenceCall>> {
        #[derive(Serialize)]
        struct Query<'a> {
            #[serde(skip_serializing_if = "Option::is_none")]
            from: Option<&'a str>,
            #[serde(skip_serializing_if = "Option::is_none")]
            to: Option<&'a str>,
            apikey: &'a str,
        }

        let url = self.client.build_url("/earning_calendar");
        self.client
            .get_with_query(
                &url,
                &Query {
                    from: from_date,
                    to: to_date,
                    apikey: self.client.api_key(),
                },
            )
            .await
    }
}

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

    fn create_test_client() -> FmpClient {
        FmpClient::builder().api_key("test_key").build().unwrap()
    }

    #[test]
    fn test_new() {
        let client = create_test_client();
        let transcripts = Transcripts::new(client);
        // Test passes if no panic occurs
    }

    #[tokio::test]
    #[ignore] // Requires API key
    async fn test_get_transcript_list() {
        let client = FmpClient::new().unwrap();
        let result = client
            .transcripts()
            .get_transcript_list("AAPL", Some(2024))
            .await;
        assert!(result.is_ok());

        let transcripts = result.unwrap();
        if !transcripts.is_empty() {
            let first_transcript = &transcripts[0];
            assert!(first_transcript.symbol.is_some());
            assert!(first_transcript.quarter.is_some() || first_transcript.year.is_some());
            println!("Found {} transcript summaries for AAPL", transcripts.len());
        }
    }

    #[tokio::test]
    #[ignore] // Requires API key
    async fn test_get_transcript_list_no_year() {
        let client = FmpClient::new().unwrap();
        let result = client.transcripts().get_transcript_list("AAPL", None).await;
        assert!(result.is_ok());

        let transcripts = result.unwrap();
        println!(
            "Found {} total transcript summaries for AAPL",
            transcripts.len()
        );
    }

    #[tokio::test]
    #[ignore] // Requires API key
    async fn test_get_earnings_transcript() {
        let client = FmpClient::new().unwrap();
        let result = client
            .transcripts()
            .get_earnings_transcript("AAPL", 2023, 4)
            .await;
        assert!(result.is_ok());

        let transcripts = result.unwrap();
        if let Some(transcript) = transcripts.first() {
            assert_eq!(transcript.symbol.as_deref(), Some("AAPL"));
            assert_eq!(transcript.year, Some(2023));

            if let Some(content) = &transcript.content {
                assert!(!content.is_empty());
                println!("Transcript content length: {} characters", content.len());
            }
        }
    }

    #[tokio::test]
    #[ignore] // Requires API key
    async fn test_get_press_releases() {
        let client = FmpClient::new().unwrap();
        let result = client
            .transcripts()
            .get_press_releases("AAPL", Some(10))
            .await;
        assert!(result.is_ok());

        let releases = result.unwrap();
        if !releases.is_empty() {
            let first_release = &releases[0];
            assert!(first_release.symbol.is_some() || first_release.company_name.is_some());
            assert!(first_release.title.is_some());
            assert!(first_release.date.is_some());
            println!("Found {} press releases for AAPL", releases.len());
        }
    }

    #[tokio::test]
    #[ignore] // Requires API key
    async fn test_get_press_releases_no_limit() {
        let client = FmpClient::new().unwrap();
        let result = client.transcripts().get_press_releases("MSFT", None).await;
        assert!(result.is_ok());

        let releases = result.unwrap();
        println!("Found {} total press releases for MSFT", releases.len());
    }

    #[tokio::test]
    #[ignore] // Requires API key
    async fn test_get_conference_schedule() {
        let client = FmpClient::new().unwrap();
        let result = client
            .transcripts()
            .get_conference_schedule(Some("2024-01-01"), Some("2024-01-31"))
            .await;
        assert!(result.is_ok());

        let calls = result.unwrap();
        if !calls.is_empty() {
            let first_call = &calls[0];
            assert!(first_call.symbol.is_some());
            assert!(first_call.date_time.is_some() || first_call.date_time.is_some());
            println!("Found {} conference calls in January 2024", calls.len());
        }
    }

    #[tokio::test]
    #[ignore] // Requires API key
    async fn test_get_conference_schedule_no_dates() {
        let client = FmpClient::new().unwrap();
        let result = client
            .transcripts()
            .get_conference_schedule(None, None)
            .await;
        assert!(result.is_ok());

        let calls = result.unwrap();
        println!("Found {} total upcoming conference calls", calls.len());
    }

    #[test]
    fn test_transcript_models_serialization() {
        use serde_json;

        // Test EarningsTranscript model
        let transcript = EarningsTranscript {
            symbol: Some("AAPL".to_string()),
            quarter: Some("Q1".to_string()),
            year: Some(2024),
            date: Some("2024-02-01".to_string()),
            content: Some("Thank you for joining Apple's Q1 2024 earnings call...".to_string()),
            company_name: Some("Apple Inc.".to_string()),
            fiscal_quarter: Some("Q1".to_string()),
            fiscal_year: Some(2024),
            transcript_id: Some("AAPL-2024-Q1".to_string()),
            language: Some("English".to_string()),
            duration: Some(60),
            analyst_count: Some(15),
            call_type: Some("Earnings".to_string()),
        };

        let json = serde_json::to_string(&transcript).unwrap();
        let deserialized: EarningsTranscript = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized.symbol, Some("AAPL".to_string()));
        assert_eq!(deserialized.year, Some(2024));

        // Test PressRelease model
        let release = PressRelease {
            symbol: Some("AAPL".to_string()),
            company_name: Some("Apple Inc.".to_string()),
            title: Some("Apple Reports Record Q1 Results".to_string()),
            date: Some("2024-02-01".to_string()),
            content: Some("Apple today announced financial results for Q1...".to_string()),
            release_type: Some("Earnings".to_string()),
            source: Some("Business Wire".to_string()),
            url: Some("https://investor.apple.com/news/press-release-details/2024/Apple-Reports-Record-Q1-Results/default.aspx".to_string()),
            language: Some("English".to_string()),
            word_count: Some(1250),
            summary: Some("Apple reported record Q1 revenue of $119.6 billion...".to_string()),
            related_symbols: Some(vec!["AAPL".to_string(), "NASDAQ".to_string()]),
            tags: Some(vec!["Earnings".to_string(), "Technology".to_string()]),
        };

        let json = serde_json::to_string(&release).unwrap();
        let deserialized: PressRelease = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized.symbol, Some("AAPL".to_string()));
        assert_eq!(
            deserialized.title,
            Some("Apple Reports Record Q1 Results".to_string())
        );
    }

    #[test]
    fn test_conference_call_model() {
        let call = ConferenceCall {
            symbol: Some("AAPL".to_string()),
            company_name: Some("Apple Inc.".to_string()),
            title: Some("Q1 2024 Earnings Call".to_string()),
            date_time: Some("2024-02-01 17:00:00".to_string()),
            call_type: Some("Earnings".to_string()),
            quarter: Some("Q1".to_string()),
            fiscal_year: Some(2024),
            year: Some(2024),
            timezone: Some("EST".to_string()),
            dial_in_info: Some("1-800-123-4567, Conference ID: 12345".to_string()),
            webcast_url: Some("https://investor.apple.com/webcast".to_string()),
            estimated_duration: Some(60),
            participants: Some(vec![
                "Tim Cook - CEO".to_string(),
                "Luca Maestri - CFO".to_string(),
            ]),
            status: Some("Scheduled".to_string()),
            industry: Some("Technology".to_string()),
            market_cap_category: Some("Large Cap".to_string()),
        };

        // Verify all fields are accessible
        assert_eq!(call.symbol, Some("AAPL".to_string()));
        assert_eq!(call.call_type, Some("Earnings".to_string()));
        assert_eq!(call.estimated_duration, Some(60));

        let json = serde_json::to_string(&call).unwrap();
        let deserialized: ConferenceCall = serde_json::from_str(&json).unwrap();
        assert_eq!(deserialized.symbol, Some("AAPL".to_string()));
    }

    #[test]
    fn test_date_parameter_validation() {
        // Test that date parameters are properly formatted
        let start_date = "2024-01-01";
        let end_date = "2024-12-31";

        // These should be valid ISO date formats
        assert!(chrono::NaiveDate::parse_from_str(start_date, "%Y-%m-%d").is_ok());
        assert!(chrono::NaiveDate::parse_from_str(end_date, "%Y-%m-%d").is_ok());
    }

    #[test]
    fn test_quarter_validation() {
        let valid_quarters = [1, 2, 3, 4];
        for quarter in valid_quarters {
            assert!(
                quarter >= 1 && quarter <= 4,
                "Quarter {} should be valid",
                quarter
            );
        }

        let invalid_quarters = [0, 5, -1, 13];
        for quarter in invalid_quarters {
            assert!(
                !(quarter >= 1 && quarter <= 4),
                "Quarter {} should be invalid",
                quarter
            );
        }
    }
}