2022-11-27 02:52:49 +01:00
|
|
|
use std::env;
|
2022-01-15 17:38:28 +01:00
|
|
|
use reqwest::Url;
|
|
|
|
use serde_derive::{Serialize, Deserialize};
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
pub struct Element {
|
|
|
|
pub id: Box<str>,
|
|
|
|
pub name: Box<str>,
|
|
|
|
pub simple_name: Box<str>,
|
2022-11-27 02:52:49 +01:00
|
|
|
pub color: u64,
|
2022-01-15 17:38:28 +01:00
|
|
|
}
|
|
|
|
|
2022-11-27 02:52:49 +01:00
|
|
|
impl Element {
|
2022-07-29 22:26:53 +02:00
|
|
|
#[allow(dead_code)]
|
2022-01-15 17:38:28 +01:00
|
|
|
async fn get(element: &str) -> Element {
|
2022-11-27 02:52:49 +01:00
|
|
|
let host = env::var("API_HOST").unwrap();
|
|
|
|
let port = env::var("API_PORT").unwrap();
|
|
|
|
let url = format!("http://{}:{}/api/elements/{}", host, port, element);
|
2022-01-15 17:38:28 +01:00
|
|
|
let url = Url::parse(&*url).expect("Can't convert url");
|
|
|
|
return reqwest::get(url).await.expect("Can't access Url").json::<Element>().await.expect("Wrong json format");
|
|
|
|
}
|
|
|
|
|
2022-07-29 22:26:53 +02:00
|
|
|
#[allow(dead_code)]
|
2022-01-15 17:38:28 +01:00
|
|
|
async fn get_all() -> Vec<Box<str>> {
|
2022-11-27 02:52:49 +01:00
|
|
|
let host = env::var("API_HOST").unwrap();
|
|
|
|
let port = env::var("API_PORT").unwrap();
|
|
|
|
let url = format!("http://{}:{}/api/elements", host, port);
|
2022-01-15 17:38:28 +01:00
|
|
|
let url = Url::parse(&*url).expect("Can't convert url");
|
|
|
|
return reqwest::get(url).await.expect("Can't access Url").json::<Vec<Box<str>>>().await.expect("Wrong json format");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-29 22:26:53 +02:00
|
|
|
#[test]
|
|
|
|
fn test_element() {
|
2022-11-27 02:52:49 +01:00
|
|
|
let data = std::fs::read_to_string("test/element.json").expect("No Element test file");
|
2022-07-29 22:26:53 +02:00
|
|
|
serde_json::from_str::<Element>(&data).expect("Didn't work");
|
2022-01-15 17:38:28 +01:00
|
|
|
}
|