use crate::api;
use crate::api::request::ApiRequest;
use crate::config;
use std::error::Error;
fn get_call(endpoint: String, version: u8) -> Result<json::JsonValue, Box<dyn Error>> {
let api_request = get_api_request(endpoint, json::object! {}, version);
api::get(api_request)
}
pub fn post_call(
endpoint: String,
json_value: json::JsonValue,
version: u8,
) -> Result<String, Box<dyn Error>> {
let api_request = get_api_request(endpoint, json_value, version);
api::post(api_request)
}
pub fn put_call(
endpoint: String,
json_value: json::JsonValue,
version: u8,
) -> Result<String, Box<dyn Error>> {
let api_request = get_api_request(endpoint, json_value, version);
api::put(api_request)
}
fn get_api_request(endpoint: String, json_value: json::JsonValue, version: u8) -> ApiRequest {
let auth_mode = config::get_config("auth_mode".to_string());
ApiRequest {
url: endpoint,
username: config::get_config("email".to_string()),
password: config::get_config("token".to_string()),
json: json_value,
namespace: config::get_config("namespace".to_string()),
version,
auth_mode: if auth_mode.is_empty() {
"Basic".to_string()
} else {
auth_mode
},
}
}
pub fn get_call_v2(endpoint: String) -> Result<json::JsonValue, Box<dyn Error>> {
get_call(endpoint, 2)
}
pub fn get_call_v3(endpoint: String) -> Result<json::JsonValue, Box<dyn Error>> {
get_call(
endpoint,
config::get_config("version".to_string())
.parse::<u8>()
.unwrap_or(3),
)
}