added async post marker function

This commit is contained in:
Valmo Trindade
2024-09-21 03:59:07 -03:00
parent bd538483b6
commit 5c071f08f1
2 changed files with 53 additions and 10 deletions

View File

@@ -11,12 +11,10 @@ pub fn get_auth_token(login_payload: LoginPayload) -> String {
} }
pub(crate) mod markers { pub(crate) mod markers {
use crate::{structs::Marker, util::{async_post_markers, parse_marker_to_payload}};
use log::{error, info}; use log::{error, info};
use std::thread;
use crate::{ use tokio::runtime::Runtime;
structs::Marker,
util::parse_marker_to_payload,
};
pub fn get(placeholder: String) -> &'static str { pub fn get(placeholder: String) -> &'static str {
info!("{}", placeholder); info!("{}", placeholder);
@@ -25,18 +23,20 @@ pub(crate) mod markers {
} }
pub fn post(data: Vec<Marker>) -> &'static str { pub fn post(data: Vec<Marker>) -> &'static str {
for item in data { thread::spawn(move || {
info!("{} - {}", item.uid, item.name); let rt = Runtime::new().unwrap();
} rt.block_on(async_post_markers(data));
});
return "not implemented yet"; "loading"
} }
pub fn post_debug(data: Vec<Marker>) -> String { pub fn post_debug(data: Vec<Marker>) -> String {
let client = reqwest::blocking::Client::new(); let client = reqwest::blocking::Client::new();
let authentication_token = data[0].api_auth_token.clone(); let authentication_token = data[0].api_auth_token.clone();
let parsed_address: String = data[0].api_address.clone() + "/api/markers?auth_token=" + &authentication_token; let parsed_address: String =
data[0].api_address.clone() + "/api/markers?auth_token=" + &authentication_token;
let mut status: String = "fetching".to_string(); let mut status: String = "fetching".to_string();

View File

@@ -1,4 +1,5 @@
use log::{error, info}; use log::{error, info};
use reqwest::Client;
use crate::structs::{LoginInfo, LoginPayload, Marker, MarkerPayload}; use crate::structs::{LoginInfo, LoginPayload, Marker, MarkerPayload};
@@ -30,6 +31,48 @@ pub fn parse_marker_to_payload(marker: Marker) -> MarkerPayload {
} }
} }
pub async fn async_post_markers(data: Vec<Marker>) {
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;
let mut status: String = "fetching".to_string();
info!("{}", status);
for marker in data {
let payload = parse_marker_to_payload(marker);
let request_body = serde_json::to_string(&payload).unwrap();
info!(
"Parsing: {}, to {} with {}",
request_body, parsed_address, authentication_token
);
let response = client
.post(&parsed_address)
.body(request_body)
.header("Content-Type", "application/json")
.send()
.await;
match response {
Ok(result) => {
status = result.status().to_string();
info!("Received: {}", result.text().await.unwrap());
}
Err(error) => {
status = "fetch failed".to_string();
error!("Error: {}", error)
}
}
}
info!("Final status: {}", status);
}
pub fn blocking_fetch_auth_token(payload: LoginInfo, api_address: String) -> String { pub fn blocking_fetch_auth_token(payload: LoginInfo, api_address: String) -> String {
let parsed_address = api_address + "/api/login?include_auth_token"; let parsed_address = api_address + "/api/login?include_auth_token";