Skip to content

Commit da5e699

Browse files
authored
Change new_spice_client return type (#6)
1 parent cd42cf6 commit da5e699

5 files changed

Lines changed: 63 additions & 46 deletions

File tree

src/client.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ use arrow_flight::decode::FlightRecordBatchStream;
33
use std::error::Error;
44
use tonic::transport::Channel;
55

6-
pub async fn new_spice_client(api_key: String) -> Result<SpiceClient, Box<dyn Error>> {
6+
pub async fn new_spice_client(api_key: String) -> SpiceClient {
77
return new_spice_client_with_address(
88
api_key.to_string(),
99
"https://data.spiceai.io".to_string(),
1010
"https://flight.spiceai.io".to_string(),
1111
)
12-
.await;
12+
.await
13+
.expect("Error Initiating Client");
1314
}
1415

1516
pub async fn new_spice_client_with_address(
@@ -35,7 +36,10 @@ impl SpiceClient {
3536
prices: PricesClient::new(Some(http_addr), api_key),
3637
}
3738
}
38-
pub async fn query(&mut self, query: String) -> Result<FlightRecordBatchStream, Box<dyn Error>> {
39+
pub async fn query(
40+
&mut self,
41+
query: String,
42+
) -> Result<FlightRecordBatchStream, Box<dyn Error>> {
3943
self.flight.query(query).await
4044
}
4145
}

src/flight.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ impl SqlFlightClient {
2929

3030
pub async fn query(
3131
&mut self,
32-
query: String
32+
query: String,
3333
) -> std::result::Result<FlightRecordBatchStream, Box<dyn Error>> {
3434
match self.authenticate().await {
3535
Err(e) => return Err(e.into()),

tests/client_test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ mod tests {
88
async fn new_client() -> Client {
99
dotenv::from_path(Path::new(".env.local")).ok();
1010
let api_key = env::var("API_KEY").expect("API_KEY not found");
11-
let result = new_spice_client(api_key).await;
12-
return result.expect("Failed to new spice client");
11+
let spice_client = new_spice_client(api_key).await;
12+
return spice_client;
1313
}
1414

1515
#[tokio::test]

tests/price_test.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
#[cfg(test)]
22
mod tests {
3+
use chrono::{TimeZone, Utc};
34
use spice_rs::*;
45
use std::env;
56
use std::path::Path;
6-
use chrono::{DateTime, Utc, TimeZone};
77

88
async fn new_client() -> Client {
99
dotenv::from_path(Path::new(".env.local")).ok();
1010
let api_key = env::var("API_KEY").expect("API_KEY not found");
11-
let result = new_spice_client(api_key).await;
12-
return result.expect("Failed to new spice client");
11+
let spice_client = new_spice_client(api_key).await;
12+
return spice_client;
1313
}
1414

1515
#[tokio::test]
@@ -31,12 +31,7 @@ mod tests {
3131

3232
let result = spice_client
3333
.prices
34-
.get_historical_prices(
35-
&[pair1, pair2],
36-
start_time,
37-
end_time,
38-
Some("1h"),
39-
)
34+
.get_historical_prices(&[pair1, pair2], start_time, end_time, Some("1h"))
4035
.await;
4136
assert!(result.is_ok());
4237
}

tests/readme_test.rs

Lines changed: 49 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,63 @@
11
#[cfg(test)]
22
mod tests {
3+
use chrono::{Duration, Utc};
34
use spice_rs::new_spice_client;
45
use std::env;
56
use std::ops::{Add, Sub};
67
use std::path::Path;
7-
use chrono::{Duration, Utc};
88

99
#[tokio::test]
1010
async fn test_readme() {
1111
dotenv::from_path(Path::new(".env.local")).ok();
1212
let api_key = env::var("API_KEY").expect("API_KEY not found");
13-
14-
match new_spice_client(api_key).await{
15-
Ok(mut client) => {
16-
let data = client.query("SELECT * FROM eth.recent_blocks LIMIT 10;".to_string()).await;
17-
if data.is_err() {
18-
assert!(false, "failed to query: {:#?}", data.expect_err(""))
19-
}
20-
let supported_pairs = client.prices.get_supported_pairs().await;
21-
if supported_pairs.is_err() {
22-
assert!(false, "failed to get supported pairs: {:#?}", supported_pairs.expect_err(""))
23-
}
24-
let price_data = client.prices.get_prices(&["BTC-USDC"]).await;
25-
if price_data.is_err() {
26-
assert!(false, "failed to get prices: {:#?}", price_data.expect_err(""))
27-
}
28-
let historical_price_data = client.prices.get_historical_prices(&["BTC-USDC"], Option::None, Option::None, Option::None).await;
29-
if historical_price_data.is_err() {
30-
assert!(false, "failed to get prices: {:#?}", historical_price_data.expect_err(""))
31-
}
32-
let now = Utc::now();
33-
let start = now.sub(Duration::seconds(3600));
3413

35-
let historical_price_data = client.prices.get_historical_prices(&["BTC-USDC"], Some(start),Some(now), Option::None).await;
36-
if historical_price_data.is_err() {
37-
assert!(false, "failed to get prices: {:#?}", historical_price_data.expect_err(""))
38-
}
39-
}
40-
Err(e) => {
41-
assert!(false, "Error: {:#?}", e);
42-
}
14+
let mut client = new_spice_client(api_key).await;
15+
let data = client
16+
.query("SELECT * FROM eth.recent_blocks LIMIT 10;".to_string())
17+
.await;
18+
if data.is_err() {
19+
assert!(false, "failed to query: {:#?}", data.expect_err(""))
20+
}
21+
let supported_pairs = client.prices.get_supported_pairs().await;
22+
if supported_pairs.is_err() {
23+
assert!(
24+
false,
25+
"failed to get supported pairs: {:#?}",
26+
supported_pairs.expect_err("")
27+
)
28+
}
29+
let price_data = client.prices.get_prices(&["BTC-USDC"]).await;
30+
if price_data.is_err() {
31+
assert!(
32+
false,
33+
"failed to get prices: {:#?}",
34+
price_data.expect_err("")
35+
)
36+
}
37+
let historical_price_data = client
38+
.prices
39+
.get_historical_prices(&["BTC-USDC"], Option::None, Option::None, Option::None)
40+
.await;
41+
if historical_price_data.is_err() {
42+
assert!(
43+
false,
44+
"failed to get prices: {:#?}",
45+
historical_price_data.expect_err("")
46+
)
47+
}
48+
let now = Utc::now();
49+
let start = now.sub(Duration::seconds(3600));
50+
51+
let historical_price_data = client
52+
.prices
53+
.get_historical_prices(&["BTC-USDC"], Some(start), Some(now), Option::None)
54+
.await;
55+
if historical_price_data.is_err() {
56+
assert!(
57+
false,
58+
"failed to get prices: {:#?}",
59+
historical_price_data.expect_err("")
60+
)
4361
}
44-
}
62+
}
4563
}

0 commit comments

Comments
 (0)