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
//! Insider trading endpoints
use crate::client::FmpClient;
use crate::error::Result;
use crate::models::institutional::{InsiderTrade, InsiderTradingRss};
/// Insider Trading API endpoints
pub struct InsiderTrades {
client: FmpClient,
}
impl InsiderTrades {
pub(crate) fn new(client: FmpClient) -> Self {
Self { client }
}
/// Get insider trading transactions for a symbol
///
/// # Arguments
///
/// * `symbol` - Stock symbol (e.g., "AAPL")
/// * `limit` - Optional limit on number of results
///
/// # Example
///
/// ```no_run
/// use fmp_rs::FmpClient;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = FmpClient::new()?;
/// let trades = client.insider_trades()
/// .get_insider_trading("AAPL", Some(20)).await?;
///
/// println!("Recent insider trades:");
/// for trade in &trades {
/// println!(" {}: {} {} {} shares at ${:.2}",
/// trade.transaction_date,
/// trade.reporting_name,
/// trade.transaction_type,
/// trade.securities_transacted,
/// trade.price);
/// }
/// Ok(())
/// }
/// ```
pub async fn get_insider_trading(
&self,
symbol: &str,
limit: Option<u32>,
) -> Result<Vec<InsiderTrade>> {
let mut url = format!("/api/v4/insider-trading?symbol={}", symbol);
if let Some(limit) = limit {
url.push_str(&format!("&limit={}", limit));
}
self.client.get(&url).await
}
/// Get insider trading RSS feed (recent filings)
///
/// # Arguments
///
/// * `limit` - Optional limit on number of results
///
/// # Example
///
/// ```no_run
/// use fmp_rs::FmpClient;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let client = FmpClient::new()?;
/// let filings = client.insider_trades()
/// .get_insider_trading_rss(Some(50)).await?;
///
/// println!("Recent insider trading filings:");
/// for filing in &filings {
/// println!(" {}: {} ({})",
/// filing.filing_date,
/// filing.symbol,
/// filing.company_name.as_deref().unwrap_or("N/A"));
/// }
/// Ok(())
/// }
/// ```
pub async fn get_insider_trading_rss(
&self,
limit: Option<u32>,
) -> Result<Vec<InsiderTradingRss>> {
let url = if let Some(limit) = limit {
format!("/api/v4/insider-trading-rss-feed?limit={}", limit)
} else {
"/api/v4/insider-trading-rss-feed".to_string()
};
self.client.get(&url).await
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_new() {
let client = FmpClient::builder().api_key("test_key").build().unwrap();
let _ = InsiderTrades::new(client);
}
// Golden path tests
#[tokio::test]
#[ignore = "requires FMP API key"]
async fn test_get_insider_trading() {
let client = FmpClient::new().unwrap();
let result = client
.insider_trades()
.get_insider_trading("AAPL", Some(20))
.await;
assert!(result.is_ok());
let trades = result.unwrap();
assert!(!trades.is_empty());
assert!(trades.len() <= 20);
}
#[tokio::test]
#[ignore = "requires FMP API key"]
async fn test_get_insider_trading_rss() {
let client = FmpClient::new().unwrap();
let result = client
.insider_trades()
.get_insider_trading_rss(Some(50))
.await;
assert!(result.is_ok());
let filings = result.unwrap();
assert!(!filings.is_empty());
assert!(filings.len() <= 50);
}
// Edge case tests
#[tokio::test]
#[ignore = "requires FMP API key"]
async fn test_get_insider_trading_no_limit() {
let client = FmpClient::new().unwrap();
let result = client
.insider_trades()
.get_insider_trading("AAPL", None)
.await;
assert!(result.is_ok());
}
#[tokio::test]
#[ignore = "requires FMP API key"]
async fn test_get_insider_trading_rss_no_limit() {
let client = FmpClient::new().unwrap();
let result = client.insider_trades().get_insider_trading_rss(None).await;
assert!(result.is_ok());
}
#[tokio::test]
#[ignore = "requires FMP API key"]
async fn test_get_insider_trading_invalid_symbol() {
let client = FmpClient::new().unwrap();
let result = client
.insider_trades()
.get_insider_trading("INVALID_XYZ123", Some(10))
.await;
// Should either return empty vec or error
if let Ok(trades) = result {
assert!(trades.is_empty());
}
}
#[tokio::test]
#[ignore = "requires FMP API key"]
async fn test_get_insider_trading_large_limit() {
let client = FmpClient::new().unwrap();
let result = client
.insider_trades()
.get_insider_trading("AAPL", Some(100))
.await;
assert!(result.is_ok());
let trades = result.unwrap();
assert!(trades.len() <= 100);
}
// Error handling tests
#[tokio::test]
async fn test_invalid_api_key() {
let client = FmpClient::builder()
.api_key("invalid_key_12345")
.build()
.unwrap();
let result = client
.insider_trades()
.get_insider_trading("AAPL", Some(10))
.await;
assert!(result.is_err());
}
#[tokio::test]
async fn test_empty_symbol() {
let client = FmpClient::builder().api_key("test_key").build().unwrap();
let result = client
.insider_trades()
.get_insider_trading("", Some(10))
.await;
// Should handle gracefully
assert!(result.is_err() || result.unwrap().is_empty());
}
}