2023-10-25 13:46:42 +11:00
|
|
|
use crate::Restful;
|
2023-10-23 16:17:50 +11:00
|
|
|
use heck::ToKebabCase;
|
2023-10-25 13:46:42 +11:00
|
|
|
use reqwest::Method;
|
2023-10-23 16:17:50 +11:00
|
|
|
use serde::Deserialize;
|
2023-10-25 12:24:36 +11:00
|
|
|
use std::collections::HashMap;
|
2023-10-23 16:17:50 +11:00
|
|
|
|
2023-10-25 12:24:36 +11:00
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
2023-10-23 16:17:50 +11:00
|
|
|
#[serde(rename_all = "UPPERCASE")]
|
2023-10-24 12:30:48 +11:00
|
|
|
pub struct TimdorrEndpoint {
|
2023-10-23 16:17:50 +11:00
|
|
|
#[serde(rename = "TYPE")]
|
2023-10-25 12:24:36 +11:00
|
|
|
pub method: String,
|
|
|
|
pub uri: String,
|
|
|
|
pub auth: bool,
|
2023-10-23 16:17:50 +11:00
|
|
|
}
|
|
|
|
|
2023-10-25 13:46:42 +11:00
|
|
|
impl Restful for TimdorrEndpoint {
|
|
|
|
fn method(&self) -> &Method {
|
|
|
|
match self.method.as_str() {
|
|
|
|
"GET" => &Method::GET,
|
|
|
|
"POST" => &Method::POST,
|
|
|
|
"PUT" => &Method::PUT,
|
|
|
|
"DELETE" => &Method::DELETE,
|
|
|
|
_ => panic!("Unknown method {}", self.method),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn uri(&self) -> &str {
|
|
|
|
&self.uri
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-24 12:30:48 +11:00
|
|
|
pub fn parse(json_str: &str) -> HashMap<String, TimdorrEndpoint> {
|
|
|
|
let map: HashMap<String, TimdorrEndpoint> = serde_json::from_str(json_str).unwrap();
|
2023-10-23 16:17:50 +11:00
|
|
|
|
2023-10-25 12:24:36 +11:00
|
|
|
// Massage all URLs to have a / before "api".
|
|
|
|
let map = map
|
|
|
|
.into_iter()
|
|
|
|
.map(|(k, mut v)| {
|
|
|
|
v.uri = format!("/{}", v.uri);
|
|
|
|
(k, v)
|
|
|
|
})
|
|
|
|
.collect::<HashMap<String, TimdorrEndpoint>>();
|
|
|
|
|
|
|
|
// Rename all the keys to kebab-case
|
2023-10-23 16:17:50 +11:00
|
|
|
map.into_iter()
|
|
|
|
.map(|(k, v)| (k.to_kebab_case(), v))
|
2023-10-24 12:30:48 +11:00
|
|
|
.collect::<HashMap<String, TimdorrEndpoint>>()
|
2023-10-23 16:17:50 +11:00
|
|
|
}
|