Skip to content

Commit 72f42b3

Browse files
committed
Rename get_latest_prices() to get_prices()
Modify get_prices() Expose new_spice_client method() Add tests for get_prices and get_historical_prices
1 parent 7ed8f0f commit 72f42b3

3 files changed

Lines changed: 72 additions & 6 deletions

File tree

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ mod tls;
66

77
pub use client::{SpiceClient as Client};
88
pub use flight::{SqlFlightClient};
9+
pub use client::new_spice_client;
910

1011
// Further public exports and integrations

src/prices.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,13 @@ impl PricesClient {
117117
Ok(response)
118118
}
119119

120-
pub async fn get_latest_prices(&self, pairs: &[&str]) -> Result<LatestPricesResponse, Box<dyn Error>> {
121-
let url = format!("{}/v1/prices/latest?pair={}", self.base_url, pairs.join(","));
122-
let request = self.client.get(&url);
123-
let resp = self.add_headers(request).send().await?;
120+
pub async fn get_prices(
121+
&self,
122+
pair: &str,
123+
) -> Result<LatestPricesResponse, Box<dyn Error>> {
124+
let mut url = format!("{}/v1/prices?pairs={}", self.base_url, pair);
125+
126+
let resp = self.add_headers(self.client.get(&url)).send().await?;
124127
match resp.status() {
125128
reqwest::StatusCode::OK => {
126129
let response: LatestPricesResponse = resp.json().await?;
@@ -130,7 +133,7 @@ impl PricesClient {
130133
reqwest::StatusCode::TOO_MANY_REQUESTS => Err(Box::new(std::io::Error::new(std::io::ErrorKind::Other, "Rate limit exceeded, slow down"))),
131134
reqwest::StatusCode::INTERNAL_SERVER_ERROR => Err(Box::new(std::io::Error::new(std::io::ErrorKind::Other, "Internal server error"))),
132135
_ => Err(Box::new(std::io::Error::new(std::io::ErrorKind::Other, format!("Unexpected response status: {}", resp.status())))),
133-
}
136+
}
134137
}
135138

136139
pub async fn get_historical_prices(
@@ -140,7 +143,7 @@ impl PricesClient {
140143
end: Option<i64>,
141144
granularity: Option<&str>
142145
) -> Result<HashMap<String, Vec<HistoricalPriceData>>, Box<dyn Error>> {
143-
let mut url = format!("{}/v1/prices?pair={}", self.base_url, pairs.join(","));
146+
let mut url = format!("{}/v1/prices/historical?pairs={}", self.base_url, pairs.join(","));
144147

145148
if let Some(start_time) = start {
146149
url.push_str(&format!("&start={}", start_time));

tests/price_test.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
2+
#[cfg(test)]
3+
mod tests {
4+
use spice_rs::*;
5+
use std::env;
6+
use std::path::Path;
7+
8+
#[tokio::test]
9+
async fn test_get_prices() {
10+
dotenv::from_path(Path::new(".env.local")).ok();
11+
let api_key = env::var("API_KEY").expect("API_KEY not found");
12+
let http_addr = "https://data.spiceai.io".to_string();
13+
let flight_addr = "grpc+tls://flight.spiceai.io".to_string();
14+
let firecache_addr = "grpc+tls://firecache.spiceai.io".to_string();
15+
16+
let pair = "BTC-USD";
17+
18+
let spice_client: Client = new_spice_client(api_key, http_addr, flight_addr, firecache_addr).await.expect("Failed to initiate spice client");
19+
20+
let result = spice_client.prices.get_prices(pair).await;
21+
assert!(result.is_ok());
22+
23+
// Code for evaluate results received
24+
// match spice_client.prices.get_prices(pair).await {
25+
// Ok(r) => {
26+
// println!("{:?}",r)
27+
// }
28+
// Err(e) => {
29+
// println!("{:?}",e)
30+
// }
31+
// }
32+
}
33+
34+
#[tokio::test]
35+
async fn test_get_historical_prices() {
36+
dotenv::from_path(Path::new(".env.local")).ok();
37+
let api_key = env::var("API_KEY").expect("API_KEY not found");
38+
let http_addr = "https://data.spiceai.io".to_string();
39+
let flight_addr = "grpc+tls://flight.spiceai.io".to_string();
40+
let firecache_addr = "grpc+tls://firecache.spiceai.io".to_string();
41+
42+
let pair1 = "BTC-USD";
43+
let pair2 = "ETH-USD";
44+
45+
let spice_client: Client = new_spice_client(api_key, http_addr, flight_addr, firecache_addr).await.expect("Failed to initiate spice client");
46+
47+
let result = spice_client.prices.get_historical_prices(&[pair1, pair2], None, None, Some("1h")).await;
48+
assert!(result.is_ok());
49+
// Code for evaluate results received
50+
// match spice_client.prices.get_historical_prices(&[pair1, pair2], Some(1672531200000), Some(1675209600000), Some("1h")).await {
51+
// match spice_client.prices.get_historical_prices(&[pair1, pair2], None, None, Some("1h")).await {
52+
// Ok(r) => {
53+
// println!("{:?}",r)
54+
// }
55+
// Err(e) => {
56+
// println!("{:?}",e)
57+
// }
58+
// }
59+
}
60+
61+
// Further tests for the client module
62+
}

0 commit comments

Comments
 (0)