use std::cmp;
use crate::error::ReturnError;
use crate::traits::{self, MakingUrlFormat};
#[cfg(feature = "async_mode")]
use crate::request_async;
#[cfg(feature = "sync_mode")]
use crate::request_sync;
pub enum ReturnFormat {
Csv,
Json,
Xml,
}
impl ToString for ReturnFormat {
fn to_string(&self) -> String {
match self {
&Self::Csv => String::from("csv"),
&Self::Json => String::from("json"),
&Self::Xml => String::from("xml"),
}
}
}
impl traits::MakingUrlFormat for ReturnFormat {
fn generate_url_format(&self) -> String {
format!("type={}", self.to_string())
}
}
#[derive(Debug)]
pub struct ApiKey(String);
impl<'a> ApiKey {
fn change(&mut self, new_key: &'a str) -> Result<(), ReturnError> {
let api_key = ApiKey(new_key.to_string());
api_key.is_api_key_valid()?;
self.0 = new_key.to_string();
Ok(())
}
#[cfg(feature = "async_mode")]
fn check_api_key_validity_async(reference_url: String) -> Result<(), ReturnError> {
match request_async::do_request(&reference_url) {
Ok(_) => Ok(()),
Err(_) => Err(ReturnError::InvalidApiKeyOrBadInternetConnection),
}
}
#[cfg(feature = "sync_mode")]
fn check_api_key_validity_sync(reference_url: String) -> Result<(), ReturnError> {
match request_sync::do_request(&reference_url) {
Ok(_) => Ok(()),
Err(_) => Err(ReturnError::InvalidApiKeyOrBadInternetConnection),
}
}
fn is_api_key_valid(&self) -> Result<(), ReturnError> {
let reference_url =
format!(
"https://evds2.tcmb.gov.tr/service/evds/series=TP.DK.USD.S.YTL{}&key={}",
"&startDate=13-12-2011&endDate=13-12-2011&type=json",
self.0,
);
#[cfg(feature = "async_mode")]
return ApiKey::check_api_key_validity_async(reference_url);
#[cfg(feature = "sync_mode")]
return ApiKey::check_api_key_validity_sync(reference_url);
}
fn get(&self) -> &str {
&self.0
}
pub fn from(key: String) -> Result<ApiKey, ReturnError> {
let api_key = ApiKey(key);
api_key.is_api_key_valid()?;
Ok(api_key)
}
}
impl cmp::PartialEq for ApiKey {
fn eq(&self, other: &Self) -> bool {
self.get() == other.get()
}
}
impl traits::MakingUrlFormat for ApiKey {
fn generate_url_format(&self) -> String {
format!("key={}", self.0)
}
}
pub struct Evds {
api_key: ApiKey,
return_format: ReturnFormat,
}
impl<'a> Evds {
pub fn from(api_key: ApiKey, return_format: ReturnFormat) -> Evds {
Evds {
api_key,
return_format,
}
}
pub fn change_api_key(&mut self, api_key: &str) -> Result<(), ReturnError> {
self.api_key.change(api_key)?;
Ok(())
}
pub fn change_return_format(&mut self, return_format: ReturnFormat) {
self.return_format = return_format;
}
pub(crate) fn get_api_key_as_url(&self) -> String {
self.api_key.generate_url_format()
}
pub(crate) fn get_return_format_as_url(&self) -> String {
self.return_format.generate_url_format()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn api_functionality_should_work() {
let mut api_key = match ApiKey::from("abc".to_string()) {
Ok(api_key) => api_key,
Err(message) => {
println!("{}", message.to_string());
ApiKey("abc".to_string())
},
};
if let Err(message) = api_key.change("new_key") {
println!("{}", message.to_string());
};
}
#[test]
fn should_change_api_key() {
let api_key = match ApiKey::from("abc".to_string()) {
Ok(api_key) => api_key,
Err(message) => {
println!("{}", message.to_string());
ApiKey("abc".to_string())
},
};
let mut evds = Evds::from(api_key, ReturnFormat::Csv);
if let Err(message) = evds.change_api_key("VALID_API_KEY") {
println!("{}", message.to_string());
}
}
#[test]
fn evds_functionalities_should_work() {
let api_key = match ApiKey::from("abc".to_string()) {
Ok(api_key) => api_key,
Err(message) => {
println!("{}", message.to_string());
ApiKey("abc".to_string())
},
};
let mut evds = Evds::from(api_key, ReturnFormat::Csv);
println!("{}", &evds.return_format.to_string());
evds.change_return_format(ReturnFormat::Json);
println!("{}", &evds.return_format.to_string());
println!("\n\n{}\n{}\n",
evds.return_format.generate_url_format(),
evds.api_key.generate_url_format());
evds.return_format = ReturnFormat::Csv;
println!("\n\n{}\n{}\n",
evds.return_format.generate_url_format(),
evds.api_key.generate_url_format());
evds.return_format = ReturnFormat::Xml;
println!("\n\n{}\n{}\n",
evds.return_format.generate_url_format(),
evds.api_key.generate_url_format());
}
}