-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #378 from systemaccounting/377-graphql-measure-sink
377 graphql measure sink
- Loading branch information
Showing
18 changed files
with
445 additions
and
131 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,8 @@ | ||
[package] | ||
name = "wsclient" | ||
version = "0.1.0" | ||
edition = "2021" | ||
rust-version.workspace = true | ||
|
||
[dependencies] | ||
tungstenite = "0.24.0" |
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,128 @@ | ||
use std::net::TcpStream; | ||
use tungstenite::{connect, stream::MaybeTlsStream, WebSocket}; | ||
|
||
pub struct WsClient { | ||
uri: WsUri, | ||
} | ||
|
||
impl WsClient { | ||
pub fn new( | ||
base_uri: String, | ||
measure: String, | ||
date: String, | ||
country: Option<String>, | ||
region: Option<String>, | ||
municipality: Option<String>, | ||
) -> Self { | ||
let uri = WsUri::new(base_uri, measure, date, country, region, municipality); | ||
WsClient { uri } | ||
} | ||
|
||
pub fn connect(&self) -> WebSocket<MaybeTlsStream<TcpStream>> { | ||
let (socket, _) = connect(self.uri.query_string()).expect("failed to connect"); | ||
socket | ||
} | ||
} | ||
|
||
// todo: merge with Params in services/measure/src/main.rs as separate crate | ||
struct WsUri { | ||
base_uri: String, | ||
measure: String, | ||
date: String, | ||
country: Option<String>, | ||
region: Option<String>, | ||
municipality: Option<String>, | ||
} | ||
|
||
impl WsUri { | ||
pub fn new( | ||
base_uri: String, | ||
measure: String, | ||
date: String, | ||
country: Option<String>, | ||
region: Option<String>, | ||
municipality: Option<String>, | ||
) -> Self { | ||
WsUri { | ||
base_uri, | ||
measure, | ||
date, | ||
country, | ||
region, | ||
municipality, | ||
} | ||
} | ||
|
||
pub fn query_string(&self) -> String { | ||
let mut uri = format!("{}?", self.base_uri); | ||
uri.push_str(&format!("measure={}", self.measure)); | ||
uri.push_str(&format!("&date={}", self.date)); | ||
if let Some(country) = &self.country { | ||
uri.push_str(&format!("&country={}", country)); | ||
} | ||
if let Some(region) = &self.region { | ||
uri.push_str(&format!("®ion={}", region)); | ||
} | ||
if let Some(municipality) = &self.municipality { | ||
uri.push_str(&format!("&municipality={}", municipality)); | ||
} | ||
uri.replace(" ", "%20") | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_ws_uri() { | ||
let ws_uri = WsUri::new( | ||
"ws://localhost:10010/ws".to_string(), | ||
"gdp".to_string(), | ||
"2024-08-21".to_string(), | ||
Some("United States of America".to_string()), | ||
Some("California".to_string()), | ||
Some("Sacramento".to_string()), | ||
); | ||
|
||
assert_eq!(ws_uri.measure, "gdp"); | ||
assert_eq!(ws_uri.date, "2024-08-21"); | ||
assert_eq!(ws_uri.country, Some("United States of America".to_string())); | ||
assert_eq!(ws_uri.region, Some("California".to_string())); | ||
assert_eq!(ws_uri.municipality, Some("Sacramento".to_string())); | ||
} | ||
|
||
#[test] | ||
fn test_ws_uri_full_query_string() { | ||
let ws_uri = WsUri::new( | ||
"ws://localhost:10010/ws".to_string(), | ||
"gdp".to_string(), | ||
"2024-08-21".to_string(), | ||
Some("United States of America".to_string()), | ||
Some("California".to_string()), | ||
Some("Sacramento".to_string()), | ||
); | ||
|
||
assert_eq!( | ||
ws_uri.query_string(), | ||
"ws://localhost:10010/ws?measure=gdp&date=2024-08-21&country=United%20States%20of%20America®ion=California&municipality=Sacramento" | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_ws_uri_cal_query_string() { | ||
let ws_uri = WsUri::new( | ||
"ws://localhost:10010/ws".to_string(), | ||
"gdp".to_string(), | ||
"2024-08-21".to_string(), | ||
Some("United States of America".to_string()), | ||
Some("California".to_string()), | ||
None, | ||
); | ||
|
||
assert_eq!( | ||
ws_uri.query_string(), | ||
"ws://localhost:10010/ws?measure=gdp&date=2024-08-21&country=United%20States%20of%20America®ion=California" | ||
); | ||
} | ||
} |
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
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
Oops, something went wrong.