Move mock to own function
This commit is contained in:
parent
4f6d474c53
commit
31ed4a0eaa
178
src/requests.rs
178
src/requests.rs
|
@ -28,110 +28,112 @@ struct Err {
|
||||||
#[serde(rename = "error-type")]
|
#[serde(rename = "error-type")]
|
||||||
error_type: String,
|
error_type: String,
|
||||||
}
|
}
|
||||||
|
#[cfg(test)]
|
||||||
|
fn mock_get_rates(code: &String) -> Result<Status, reqwest::Error> {
|
||||||
|
let response: ConversionRates = match code.as_str() {
|
||||||
|
"PLN" => serde_json::from_str(include_str!(concat!(
|
||||||
|
".",
|
||||||
|
crate::main_separator!(),
|
||||||
|
"mock_data",
|
||||||
|
crate::main_separator!(),
|
||||||
|
"PLN.json"
|
||||||
|
)))
|
||||||
|
.expect("Error when deserializng"),
|
||||||
|
"EUR" => serde_json::from_str(include_str!(concat!(
|
||||||
|
".",
|
||||||
|
crate::main_separator!(),
|
||||||
|
"mock_data",
|
||||||
|
crate::main_separator!(),
|
||||||
|
"EUR.json"
|
||||||
|
)))
|
||||||
|
.expect("Error when deserializng"),
|
||||||
|
_ => {
|
||||||
|
panic!("Unknown code")
|
||||||
|
}
|
||||||
|
};
|
||||||
|
cache::add_rates(
|
||||||
|
response.time_next_update_unix,
|
||||||
|
&response.base_code,
|
||||||
|
&response.conversion_rates,
|
||||||
|
)
|
||||||
|
.expect("Error while caching response");
|
||||||
|
Ok(Status::OK)
|
||||||
|
}
|
||||||
|
#[cfg(test)]
|
||||||
|
pub fn mock_get_currencies() -> Result<Status, reqwest::Error> {
|
||||||
|
let codes: CurrencyCodes = serde_json::from_str(include_str!(concat!(
|
||||||
|
".",
|
||||||
|
crate::main_separator!(),
|
||||||
|
"mock_data",
|
||||||
|
crate::main_separator!(),
|
||||||
|
"codes.json"
|
||||||
|
)))
|
||||||
|
.expect("Error when deserializng");
|
||||||
|
for code in codes.supported_codes {
|
||||||
|
cache::add_code(code).expect("Error when adding code to cache");
|
||||||
|
}
|
||||||
|
return Ok(Status::OK);
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get_rates(code: &String) -> Result<Status, reqwest::Error> {
|
pub fn get_rates(code: &String) -> Result<Status, reqwest::Error> {
|
||||||
if !cfg!(test) {
|
if cfg!(test) {
|
||||||
let response = reqwest::blocking::get(format!(
|
#[cfg(test)]
|
||||||
"{}{}{}{}",
|
return mock_get_rates(code);
|
||||||
get_endpoint(),
|
|
||||||
get_api_key().expect("Error when getting api key from cache"),
|
|
||||||
"/latest/",
|
|
||||||
code.to_uppercase()
|
|
||||||
))?;
|
|
||||||
if response.status().is_success() {
|
|
||||||
let response: ConversionRates =
|
|
||||||
serde_json::from_str(&response.text()?).expect("Error when deserializng");
|
|
||||||
cache::add_rates(
|
|
||||||
response.time_next_update_unix,
|
|
||||||
&response.base_code,
|
|
||||||
&response.conversion_rates,
|
|
||||||
)
|
|
||||||
.expect("Error while caching response");
|
|
||||||
return Ok(Status::OK);
|
|
||||||
} else {
|
|
||||||
let err: Err =
|
|
||||||
serde_json::from_str(&response.text()?).expect("Error when deserializng");
|
|
||||||
if err.error_type == "invalid-key" {
|
|
||||||
return Ok(Status::INVALID);
|
|
||||||
} else if err.error_type == "quota-reached" {
|
|
||||||
return Ok(Status::LIMIT);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(Status::ERROR)
|
|
||||||
}
|
}
|
||||||
// TEST
|
let response = reqwest::blocking::get(format!(
|
||||||
else {
|
"{}{}{}{}",
|
||||||
let response: ConversionRates = match code.as_str() {
|
get_endpoint(),
|
||||||
"PLN" => serde_json::from_str(include_str!(concat!(
|
get_api_key().expect("Error when getting api key from cache"),
|
||||||
".",
|
"/latest/",
|
||||||
crate::main_separator!(),
|
code.to_uppercase()
|
||||||
"mock_data",
|
))?;
|
||||||
crate::main_separator!(),
|
if response.status().is_success() {
|
||||||
"PLN.json"
|
let response: ConversionRates =
|
||||||
)))
|
serde_json::from_str(&response.text()?).expect("Error when deserializng");
|
||||||
.expect("Error when deserializng"),
|
|
||||||
"EUR" => serde_json::from_str(include_str!(concat!(
|
|
||||||
".",
|
|
||||||
crate::main_separator!(),
|
|
||||||
"mock_data",
|
|
||||||
crate::main_separator!(),
|
|
||||||
"EUR.json"
|
|
||||||
)))
|
|
||||||
.expect("Error when deserializng"),
|
|
||||||
_ => {
|
|
||||||
panic!("Unknown code")
|
|
||||||
}
|
|
||||||
};
|
|
||||||
cache::add_rates(
|
cache::add_rates(
|
||||||
response.time_next_update_unix,
|
response.time_next_update_unix,
|
||||||
&response.base_code,
|
&response.base_code,
|
||||||
&response.conversion_rates,
|
&response.conversion_rates,
|
||||||
)
|
)
|
||||||
.expect("Error while caching response");
|
.expect("Error while caching response");
|
||||||
Ok(Status::OK)
|
return Ok(Status::OK);
|
||||||
|
} else {
|
||||||
|
let err: Err = serde_json::from_str(&response.text()?).expect("Error when deserializng");
|
||||||
|
if err.error_type == "invalid-key" {
|
||||||
|
return Ok(Status::INVALID);
|
||||||
|
} else if err.error_type == "quota-reached" {
|
||||||
|
return Ok(Status::LIMIT);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Ok(Status::ERROR)
|
||||||
}
|
}
|
||||||
pub fn get_currencies() -> Result<Status, reqwest::Error> {
|
pub fn get_currencies() -> Result<Status, reqwest::Error> {
|
||||||
if !cfg!(test) {
|
if cfg!(test) {
|
||||||
let response = reqwest::blocking::get(format!(
|
#[cfg(test)]
|
||||||
"{}{}{}",
|
return mock_get_currencies();
|
||||||
get_endpoint(),
|
|
||||||
get_api_key().expect("Error when getting api key from cache"),
|
|
||||||
"/codes"
|
|
||||||
))?;
|
|
||||||
if response.status().is_success() {
|
|
||||||
let codes: CurrencyCodes =
|
|
||||||
serde_json::from_str(&response.text()?).expect("Error when deserializng");
|
|
||||||
for code in codes.supported_codes {
|
|
||||||
cache::add_code(code).expect("Error when adding code to cache");
|
|
||||||
}
|
|
||||||
return Ok(Status::OK);
|
|
||||||
} else {
|
|
||||||
let err: Err =
|
|
||||||
serde_json::from_str(&response.text()?).expect("Error when deserializng");
|
|
||||||
if err.error_type == "invalid-key" {
|
|
||||||
return Ok(Status::INVALID);
|
|
||||||
} else if err.error_type == "quota-reached" {
|
|
||||||
return Ok(Status::LIMIT);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(Status::ERROR)
|
|
||||||
}
|
}
|
||||||
//TEST
|
let response = reqwest::blocking::get(format!(
|
||||||
else {
|
"{}{}{}",
|
||||||
let codes: CurrencyCodes = serde_json::from_str(include_str!(concat!(
|
get_endpoint(),
|
||||||
".",
|
get_api_key().expect("Error when getting api key from cache"),
|
||||||
crate::main_separator!(),
|
"/codes"
|
||||||
"mock_data",
|
))?;
|
||||||
crate::main_separator!(),
|
if response.status().is_success() {
|
||||||
"codes.json"
|
let codes: CurrencyCodes =
|
||||||
)))
|
serde_json::from_str(&response.text()?).expect("Error when deserializng");
|
||||||
.expect("Error when deserializng");
|
|
||||||
for code in codes.supported_codes {
|
for code in codes.supported_codes {
|
||||||
cache::add_code(code).expect("Error when adding code to cache");
|
cache::add_code(code).expect("Error when adding code to cache");
|
||||||
}
|
}
|
||||||
return Ok(Status::OK);
|
return Ok(Status::OK);
|
||||||
|
} else {
|
||||||
|
let err: Err = serde_json::from_str(&response.text()?).expect("Error when deserializng");
|
||||||
|
if err.error_type == "invalid-key" {
|
||||||
|
return Ok(Status::INVALID);
|
||||||
|
} else if err.error_type == "quota-reached" {
|
||||||
|
return Ok(Status::LIMIT);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Ok(Status::ERROR)
|
||||||
}
|
}
|
||||||
|
|
Reference in New Issue