diff options
| author | Fuwn <[email protected]> | 2023-04-06 01:53:22 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2023-04-06 01:53:22 -0700 |
| commit | 9235be2eedc2c4f3a78cb704b0a107f33607e530 (patch) | |
| tree | fc927ec6d5a58d2e995cf6ff80ae2804a2265531 /src/modules/stocks.rs | |
| parent | deps(windmark): bump 0.2.5 -> 0.3.1 (diff) | |
| download | locus-9235be2eedc2c4f3a78cb704b0a107f33607e530.tar.xz locus-9235be2eedc2c4f3a78cb704b0a107f33607e530.zip | |
feat: native async reqwest calls
Diffstat (limited to 'src/modules/stocks.rs')
| -rw-r--r-- | src/modules/stocks.rs | 90 |
1 files changed, 47 insertions, 43 deletions
diff --git a/src/modules/stocks.rs b/src/modules/stocks.rs index 108e792..ff1a77d 100644 --- a/src/modules/stocks.rs +++ b/src/modules/stocks.rs @@ -72,19 +72,19 @@ impl Quote { } } -fn symbol_to_string(symbol: &str) -> String { +async fn symbol_to_string(symbol: &str) -> String { let mut quote = None; // https://github.com/seanmonstar/reqwest/issues/1017#issuecomment-1157260218 - if let Ok(response) = tokio::task::block_in_place(|| { - reqwest::blocking::get(&format!( - "https://finnhub.io/api/v1/quote?symbol={}&token={}", - symbol, - std::env::var("FINNHUB_TOKEN") - .expect("could not locate FINNHUB_TOKEN environment variable") - )) - }) { - if let Ok(response_content) = response.json::<Quote>() { + if let Ok(response) = reqwest::get(&format!( + "https://finnhub.io/api/v1/quote?symbol={}&token={}", + symbol, + std::env::var("FINNHUB_TOKEN") + .expect("could not locate FINNHUB_TOKEN environment variable") + )) + .await + { + if let Ok(response_content) = response.json::<Quote>().await { if response_content.dp.is_some() { quote = Some(response_content); } else { @@ -136,15 +136,17 @@ pub fn module(router: &mut windmark::Router) { "/stocks", "Explore and search the stock market using Gemini!", |context| { - success!( + async move { + success!( format!( "# The Stock Market\n\n=> /stocks/search Symbol Search\n=> /stocks/referrals Referrals\n=> /cryptocurrency Cryptocurrency Dashboard\n=> /stocks/telegram Telegram Groups\n\n## Popular \ Symbols\n\n### AAPL\n\n{}\n\n### TSLA\n\n{}\n\n## Credits\n\nFinancial data provided by\n\n=> https://finnhub.io/ Finnhub", - symbol_to_string("AAPL"), - symbol_to_string("TSLA") + symbol_to_string("AAPL").await, + symbol_to_string("TSLA").await ), context ) + } }, ); @@ -153,47 +155,49 @@ pub fn module(router: &mut windmark::Router) { "/stocks/search", "Search for a specific symbol", |context| { - let mut symbol = context.url.query().unwrap_or("Symbol Search"); + async move { + let mut symbol = context.url.query().unwrap_or("Symbol Search"); - if symbol.is_empty() { - symbol = "Symbol Search"; - } - - let mut response = format!( - "# {}\n\n=> /stocks Dashboard\n=> /cryptocurrency Cryptocurrency \ - Dashboard\n=> /stocks/telegram Telegram Groups\n=> /stocks/search \ - Search", - symbol - ); - - if symbol != "Symbol Search" { - if let Some(query) = context.url.query_pairs().next() { - if query.0 == "action" && query.1 == "go" { - return windmark::Response::input( - "Which symbol would you like to explore?", - ); - } - - let symbol = query.0; + if symbol.is_empty() { + symbol = "Symbol Search"; + } - if symbol != "Symbol Search" { - response = format!( - "{}\n\nYou searched for \"{}\"!\n\n## Key Statistics\n\n{}", - response, - symbol, - symbol_to_string(&symbol) - ); + let mut response = format!( + "# {}\n\n=> /stocks Dashboard\n=> /cryptocurrency Cryptocurrency \ + Dashboard\n=> /stocks/telegram Telegram Groups\n=> /stocks/search \ + Search", + symbol + ); + + if symbol != "Symbol Search" { + if let Some(query) = context.url.query_pairs().next() { + if query.0 == "action" && query.1 == "go" { + return windmark::Response::input( + "Which symbol would you like to explore?", + ); + } + + let symbol = query.0; + + if symbol != "Symbol Search" { + response = format!( + "{}\n\nYou searched for \"{}\"!\n\n## Key Statistics\n\n{}", + response, + symbol, + symbol_to_string(&symbol).await + ); + } } } - } - success!( + success!( format!( "{}\n\n## Credits\n\nFinancial data provided by\n\n=> https://finnhub.io/ Finnhub", response ), context ) + } }, ); } |