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