127 lines
3.5 KiB
Rust
127 lines
3.5 KiB
Rust
use core::sync;
|
|
use std::collections::HashMap;
|
|
use serde::{Serialize, Deserialize};
|
|
use serde_json::{ to_string_pretty, Value };
|
|
use anyhow::Result;
|
|
use std::sync::Arc;
|
|
|
|
|
|
// v2
|
|
#[derive(Serialize, Deserialize, Debug, PartialEq)]
|
|
pub struct ApiConfigV2 {
|
|
pub id : u64,
|
|
#[serde(default)]
|
|
pub template : Vec<Template>,
|
|
pub ip_address : String,
|
|
pub login : Option<String>,
|
|
pub pass : Option<String>,
|
|
pub api_key : Option<String>,
|
|
pub period : u32, // if "0" -> inf
|
|
pub timeout : u32, // if "0" -> no-delay
|
|
}
|
|
|
|
impl Default for ApiConfigV2 {
|
|
fn default() -> Self {
|
|
ApiConfigV2 {
|
|
id : 0,
|
|
template : Vec::new(),
|
|
ip_address : String::from("no_ip"),
|
|
login : None,
|
|
pass : None,
|
|
api_key : None,
|
|
period : 0,
|
|
timeout : 0,
|
|
}
|
|
}
|
|
}
|
|
impl ApiConfigV2 {
|
|
pub fn template() -> Self {
|
|
ApiConfigV2 {
|
|
id : 1111,
|
|
template : Vec::new(),
|
|
ip_address : String::from("ip"),
|
|
login : None,
|
|
pass : None,
|
|
api_key : None,
|
|
period : 1111,
|
|
timeout : 1111,
|
|
}
|
|
}
|
|
pub fn pattern() -> Self {
|
|
ApiConfigV2 {
|
|
id : 1111,
|
|
template : vec![
|
|
Template {
|
|
id : String::from("no id"),
|
|
name : String::from("open api"),
|
|
url : String::from("https://dummy-json.mock.beeceptor.com/countries"),
|
|
method : String::from("GET"),
|
|
measure : Vec::new(),
|
|
}
|
|
],
|
|
ip_address : String::from("ip"),
|
|
login : None,
|
|
pass : None,
|
|
api_key : None,
|
|
period : 1,
|
|
timeout : 1,
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
|
|
pub struct Template {
|
|
pub id : String,
|
|
pub name : String,
|
|
pub url : String,
|
|
pub method : String,
|
|
#[serde(default)]
|
|
pub measure : Vec<String>,
|
|
}
|
|
|
|
impl Default for Template {
|
|
fn default() -> Self {
|
|
Template {
|
|
id : String::from("no-id"),
|
|
name : String::from("no-name"),
|
|
url : String::from("no-url"),
|
|
method : String::from("post"),
|
|
measure : Vec::new(),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
pub struct ProcessedEndpoint {
|
|
id : String,
|
|
name : String,
|
|
url : String,
|
|
method : String,
|
|
#[serde(default)]
|
|
metrics : HashMap<String, Value>,
|
|
}
|
|
|
|
impl ProcessedEndpoint {
|
|
pub fn new(id: &str, name: &str, url: &str, method: &str, metrics: HashMap<String, Value>) -> Self {
|
|
ProcessedEndpoint {
|
|
id : id.to_owned(),
|
|
name : name.to_owned(),
|
|
url : url.to_owned(),
|
|
method : method.to_owned(),
|
|
metrics : metrics,
|
|
}
|
|
}
|
|
pub fn from_target_response(response: &str, keys: &Template) -> Result<String> {
|
|
let mut hm: HashMap<String, Value> = HashMap::new();
|
|
let mut response: Value = serde_json::from_str(response)?;
|
|
|
|
let _ = keys.measure.iter()
|
|
.map(|key| (key, response[key].take()))
|
|
.for_each(|(key, value)| {
|
|
hm.insert(key.clone(), value);
|
|
});
|
|
let val = ProcessedEndpoint::new(&keys.id, &keys.name, &keys.url, &keys.method,hm);
|
|
Ok(to_string_pretty(&val)?)
|
|
}
|
|
} |