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

- Update error structure to support JSON-RPC 2.0 specification

- Add follow_api and tags_api to support Beem (python library)
parent f4ebbddb
No related branches found
No related tags found
No related merge requests found
...@@ -574,7 +574,7 @@ checksum = "0688c2a7f92e427f44895cd63841bff7b29f8d7a1648b9e7e07a4a365b2e1257" ...@@ -574,7 +574,7 @@ checksum = "0688c2a7f92e427f44895cd63841bff7b29f8d7a1648b9e7e07a4a365b2e1257"
[[package]] [[package]]
name = "drone" name = "drone"
version = "0.2.2" version = "0.2.3"
dependencies = [ dependencies = [
"actix-cors", "actix-cors",
"actix-web", "actix-web",
......
[package] [package]
name = "drone" name = "drone"
version = "0.2.2" version = "0.2.3"
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."
......
...@@ -81,6 +81,8 @@ impl Serialize for ErrorField { ...@@ -81,6 +81,8 @@ impl Serialize for ErrorField {
// Structure for the error response. // Structure for the error response.
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
struct ErrorStructure { struct ErrorStructure {
jsonrpc: String,
id: u32,
code: i32, code: i32,
message: String, message: String,
error: ErrorField, error: ErrorField,
...@@ -166,6 +168,8 @@ async fn handle_request( ...@@ -166,6 +168,8 @@ async fn handle_request(
"condenser_api.get_replies_by_last_update" => Endpoints::HIVEMIND, "condenser_api.get_replies_by_last_update" => Endpoints::HIVEMIND,
"condenser_api.get_reblogged_by" => Endpoints::HIVEMIND, "condenser_api.get_reblogged_by" => Endpoints::HIVEMIND,
_bridge_endpoint if method.starts_with("bridge.") => Endpoints::HIVEMIND, _bridge_endpoint if method.starts_with("bridge.") => Endpoints::HIVEMIND,
_follow_api_endpoint if method.starts_with("follow_api.") => Endpoints::HIVEMIND, // Remove when beem is updated. (Deprecated)
_tags_api_endpoint if method.starts_with("tags_api.") => Endpoints::HIVEMIND, // Remove when beem is updated. (Deprecated)
_anything_else => Endpoints::HAF, _anything_else => Endpoints::HAF,
}; };
...@@ -191,6 +195,8 @@ async fn handle_request( ...@@ -191,6 +195,8 @@ async fn handle_request(
Ok(response) => response, Ok(response) => response,
Err(err) => { Err(err) => {
return Err(ErrorStructure { return Err(ErrorStructure {
jsonrpc: request.jsonrpc.clone(),
id : request.id,
code: -32700, code: -32700,
message: format!("Unable to send request to endpoint."), message: format!("Unable to send request to endpoint."),
error: ErrorField::Message(err.to_string()), error: ErrorField::Message(err.to_string()),
...@@ -201,6 +207,8 @@ async fn handle_request( ...@@ -201,6 +207,8 @@ async fn handle_request(
Ok(text) => text, Ok(text) => text,
Err(err) => { Err(err) => {
return Err(ErrorStructure { return Err(ErrorStructure {
jsonrpc: request.jsonrpc.clone(),
id : request.id,
code: -32600, code: -32600,
message: format!("Received an invalid response from the endpoint."), message: format!("Received an invalid response from the endpoint."),
error: ErrorField::Message(err.to_string()), error: ErrorField::Message(err.to_string()),
...@@ -211,6 +219,8 @@ async fn handle_request( ...@@ -211,6 +219,8 @@ async fn handle_request(
Ok(parsed) => parsed, Ok(parsed) => parsed,
Err(err) => { Err(err) => {
return Err(ErrorStructure { return Err(ErrorStructure {
jsonrpc: request.jsonrpc.clone(),
id : request.id,
code: -32602, code: -32602,
message: format!("Unable to parse endpoint data."), message: format!("Unable to parse endpoint data."),
error: ErrorField::Message(err.to_string()), error: ErrorField::Message(err.to_string()),
...@@ -219,6 +229,8 @@ async fn handle_request( ...@@ -219,6 +229,8 @@ async fn handle_request(
}; };
if json_body["error"].is_object() { if json_body["error"].is_object() {
return Err(ErrorStructure { return Err(ErrorStructure {
jsonrpc: request.jsonrpc.clone(),
id : request.id,
code: -32700, code: -32700,
message: format!("Endpoint returned an error."), message: format!("Endpoint returned an error."),
error: ErrorField::Object(json_body["error"].clone()), error: ErrorField::Object(json_body["error"].clone()),
...@@ -262,6 +274,8 @@ async fn api_call( ...@@ -262,6 +274,8 @@ async fn api_call(
Ok(ip) => ip, Ok(ip) => ip,
Err(_) => { Err(_) => {
return HttpResponse::InternalServerError().json(ErrorStructure { return HttpResponse::InternalServerError().json(ErrorStructure {
jsonrpc: "2.0".to_string(),
id: 0,
code: -32000, code: -32000,
message: "Internal Server Error".to_string(), message: "Internal Server Error".to_string(),
error: ErrorField::Message("Invalid Cloudflare Proxy Header.".to_string()), error: ErrorField::Message("Invalid Cloudflare Proxy Header.".to_string()),
...@@ -288,6 +302,8 @@ async fn api_call( ...@@ -288,6 +302,8 @@ async fn api_call(
let mut responses = Vec::new(); let mut responses = Vec::new();
if requests.len() > 100 { if requests.len() > 100 {
return HttpResponse::InternalServerError().json(ErrorStructure { return HttpResponse::InternalServerError().json(ErrorStructure {
jsonrpc: "2.0".to_string(),
id: 0,
code: -32600, code: -32600,
message: "Request parameter error.".to_string(), message: "Request parameter error.".to_string(),
error: ErrorField::Message( error: ErrorField::Message(
......
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