From 5c49a26aa4b97fd4174b395f7ab08ecd23c05e47 Mon Sep 17 00:00:00 2001 From: Valmo Trindade Date: Thu, 19 Sep 2024 00:49:00 -0300 Subject: [PATCH] added error handling to extract_auth_token workflow --- .../extract_data/fn_extract_auth_token.sqf | 4 +- addons/main/functions/fn_init.sqf | 19 ++++---- src/commands.rs | 43 ++++++++++++++++--- src/util.rs | 14 +++++- 4 files changed, 61 insertions(+), 19 deletions(-) diff --git a/addons/main/functions/extract_data/fn_extract_auth_token.sqf b/addons/main/functions/extract_data/fn_extract_auth_token.sqf index 9474706..723c465 100644 --- a/addons/main/functions/extract_data/fn_extract_auth_token.sqf +++ b/addons/main/functions/extract_data/fn_extract_auth_token.sqf @@ -4,9 +4,9 @@ private _atak_server_instance_password = missionNamespace getVariable "_atak_ser private _authentication = [_atak_server_instance, _atak_server_instance_username, _atak_server_instance_password]; -_atak_server_instance_token = ("armatak" callExtension ["get_auth_token", _authentication]) select 0; +_atak_server_instance_token = ("armatak" callExtension ["get_auth_token", [_authentication]]) select 0; -if ((_atak_server_instance_token != "") and !(["error", _atak_server_instance_token, false] call BIS_fnc_inString)) then { +if ((_atak_server_instance_token != "") and !(["ERROR", _atak_server_instance_token, false] call BIS_fnc_inString)) then { missionNamespace setVariable ["_atak_server_instance_token", _atak_server_instance_token]; private _warning = format ["ARMATAK
%1", "Authorized"]; diff --git a/addons/main/functions/fn_init.sqf b/addons/main/functions/fn_init.sqf index 0b04a24..bcd7e48 100644 --- a/addons/main/functions/fn_init.sqf +++ b/addons/main/functions/fn_init.sqf @@ -19,16 +19,17 @@ if (isServer && _activated) exitWith { missionNamespace setVariable ["_atak_server_instance_username", _atak_ots_api_username]; missionNamespace setVariable ["_atak_server_instance_password", _atak_ots_api_password]; - /* - _atak_server_instance_token = call armatak_fnc_extract_auth_token; + _atak_server_instance_token = call armatak_fnc_extract_auth_token; - if (_atak_server_instance_token == "") then { - private _warning = format ["ARMATAK
%1", "Connection Failed"]; - [[_warning, 2]] call CBA_fnc_notify; - } else { - private _warning = format ["ARMATAK
%1", "Connected"]; - [[_warning, 2]] call CBA_fnc_notify; - }; + if (_atak_server_instance_token == "") then { + private _warning = format ["ARMATAK
%1", "Connection Failed"]; + [[_warning, 2]] call CBA_fnc_notify; + } else { + private _warning = format ["ARMATAK
%1", "Connected"]; + [[_warning, 2]] call CBA_fnc_notify; + }; + + /* if (isMultiplayer) then { [{ diff --git a/src/commands.rs b/src/commands.rs index d3c3256..ab842b6 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -1,4 +1,5 @@ use crate::structs::{LoginInfo, LoginPayload}; +use log::error; use reqwest; use serde_json; @@ -15,20 +16,48 @@ pub fn get_auth_token(payload: LoginPayload) -> String { .post(&parsed_address) .body(request_body) .header("Content-Type", "application/json") - .send() - .unwrap(); + .send(); - let response_body: serde_json::Value = serde_json::from_str(&response.text().unwrap()).unwrap(); + match response { + Ok(result) => { + let response_body: Result = + serde_json::from_str(&result.text().unwrap()); - let csrf_token = response_body["response"]["csrf_token"].as_str().unwrap(); + match response_body { + Ok(result) => { + let csrf_token = result["response"]["csrf_token"].as_str(); - return csrf_token.to_string() + match csrf_token { + Some(result) => { + return result.to_string(); + } + None => { + let message = "ERROR: Provided JSON doesnt match a valid CSRF Token"; + error!("{}", message); + + return message.to_string(); + } + } + } + Err(error) => { + error!("{}", error); + + return "ERROR: failed to parse the response body to a valid JSON".to_string(); + } + } + } + Err(error) => { + error!("{}", error); + + return "ERROR: failed to fetch the OTS API".to_string(); + } + } } pub(crate) mod markers { use log::info; - use crate::structs::Marker; + use crate::{structs::Marker, util::post_marker}; pub fn get(placeholder: String) -> &'static str { info!("{}", placeholder); @@ -38,7 +67,7 @@ pub(crate) mod markers { pub fn post(data: Vec) -> &'static str { for item in data { - info!("{} - {}", item.uid, item.name) + post_marker(item); } return "not implemented yet"; diff --git a/src/util.rs b/src/util.rs index 1736d63..6542690 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,5 +1,17 @@ +use log::info; + +use crate::structs::Marker; + pub fn get_uuid() -> String { use uuid::Uuid; Uuid::new_v4().to_string() -} \ No newline at end of file +} + +pub async fn post_marker(marker: Marker) -> String { + let placeholder = format!("{} - {}", marker.uid, marker.name); + + info!("{}", placeholder); + + return placeholder +}