use std::borrow::Borrow;
use std::convert::Infallible;
use crate::schema::structs::ErrorInfo;
use crate::util;
use futures::future::AndThen;
use serde::de::DeserializeOwned;
use serde::Serialize;
use std::future::Future;
use std::sync::Arc;
use std::time::Duration;
use itertools::Itertools;
use prost::Message;
use serde::__private::de::Borrowed;
use warp::reply::Json;
use warp::{Filter, Rejection};
use redgold_schema::{error_info, ProtoHashable, SafeOption};
use redgold_schema::structs::{GetPeersInfoRequest, GetPeersInfoResponse, Request, Response};
use crate::node_config::NodeConfig;
use crate::util::lang_util::SameResult;
pub mod control_api;
#[cfg(not(target_arch = "wasm32"))]
pub mod p2p_io;
pub mod public_api;
pub mod rosetta;
pub mod faucet;
pub mod lp2p;
pub mod hash_query;
pub mod udp_api;
pub mod about;
pub mod explorer;
#[derive(Clone)]
pub struct RgHttpClient {
pub url: String,
pub port: u16,
pub timeout: Duration,
}
impl RgHttpClient {
pub fn new(url: String, port: u16) -> Self {
Self {
url,
port,
timeout: Duration::from_secs(60),
}
}
#[allow(dead_code)]
fn formatted_url(&self) -> String {
return "http://".to_owned() + &*self.url.clone() + ":" + &*self.port.to_string();
}
#[allow(dead_code)]
pub async fn json_post_request<Req: Serialize + ?Sized, Resp: DeserializeOwned>(
&self,
r: &Req,
) -> Result<Resp, ErrorInfo> {
self.json_post(r, "request".into()).await
}
#[allow(dead_code)]
pub async fn json_post<Req: Serialize + ?Sized, Resp: DeserializeOwned>(
&self,
r: &Req,
endpoint: String,
) -> Result<Resp, ErrorInfo> {
use reqwest::ClientBuilder;
let client = ClientBuilder::new().timeout(self.timeout).build().unwrap();
let sent = client
.post(format!("{}/{}", self.formatted_url(), endpoint))
.json::<Req>(r)
.send();
let response = sent.await;
match response {
Ok(r) => {
let text = r.text().await
.map_err(|e| error_info(format!("{} {}", "Failed to get response text ", e.to_string())))?;
let resp = serde_json::from_str::<Resp>(&*text.clone())
.map_err(|e| error_info(format!("{} {}", e.to_string(), text)))?;
Ok(resp)
},
Err(e) => Err(error_info(e.to_string())),
}
}
#[allow(dead_code)]
pub async fn proto_post<Req: Sized + Message>(
&self,
r: &Req,
endpoint: String,
) -> Result<Response, ErrorInfo> {
use reqwest::ClientBuilder;
let client = ClientBuilder::new().timeout(self.timeout).build().unwrap();
let sent = client
.post(format!("{}/{}", self.formatted_url(), endpoint))
.body(r.encode_to_vec())
.send();
let response = sent.await.map_err(|e| ErrorInfo::error_info(
format!("Proto request failure: {}", e.to_string())))?;
let bytes = response.bytes().await.map_err(|e| ErrorInfo::error_info(
format!("Proto request bytes decode failure: {}", e.to_string())))?;
let vec = bytes.to_vec();
let deser = Response::deserialize(vec).map_err(|e| ErrorInfo::error_info(
format!("Proto request response decode failure: {}", e.to_string())))?;
Ok(deser)
}
pub async fn proto_post_request(&self, r: &mut Request, nc: Option<NodeConfig>) -> Result<Response, ErrorInfo> {
if let Some(nc) = nc {
r.with_metadata(nc.node_metadata());
r.with_auth(&nc.internal_mnemonic().active_keypair());
}
let result = self.proto_post(r, "request_proto".to_string()).await?;
result.as_error_info()?;
Ok(result)
}
pub async fn test_request<Req, Resp>(port: u16, req: &Req, endpoint: String) -> Result<Resp, ErrorInfo>
where
Req: Serialize + ?Sized,
Resp: DeserializeOwned
{
let client = RgHttpClient::new("localhost".into(), port);
tokio::time::sleep(Duration::from_secs(2)).await;
client.json_post::<Req, Resp>(&req, endpoint).await
}
pub async fn get_peers(&self) -> Result<Response, ErrorInfo> {
let mut req = Request::default();
req.get_peers_info_request = Some(GetPeersInfoRequest::default());
let response = self.proto_post_request(&mut req, None).await?;
Ok(response)
}
}
pub fn easy_post<T, ReqT, RespT, Fut, S>(
clonable: T,
endpoint: S,
handler: fn(T, ReqT) -> Fut,
length_limit_kb: u64
) -> impl Filter<Extract = (Result<RespT, ErrorInfo>,), Error = Rejection> + Clone
where T : Clone + Send,
ReqT : DeserializeOwned + Send + std::fmt::Debug + ?Sized + Serialize + Clone,
RespT : DeserializeOwned + Send + std::fmt::Debug,
Fut: Future<Output = Result<Result<RespT, ErrorInfo>, Rejection>> + Send,
S: Into<String> + Clone + Send
{
warp::post()
.and(warp::path !("account" / "balance"))
.and(warp::body::content_length_limit(1024 * length_limit_kb))
.map(move || clonable.clone())
.and(warp::body::json::<ReqT>())
.map(move |x, y: ReqT| {
let y2 = y.clone();
let ser = serde_json::to_string(&y2).unwrap_or("request ser failed".into());
log::debug!("Request endpoint: {} {} ", endpoint.clone().into(), ser);
(x, y)
})
.untuple_one()
.and_then(handler)
}
pub fn with_response_logger<Resp>(resp: Resp, endpoint: String) -> Resp
where Resp: ?Sized + Serialize + Clone {
let y2 = resp.clone();
let ser = serde_json::to_string(&y2).unwrap_or("response ser failed".into());
log::debug!("Response endpoint: {} {} ", endpoint.clone(), ser);
resp
}
pub fn with_response_logger_error<Resp, ErrT>(resp: Result<Resp, ErrT>, endpoint_i: String) -> Result<Resp, ErrT>
where Resp: ?Sized + Serialize + Clone,
ErrT: ?Sized + Serialize + Clone {
let endpoint = endpoint_i.clone();
let endpoint2 = endpoint_i.clone();
resp.map(move |r| {
let y2 = r.clone();
let ser = serde_json::to_string( & y2).unwrap_or("response ser failed".into());
log::debug ! ("Response success endpoint: {} {} ", endpoint.clone(), ser);
r
}).map_err(move |r| {
let y2 = r.clone();
let ser = serde_json::to_string( & y2).unwrap_or("response ser failed".into());
log::debug ! ("Response error endpoint: {} {} ", endpoint2.clone(), ser);
r
})
}
pub fn as_warp_json_response<T: Serialize, E: Serialize>(response: Result<T, E>) -> Result<Json, warp::reject::Rejection> {
Ok(response.map_err(|e| warp::reply::json(&e))
.map(|r| warp::reply::json(&r))
.combine()
)
}