Skip to content

Commit

Permalink
support auth in frost connector
Browse files Browse the repository at this point in the history
  • Loading branch information
intarga committed Nov 15, 2024
1 parent cc3c474 commit fb6c779
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 17 deletions.
23 changes: 20 additions & 3 deletions met_binary/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clap::Parser;
use met_connectors::Frost;
use met_connectors::LustreNetatmo;
use met_connectors::{frost, Frost};
use rove::{
data_switch::{DataConnector, DataSwitch},
load_pipelines, start_server,
Expand All @@ -17,6 +17,10 @@ struct Args {
max_trace_level: Level,
#[arg(short, long, default_value_t = String::from("sample_pipeline/fresh"))]
pipeline_dir: String,
#[arg(short = 'u', long, default_value_t = String::from(""))]
frost_username: String,
#[arg(short = 'u', long, default_value_t = String::from(""))]
frost_password: String,
}

// TODO: use anyhow for error handling?
Expand All @@ -28,9 +32,22 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.with_max_level(args.max_trace_level)
.init();

let frost_connector = Frost {
credentials: frost::Credentials {
username: args.frost_username,
password: args.frost_password,
},
};

let data_switch = DataSwitch::new(HashMap::from([
("frost", &Frost as &dyn DataConnector),
("lustre_netatmo", &LustreNetatmo as &dyn DataConnector),
(
"frost",
Box::new(frost_connector) as Box<dyn DataConnector + Send>,
),
(
"lustre_netatmo",
Box::new(LustreNetatmo) as Box<dyn DataConnector + Send>,
),
]));

start_server(
Expand Down
4 changes: 4 additions & 0 deletions met_connectors/src/frost/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use chrono::{prelude::*, Duration};
use chronoutil::RelativeDuration;
use rove::data_switch::{self, DataCache, Polygon, SpaceSpec, TimeSpec, Timeseries, Timestamp};

use super::Credentials;

#[allow(clippy::type_complexity)]
fn extract_data(
mut resp: serde_json::Value,
Expand Down Expand Up @@ -168,6 +170,7 @@ pub async fn fetch_data_inner(
num_leading_points: u8,
num_trailing_points: u8,
extra_spec: Option<&str>,
credentials: &Credentials,
) -> Result<DataCache, data_switch::Error> {
// TODO: figure out how to share the client between rove reqs
let client = reqwest::Client::new();
Expand All @@ -194,6 +197,7 @@ pub async fn fetch_data_inner(

let resp: serde_json::Value = client
.get("https://frost-beta.met.no/api/v1/obs/met.no/filter/get")
.basic_auth(&credentials.username, Some(&credentials.password))
.query(&[
extra_query_param,
("elementids", element_id.to_string()),
Expand Down
11 changes: 10 additions & 1 deletion met_connectors/src/frost/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,16 @@ pub enum Error {
Misalignment(String),
}

#[derive(Debug, Clone)]
pub struct Credentials {
pub username: String,
pub password: String,
}

#[derive(Debug)]
pub struct Frost;
pub struct Frost {
pub credentials: Credentials,
}

#[derive(Deserialize, Debug)]
struct FrostObsBody {
Expand Down Expand Up @@ -114,6 +122,7 @@ impl DataConnector for Frost {
num_leading_points,
num_trailing_points,
extra_spec,
&self.credentials,
)
.await
}
Expand Down
2 changes: 1 addition & 1 deletion met_connectors/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mod frost;
pub mod frost;
mod lustre_netatmo;

pub use frost::Frost;
Expand Down
10 changes: 5 additions & 5 deletions src/data_switch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,23 +216,23 @@ pub trait DataConnector: Sync + std::fmt::Debug {
/// use std::collections::HashMap;
///
/// let data_switch = DataSwitch::new(HashMap::from([
/// ("test", &TestDataSource{
/// ("test", Box::new(TestDataSource{
/// data_len_single: 3,
/// data_len_series: 1000,
/// data_len_spatial: 1000,
/// } as &dyn DataConnector),
/// }) as Box<dyn DataConnector + Send>),
/// ]));
/// ```
#[derive(Debug, Clone)]
#[derive(Debug)]
pub struct DataSwitch<'ds> {
sources: HashMap<&'ds str, &'ds dyn DataConnector>,
sources: HashMap<&'ds str, Box<dyn DataConnector + Send>>,
}

impl<'ds> DataSwitch<'ds> {
/// Instantiate a new DataSwitch
///
/// See the DataSwitch struct documentation for more info
pub fn new(sources: HashMap<&'ds str, &'ds dyn DataConnector>) -> Self {
pub fn new(sources: HashMap<&'ds str, Box<dyn DataConnector + Send>>) -> Self {
Self { sources }
}

Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let data_switch = DataSwitch::new(HashMap::from([
//! ("test", &TestDataSource{
//! ("test", Box::new(TestDataSource{
//! data_len_single: 3,
//! data_len_series: 1000,
//! data_len_spatial: 1000,
//! } as &dyn DataConnector),
//! }) as Box<dyn DataConnector + Send>),
//! ]));
//!
//! start_server(
Expand All @@ -46,11 +46,11 @@
//! #[tokio::main]
//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
//! let data_switch = DataSwitch::new(HashMap::from([
//! ("test", &TestDataSource{
//! ("test", Box::new(TestDataSource{
//! data_len_single: 3,
//! data_len_series: 1000,
//! data_len_spatial: 1000,
//! } as &dyn DataConnector),
//! }) as Box<dyn DataConnector + Send>),
//! ]));
//!
//! let rove_scheduler = Scheduler::new(construct_hardcoded_pipeline(), data_switch);
Expand Down
2 changes: 1 addition & 1 deletion src/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub enum Error {
/// Receiver type for QC runs
///
/// Holds information about test pipelines and data sources
#[derive(Debug, Clone)]
#[derive(Debug)]
pub struct Scheduler<'a> {
// this is pub so that the server can determine the number of checks in a pipeline to size
// its channel with. can be made private if the server functionality is deprecated
Expand Down
4 changes: 2 additions & 2 deletions tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ pub async fn set_up_rove(
async fn integration_test_hardcoded_pipeline() {
let data_switch = DataSwitch::new(HashMap::from([(
"test",
&TestDataSource {
Box::new(TestDataSource {
data_len_single: DATA_LEN_SINGLE,
data_len_series: 1,
data_len_spatial: DATA_LEN_SPATIAL,
} as &dyn DataConnector,
}) as Box<dyn DataConnector + Send>,
)]));

let (coordinator_future, mut client) =
Expand Down

0 comments on commit fb6c779

Please sign in to comment.