Skip to content
Snippets Groups Projects
Commit bf55a308 authored by DeathwingTheBoss's avatar DeathwingTheBoss
Browse files

Accept API calls without params field properly

parent 0765b8ec
No related branches found
No related tags found
No related merge requests found
......@@ -585,7 +585,7 @@ checksum = "0688c2a7f92e427f44895cd63841bff7b29f8d7a1648b9e7e07a4a365b2e1257"
[[package]]
name = "drone"
version = "0.2.4"
version = "0.2.5"
dependencies = [
"actix-cors",
"actix-web",
......
[package]
name = "drone"
version = "0.2.4"
version = "0.2.5"
edition = "2021"
authors = ["Deathwing <hi@deathwing.me>"]
description = "A caching reverse-proxy application for the Hive blockchain."
......
......@@ -53,12 +53,8 @@ struct APIRequest {
jsonrpc: String,
id: u32,
method: String,
#[serde(default = "default_params")]
params: Value,
}
fn default_params() -> Value {
Value::Object(serde_json::Map::new())
#[serde(skip_serializing_if = "Option::is_none")]
params: Option<Value>,
}
#[derive(Debug, Deserialize)]
......@@ -125,7 +121,7 @@ async fn handle_request(
let method = request.method.as_str();
if method == "call" {
if let Some(params) = request.params.as_array() {
if let Some(params) = request.params.as_ref().and_then(Value::as_array) {
if params.len() > 2 {
if let Some(method) = params[0].as_str() {
let format_new_method = format!("{}.{}", method, params[1].as_str().unwrap());
......@@ -134,7 +130,7 @@ async fn handle_request(
jsonrpc: "2.0".to_string(),
id: request.id,
method: format_new_method,
params: new_params,
params: Some(new_params),
};
return handle_request(&new_request, data, client_ip).await;
}
......@@ -144,10 +140,17 @@ async fn handle_request(
// Get humantime for logging.
let human_timestamp = humantime::format_rfc3339_seconds(std::time::SystemTime::now());
let formatted_log = format!(
"Timestamp: {} || IP: {} || Request Method: {} || Request Params: {}",
human_timestamp, client_ip, request.method, request.params,
);
let formatted_log = if let Some(params) = &request.params {
format!(
"Timestamp: {} || IP: {} || Request Method: {} || Request Params: {}",
human_timestamp, client_ip, request.method, params,
)
} else {
format!(
"Timestamp: {} || IP: {} || Request Method: {}",
human_timestamp, client_ip, request.method,
)
};
println!("{}", formatted_log);
......@@ -195,8 +198,9 @@ async fn handle_request(
_anything_else => Endpoints::HAF,
};
// Check if the call is in the cache. If it is, return only the result while keeping rest of the response the same.
if let Some(cached_call) = data.cache.lock().unwrap().get(&request.params.to_string()) {
// Check if the call is in the cache. If it is, return only the result while keeping the rest of the response the same.
let params_str = request.params.as_ref().map_or("[]".to_string(), |v: &Value| v.to_string());
if let Some(cached_call) = data.cache.lock().unwrap().get(&params_str) {
// build result with data from cache and response
let result = cached_call.clone();
return Ok(APICallResponse {
......@@ -207,6 +211,7 @@ async fn handle_request(
});
}
// Send the request to the endpoints.
let res = match client
.post(endpoints.choose_endpoint(&data))
......@@ -268,10 +273,11 @@ async fn handle_request(
}
if DRONE_CACHEABLE_METHODS.contains(&request.method.as_str()) && cacheable {
let params_str = request.params.as_ref().map_or("[]".to_string(), |v| v.to_string());
data.cache
.lock()
.unwrap()
.insert(request.params.to_string(), json_body.clone());
.insert(params_str, json_body.clone());
}
Ok(APICallResponse {
jsonrpc: request.jsonrpc.clone(),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment