From 1aeeee426f0160ecf85cca2d61b90e051f3f9322 Mon Sep 17 00:00:00 2001 From: Valmo Trindade Date: Tue, 28 Jan 2025 16:24:48 -0300 Subject: [PATCH] added chrono depedency and removed util functions related to http requests --- Cargo.lock | 7 ++-- Cargo.toml | 1 + src/util.rs | 99 +---------------------------------------------------- 3 files changed, 7 insertions(+), 100 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 79ba820..4f7b3b0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -86,6 +86,7 @@ name = "armatak" version = "0.8.1" dependencies = [ "arma-rs", + "chrono", "futures", "futures-util", "http", @@ -248,13 +249,15 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "chrono" -version = "0.4.38" +version = "0.4.39" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a21f936df1771bf62b77f047b726c4625ff2e8aa607c01ec06e5a05bd8463401" +checksum = "7e36cc9d416881d2e24f9a963be5fb1cd90966419ac844274161d10488b3e825" dependencies = [ "android-tzdata", "iana-time-zone", + "js-sys", "num-traits", + "wasm-bindgen", "windows-targets 0.52.6", ] diff --git a/Cargo.toml b/Cargo.toml index fb72775..5e21719 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,7 @@ edition = "2021" [dependencies] arma-rs = "1.10.4" +chrono = "0.4.39" futures = "0.3.31" futures-util = "0.3.31" http = "1.1.0" diff --git a/src/util.rs b/src/util.rs index 9663933..2f0e843 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,7 +1,6 @@ use log::{error, info, warn}; -use reqwest::Client; use std::net::{IpAddr, UdpSocket}; -use crate::structs::{LogPayload, LoginInfo, LoginPayload, Marker, MarkerPayload}; +use crate::structs::LogPayload; pub fn log_info(data: LogPayload) -> String { match data.status.as_str() { @@ -44,99 +43,3 @@ pub fn get_local_address() -> String { }, } } - -pub fn parse_login_to_payload(login_payload: LoginPayload) -> LoginInfo { - return LoginInfo { - username: login_payload.username.to_owned(), - password: login_payload.password.to_owned() - } -} - -pub fn parse_marker_to_payload(marker: Marker) -> MarkerPayload { - return MarkerPayload { - uid: marker.uid, - longitude: marker.longitude, - latitude: marker.latitude, - name: marker.name, - r#type: marker.r#type, - course: marker.course, - speed: marker.speed, - hae: marker.hae - } -} - -pub async fn async_post_markers(data: Vec) { - let client = Client::new(); - - let authentication_token = data[0].api_auth_token.clone(); - let parsed_address: String = - data[0].api_address.clone() + "/api/markers?auth_token=" + &authentication_token; - - for marker in data { - let payload = parse_marker_to_payload(marker); - let request_body = serde_json::to_string(&payload).unwrap(); - - let response = client - .post(&parsed_address) - .body(request_body) - .header("Content-Type", "application/json") - .send() - .await; - - match response { - Ok(result) => { - info!("Received: {}", result.text().await.unwrap()); - } - Err(error) => { - error!("Error: {}", error) - } - } - } -} - -pub fn blocking_fetch_auth_token(payload: LoginInfo, api_address: String) -> String { - let parsed_address = api_address + "/api/login?include_auth_token"; - - let request_body = serde_json::to_string(&payload).unwrap(); - let client = reqwest::blocking::Client::new(); - let response = client - .post(&parsed_address) - .body(request_body) - .header("Content-Type", "application/json") - .send(); - - match response { - Ok(result) => { - let response_body: Result = - serde_json::from_str(&result.text().unwrap()); - - match response_body { - Ok(result) => { - let csrf_token = result["response"]["user"]["authentication_token"].as_str(); - info!("Provided JSON: {:?}", result.as_str()); - match csrf_token { - Some(result) => { - return result.to_string(); - } - None => { - let message = "ERROR: Provided JSON doesnt match a valid Authentication Token"; - error!("{}", message); - - return message.to_string(); - } - } - } - Err(error) => { - error!("ERROR: failed to parse the response body to a valid JSON: {}", error); - - return "ERROR: failed to parse the response body to a valid JSON".to_string(); - } - } - } - Err(error) => { - error!("ERROR: failed to fetch the OTS API: {}", error); - - return "ERROR: failed to fetch the OTS API".to_string(); - } - } -} \ No newline at end of file