use serde::{Deserialize, Serialize};
use crate::adapters::common::encode_path_segment;
use crate::error::Result;
use super::build_client;
use super::models::{FmpQuote, HistoricalPriceResponse, IntradayPrice};
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct AvailableSymbol {
pub symbol: Option<String>,
pub name: Option<String>,
pub currency: Option<String>,
#[serde(rename = "stockExchange")]
pub stock_exchange: Option<String>,
#[serde(rename = "exchangeShortName")]
pub exchange_short_name: Option<String>,
}
pub async fn crypto_quote(symbol: &str) -> Result<Vec<FmpQuote>> {
let client = build_client()?;
let path = format!("/api/v3/quote/{}", encode_path_segment(symbol));
client.get(&path, &[]).await
}
pub async fn crypto_available() -> Result<Vec<AvailableSymbol>> {
let client = build_client()?;
client
.get("/api/v3/symbol/available-cryptocurrencies", &[])
.await
}
pub async fn crypto_historical(
symbol: &str,
params: &[(&str, &str)],
) -> Result<HistoricalPriceResponse> {
let client = build_client()?;
let path = format!(
"/api/v3/historical-price-full/{}",
encode_path_segment(symbol)
);
client.get(&path, params).await
}
pub async fn crypto_intraday(
symbol: &str,
interval: &str,
params: &[(&str, &str)],
) -> Result<Vec<IntradayPrice>> {
let client = build_client()?;
let path = format!(
"/api/v3/historical-chart/{}/{}",
encode_path_segment(interval),
encode_path_segment(symbol)
);
client.get(&path, params).await
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_crypto_available_mock() {
let mut server = mockito::Server::new_async().await;
let _mock = server
.mock("GET", "/api/v3/symbol/available-cryptocurrencies")
.match_query(mockito::Matcher::AllOf(vec![mockito::Matcher::UrlEncoded(
"apikey".into(),
"test-key".into(),
)]))
.with_status(200)
.with_body(
serde_json::json!([
{
"symbol": "BTCUSD",
"name": "Bitcoin USD",
"currency": "USD",
"stockExchange": "CCC",
"exchangeShortName": "CRYPTO"
}
])
.to_string(),
)
.create_async()
.await;
let client = super::super::build_test_client(&server.url()).unwrap();
let result: Vec<AvailableSymbol> = client
.get("/api/v3/symbol/available-cryptocurrencies", &[])
.await
.unwrap();
assert_eq!(result.len(), 1);
assert_eq!(result[0].symbol.as_deref(), Some("BTCUSD"));
}
}