Finish custom JSON response handler

This commit is contained in:
Paul Hauner
2021-09-24 09:51:35 +10:00
parent cb5e33d53c
commit 95e9407cd9
2 changed files with 23 additions and 35 deletions

View File

@@ -17,7 +17,7 @@ pub enum Error {
Json(serde_json::Error), Json(serde_json::Error),
ServerMessage(String), ServerMessage(String),
Eip155Error, Eip155Error,
NoResultField, NoResultOrError,
} }
impl From<reqwest::Error> for Error { impl From<reqwest::Error> for Error {

View File

@@ -1,8 +1,8 @@
use super::*; use super::*;
use async_trait::async_trait; use async_trait::async_trait;
use eth1::http::{response_result_or_error, send_rpc_request, EIP155_ERROR_STR}; use eth1::http::{response_result_or_error, send_rpc_request, EIP155_ERROR_STR};
use reqwest::header::CONTENT_TYPE;
pub use reqwest::Client; pub use reqwest::Client;
use reqwest::{header::CONTENT_TYPE, ClientBuilder, StatusCode};
use sensitive_url::SensitiveUrl; use sensitive_url::SensitiveUrl;
use serde::{de::DeserializeOwned, Deserialize, Serialize}; use serde::{de::DeserializeOwned, Deserialize, Serialize};
use serde_json::json; use serde_json::json;
@@ -43,7 +43,7 @@ impl HttpJsonRpc {
params: serde_json::Value, params: serde_json::Value,
timeout: Duration, timeout: Duration,
) -> Result<T, Error> { ) -> Result<T, Error> {
let body = JsonBody { let body = JsonRequestBody {
jsonrpc: "2.0", jsonrpc: "2.0",
method: method, method: method,
params, params,
@@ -52,7 +52,7 @@ impl HttpJsonRpc {
let url: &str = self.url.as_ref(); let url: &str = self.url.as_ref();
let body: serde_json::Value = self let body: JsonResponseBody = self
.client .client
.post(url) .post(url)
.timeout(timeout) .timeout(timeout)
@@ -64,38 +64,17 @@ impl HttpJsonRpc {
.json() .json()
.await?; .await?;
if let Some(error) = body.get("error").map(|e| e.get("message")).flatten() { match (body.result, body.error) {
let error = error.to_string(); (Some(result), None) => serde_json::from_value(result).map_err(Into::into),
if error.contains(EIP155_ERROR_STR) { (_, Some(error)) => {
Err(Error::Eip155Error) if error.contains(EIP155_ERROR_STR) {
} else { Err(Error::Eip155Error)
Err(Error::ServerMessage(error)) } else {
Err(Error::ServerMessage(error))
}
} }
} else { (None, None) => Err(Error::NoResultOrError),
body.get("result").cloned().ok_or(Error::NoResultField)
} }
/*
let encoding = response
.headers()
.get(CONTENT_TYPE)
.ok_or("No content-type header in response")?
.to_str()
.map(|s| s.to_string())
.map_err(|e| format!("Failed to parse content-type header: {}", e))?;
response
.bytes()
.map_err(|e| format!("Failed to receive body: {:?}", e))
.await
.and_then(move |bytes| match encoding.as_str() {
"application/json" => Ok(bytes),
"application/json; charset=utf-8" => Ok(bytes),
other => Err(format!("Unsupported encoding: {}", other)),
})
.map(|bytes| String::from_utf8_lossy(&bytes).into_owned())
.map_err(|e| format!("Failed to receive body: {:?}", e))
*/
} }
} }
@@ -224,13 +203,22 @@ impl EngineApi for HttpJsonRpc {
#[derive(Debug, PartialEq, Serialize, Deserialize)] #[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename = "camelCase")] #[serde(rename = "camelCase")]
struct JsonBody<'a> { struct JsonRequestBody<'a> {
jsonrpc: &'a str, jsonrpc: &'a str,
method: &'a str, method: &'a str,
params: serde_json::Value, params: serde_json::Value,
id: u32, id: u32,
} }
#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename = "camelCase")]
struct JsonResponseBody {
jsonrpc: String,
error: Option<String>,
result: Option<serde_json::Value>,
id: u32,
}
#[derive(Debug, PartialEq, Serialize, Deserialize)] #[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename = "camelCase")] #[serde(rename = "camelCase")]
struct JsonPreparePayloadRequest { struct JsonPreparePayloadRequest {