mirror of
https://github.com/valmojr/armatak.git
synced 2026-06-13 23:43:28 +00:00
added async post marker function
This commit is contained in:
43
src/util.rs
43
src/util.rs
@@ -1,4 +1,5 @@
|
||||
use log::{error, info};
|
||||
use reqwest::Client;
|
||||
|
||||
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 {
|
||||
let parsed_address = api_address + "/api/login?include_auth_token";
|
||||
|
||||
|
||||
Reference in New Issue
Block a user