-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Examples: Add concurrent example for streaming tick prices
- Loading branch information
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use xapi; | ||
|
||
use futures::future::join_all; | ||
use std::error::Error; | ||
use std::fs; | ||
use tokio::time::{sleep, Duration}; | ||
|
||
async fn get_tick_prices(x: xapi::XApi) -> Result<(), xapi::Error> { | ||
x.stream.get_tick_prices("BITCOIN", 0, 0).await?; | ||
x.stream.get_tick_prices("ETHEREUM", 0, 0).await | ||
} | ||
|
||
async fn listen(x: xapi::XApi) -> Result<(), xapi::Error> { | ||
loop { | ||
let record = x.stream.listen().await?; | ||
println!("{:?}", record); | ||
} | ||
} | ||
|
||
async fn listen_tick_prices(credentials: &xapi::Credentials) -> Result<(), xapi::Error> { | ||
let x = xapi::connect(&credentials).await?; | ||
|
||
let handles = vec![ | ||
tokio::spawn(get_tick_prices(x.clone())), | ||
tokio::spawn(listen(x.clone())), | ||
]; | ||
|
||
let results = join_all(handles).await; | ||
for result in results { | ||
if let Err(err) = result.unwrap() { | ||
return Err(err); | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Box<dyn Error>> { | ||
let json = fs::read_to_string("credentials.json")?; | ||
let credentials = xapi::Credentials::from(&json)?; | ||
|
||
while let Err(err) = listen_tick_prices(&credentials).await { | ||
println!("{}, Reconnecting in 5 seconds ...", err); | ||
sleep(Duration::from_secs(5)).await; | ||
} | ||
|
||
Ok(()) | ||
} |