use std::borrow::Cow;
use log::debug;
use serde_json::Value;
use crate::structs::{context::Platform, struct_to_vec::param};
use super::*;
pub async fn api_call(
platform: Platform,
method: &str,
mut params: Vec<(Cow<'_, str>, Cow<'_, str>)>,
config: &Config,
) -> Result<Value, String> {
let url = match platform {
Platform::VK => format!("https://api.vk.com/method/{}", method),
Platform::Telegram => format!(
"https://api.telegram.org/{}/{}",
config.tg_access_token, method
),
};
let access_token = match platform {
Platform::VK => config.vk_access_token.clone(),
Platform::Telegram => "".to_owned(),
};
if platform == Platform::VK {
params.push(param("v", &config.vk_api_version));
}
let response = request(&url, &access_token, params).await;
match response {
Ok(response_text) => {
debug!("API call response text: {}", response_text);
let response_json: serde_json::Value =
serde_json::from_str(&response_text).map_err(|e| e.to_string())?;
match platform {
Platform::VK => {
if let Some(error) = response_json.get("error") {
let error_msg = error["error_msg"].as_str().unwrap_or("Unknown error");
Err(error_msg.to_string())
} else {
Ok(response_json)
}
}
Platform::Telegram => {
if let Some(ok) = response_json.get("ok") {
if ok.as_bool().unwrap_or(false) {
Ok(response_json)
} else {
let error_msg = response_json["description"]
.as_str()
.unwrap_or("Unknown error");
Err(error_msg.to_string())
}
} else {
Err("Invalid response from Telegram API".to_string())
}
}
}
}
Err(_) => Err("Error while sending request".to_string()),
}
}