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
//! Analyst endpoints
use crate::Result;
use crate::client::FmpClient;
use crate::models::analyst::{AnalystEstimates, AnalystGrade, ConsensusSummary, PriceTarget};
use crate::models::common::Period;
use serde::Serialize;
/// Analyst API endpoints
pub struct Analyst {
client: FmpClient,
}
#[derive(Debug, Clone, Serialize)]
struct AnalystQuery {
#[serde(skip_serializing_if = "Option::is_none")]
period: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
limit: Option<u32>,
}
impl Analyst {
pub(crate) fn new(client: FmpClient) -> Self {
Self { client }
}
/// Get analyst estimates for a symbol
///
/// # Arguments
/// * `symbol` - Stock symbol (e.g., "AAPL")
/// * `period` - Period (annual or quarter, optional)
/// * `limit` - Number of results (optional)
///
/// # Example
/// ```no_run
/// # use fmp_rs::FmpClient;
/// # use fmp_rs::models::common::Period;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = FmpClient::new()?;
/// let estimates = client.analyst().get_estimates("AAPL", Some(Period::Annual), Some(10)).await?;
/// for estimate in estimates {
/// println!("{}: Estimated EPS {:.2}", estimate.date, estimate.estimated_eps_avg);
/// }
/// # Ok(())
/// # }
/// ```
pub async fn get_estimates(
&self,
symbol: &str,
period: Option<Period>,
limit: Option<u32>,
) -> Result<Vec<AnalystEstimates>> {
let query = AnalystQuery {
period: period.map(|p| p.to_string()),
limit,
};
self.client
.get_with_query(&format!("v3/analyst-estimates/{}", symbol), &query)
.await
}
/// Get price targets for a symbol
///
/// # Arguments
/// * `symbol` - Stock symbol (e.g., "AAPL")
///
/// # Example
/// ```no_run
/// # use fmp_rs::FmpClient;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = FmpClient::new()?;
/// let targets = client.analyst().get_price_targets("AAPL").await?;
/// for target in targets {
/// println!("{} from {}: ${:.2}", target.published_date, target.analyst_company, target.price_target);
/// }
/// # Ok(())
/// # }
/// ```
pub async fn get_price_targets(&self, symbol: &str) -> Result<Vec<PriceTarget>> {
self.client
.get_with_query(&format!("v4/price-target?symbol={}", symbol), &())
.await
}
/// Get analyst price target consensus for a symbol
///
/// # Arguments
/// * `symbol` - Stock symbol (e.g., "AAPL")
///
/// # Example
/// ```no_run
/// # use fmp_rs::FmpClient;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = FmpClient::new()?;
/// let consensus = client.analyst().get_price_target_consensus("AAPL").await?;
/// println!("Target High: ${:.2}, Target Low: ${:.2}",
/// consensus.first().unwrap().target_high,
/// consensus.first().unwrap().target_low);
/// # Ok(())
/// # }
/// ```
pub async fn get_price_target_consensus(
&self,
symbol: &str,
) -> Result<Vec<PriceTargetConsensus>> {
self.client
.get_with_query(&format!("v4/price-target-consensus?symbol={}", symbol), &())
.await
}
/// Get analyst recommendations/upgrades for a symbol
///
/// # Arguments
/// * `symbol` - Stock symbol (e.g., "AAPL")
///
/// # Example
/// ```no_run
/// # use fmp_rs::FmpClient;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = FmpClient::new()?;
/// let grades = client.analyst().get_grades("AAPL").await?;
/// for grade in grades {
/// println!("{} from {}: {} -> {}",
/// grade.published_date,
/// grade.grading_company,
/// grade.previous_grade.unwrap_or_default(),
/// grade.new_grade);
/// }
/// # Ok(())
/// # }
/// ```
pub async fn get_grades(&self, symbol: &str) -> Result<Vec<AnalystGrade>> {
self.client
.get_with_query(&format!("v3/grade/{}", symbol), &())
.await
}
/// Get analyst recommendation consensus for a symbol
///
/// # Arguments
/// * `symbol` - Stock symbol (e.g., "AAPL")
///
/// # Example
/// ```no_run
/// # use fmp_rs::FmpClient;
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = FmpClient::new()?;
/// let consensus = client.analyst().get_recommendation_consensus("AAPL").await?;
/// for rec in consensus {
/// println!("Strong Buy: {}, Buy: {}, Hold: {}, Sell: {}, Strong Sell: {}",
/// rec.strong_buy, rec.buy, rec.hold, rec.sell, rec.strong_sell);
/// println!("Consensus: {}", rec.consensus);
/// }
/// # Ok(())
/// # }
/// ```
pub async fn get_recommendation_consensus(
&self,
symbol: &str,
) -> Result<Vec<ConsensusSummary>> {
self.client
.get_with_query(&format!("v3/rating/{}", symbol), &())
.await
}
}
/// Price target consensus
#[derive(Debug, Clone, Serialize, serde::Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct PriceTargetConsensus {
pub symbol: String,
pub target_high: f64,
pub target_low: f64,
pub target_consensus: f64,
pub target_median: f64,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_new() {
let client = FmpClient::builder().api_key("test_key").build().unwrap();
let analyst = Analyst::new(client);
assert!(std::ptr::addr_of!(analyst.client).is_null() == false);
}
#[tokio::test]
#[ignore] // Requires API key
async fn test_get_estimates() {
let client = FmpClient::new().unwrap();
let result = client
.analyst()
.get_estimates("AAPL", Some(Period::Annual), Some(5))
.await;
assert!(result.is_ok());
}
#[tokio::test]
#[ignore] // Requires API key
async fn test_get_price_targets() {
let client = FmpClient::new().unwrap();
let result = client.analyst().get_price_targets("AAPL").await;
assert!(result.is_ok());
}
#[tokio::test]
#[ignore] // Requires API key
async fn test_get_grades() {
let client = FmpClient::new().unwrap();
let result = client.analyst().get_grades("AAPL").await;
assert!(result.is_ok());
}
#[tokio::test]
#[ignore] // Requires API key
async fn test_get_recommendation_consensus() {
let client = FmpClient::new().unwrap();
let result = client.analyst().get_recommendation_consensus("AAPL").await;
assert!(result.is_ok());
}
// Missing endpoint test
#[tokio::test]
#[ignore] // Requires API key
async fn test_get_price_target_consensus() {
let client = FmpClient::new().unwrap();
let result = client.analyst().get_price_target_consensus("AAPL").await;
assert!(result.is_ok());
let consensus = result.unwrap();
if !consensus.is_empty() {
let target = &consensus[0];
assert_eq!(target.symbol, "AAPL".to_string());
// Validate price target ranges
assert!(target.target_high >= target.target_low); // High should be >= low
assert!(target.target_high > 0.0 && target.target_low > 0.0); // Prices should be positive
}
}
// Edge case tests
#[tokio::test]
#[ignore] // Requires API key
async fn test_analyst_data_with_invalid_symbol() {
let client = FmpClient::new().unwrap();
let invalid_symbol = "INVALID_STOCK_12345";
// Test all endpoints with invalid symbol
let estimates_result = client
.analyst()
.get_estimates(invalid_symbol, Some(Period::Annual), Some(5))
.await;
let targets_result = client.analyst().get_price_targets(invalid_symbol).await;
let consensus_result = client
.analyst()
.get_price_target_consensus(invalid_symbol)
.await;
let grades_result = client.analyst().get_grades(invalid_symbol).await;
let rec_consensus_result = client
.analyst()
.get_recommendation_consensus(invalid_symbol)
.await;
// All should handle gracefully - either empty results or errors
match estimates_result {
Ok(data) => assert!(data.is_empty()),
Err(_) => {} // Error is acceptable
}
match targets_result {
Ok(data) => assert!(data.is_empty()),
Err(_) => {} // Error is acceptable
}
match consensus_result {
Ok(data) => assert!(data.is_empty()),
Err(_) => {} // Error is acceptable
}
match grades_result {
Ok(data) => assert!(data.is_empty()),
Err(_) => {} // Error is acceptable
}
match rec_consensus_result {
Ok(data) => assert!(data.is_empty()),
Err(_) => {} // Error is acceptable
}
}
#[tokio::test]
#[ignore] // Requires API key
async fn test_estimates_with_different_periods() {
let client = FmpClient::new().unwrap();
// Test both annual and quarterly periods
let annual_result = client
.analyst()
.get_estimates("AAPL", Some(Period::Annual), Some(3))
.await;
let quarterly_result = client
.analyst()
.get_estimates("AAPL", Some(Period::Quarter), Some(3))
.await;
assert!(annual_result.is_ok());
assert!(quarterly_result.is_ok());
let annual_estimates = annual_result.unwrap();
let quarterly_estimates = quarterly_result.unwrap();
// Should have different data for different periods
if !annual_estimates.is_empty() && !quarterly_estimates.is_empty() {
// Data should be structured correctly
assert_eq!(annual_estimates[0].symbol, "AAPL".to_string());
assert_eq!(quarterly_estimates[0].symbol, "AAPL".to_string());
}
}
#[tokio::test]
#[ignore] // Requires API key
async fn test_estimates_limit_parameter() {
let client = FmpClient::new().unwrap();
// Test with different limits
let small_limit_result = client
.analyst()
.get_estimates("AAPL", Some(Period::Annual), Some(2))
.await;
let large_limit_result = client
.analyst()
.get_estimates("AAPL", Some(Period::Annual), Some(10))
.await;
assert!(small_limit_result.is_ok());
assert!(large_limit_result.is_ok());
let small_estimates = small_limit_result.unwrap();
let large_estimates = large_limit_result.unwrap();
// Small limit should return <= 2 items
assert!(small_estimates.len() <= 2);
// Large limit should return more data (if available)
if !small_estimates.is_empty() && !large_estimates.is_empty() {
// Large limit should return at least as much data as small limit
assert!(large_estimates.len() >= small_estimates.len());
}
}
#[tokio::test]
#[ignore] // Requires API key
async fn test_price_targets_data_validation() {
let client = FmpClient::new().unwrap();
let result = client.analyst().get_price_targets("AAPL").await;
assert!(result.is_ok());
let targets = result.unwrap();
if !targets.is_empty() {
let target = &targets[0];
// Validate required fields
assert!(!target.symbol.is_empty());
// Validate price target values
assert!(target.price_target > 0.0); // Should be positive
// Validate analyst name
assert!(!target.analyst_name.is_empty()); // Should not be empty string
// Validate company name
assert!(!target.analyst_company.is_empty()); // Should not be empty string
}
}
#[tokio::test]
#[ignore] // Requires API key
async fn test_grades_data_validation() {
let client = FmpClient::new().unwrap();
let result = client.analyst().get_grades("AAPL").await;
assert!(result.is_ok());
let grades = result.unwrap();
if !grades.is_empty() {
let grade = &grades[0];
// Validate symbol
assert_eq!(grade.symbol, "AAPL".to_string());
// Validate grade values
let new_grade = &grade.new_grade;
// Common analyst grades
let _valid_grades = vec![
"BUY",
"SELL",
"HOLD",
"STRONG_BUY",
"STRONG_SELL",
"OUTPERFORM",
"UNDERPERFORM",
"NEUTRAL",
"OVERWEIGHT",
"UNDERWEIGHT",
];
// Should be a recognized grade or at least not empty
assert!(!new_grade.is_empty());
}
}
#[tokio::test]
#[ignore] // Requires API key
async fn test_multiple_symbols_consistency() {
let client = FmpClient::new().unwrap();
let symbols = vec!["AAPL", "MSFT", "GOOGL"];
for symbol in symbols {
let result = client.analyst().get_price_targets(symbol).await;
assert!(result.is_ok());
let targets = result.unwrap();
if !targets.is_empty() {
// Each result should be for the correct symbol
assert_eq!(targets[0].symbol, symbol.to_string());
}
}
}
}