Skip to content

Commit

Permalink
[refactor]: reuse existing Scale util
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitry Balashov <a.marcius26@gmail.com>
  • Loading branch information
0x009922 committed Oct 20, 2023
1 parent e60b2dd commit e941a3c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
28 changes: 10 additions & 18 deletions cli/src/torii/routing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -431,25 +431,17 @@ fn update_metrics_gracefully(sumeragi: &SumeragiHandle) {
fn handle_status(
sumeragi: &SumeragiHandle,
accept: Option<impl AsRef<str>>,
subpath: &warp::path::Tail,
) -> Result<reply::Response> {
tail: &warp::path::Tail,
) -> Result<Response> {
use eyre::ContextCompat;
const PARITY_SCALE_MIME: &'_ str = "application/x-parity-scale";

update_metrics_gracefully(sumeragi);
let status = Status::from(&sumeragi.metrics());

let subpath = subpath.as_str();
if subpath.is_empty() {
if accept.is_some_and(|x| x.as_ref() == PARITY_SCALE_MIME) {
let body: hyper::Body = status.encode().into();

warp::http::Response::builder()
.status(StatusCode::OK)
.header(warp::http::header::CONTENT_TYPE, PARITY_SCALE_MIME)
.body(body)
.wrap_err("Failed to build response body")
.map_err(Error::StatusFailure)
let tail = tail.as_str();
if tail.is_empty() {
if accept.is_some_and(|x| x.as_ref() == PARITY_SCALE_MIME_TYPE) {
Ok(Scale(status).into_response())
} else {
Ok(reply::json(&status).into_response())
}
Expand All @@ -463,12 +455,12 @@ fn handle_status(
.wrap_err("Failed to serialize JSON")
.map_err(Error::StatusFailure)?;

let reply = subpath
let reply = tail
.split('/')
.try_fold(&value, serde_json::Value::get)
.wrap_err_with(|| eyre!("Path not found: \"{}\"", subpath))
.map(|segment| reply::json(segment).into_response())
.map_err(Error::StatusSegmentNotFound)?;
.wrap_err_with(|| eyre!("Path not found: \"{}\"", tail))
.map_err(Error::StatusSegmentNotFound)
.map(|segment| reply::json(segment).into_response())?;

Ok(reply)
}
Expand Down
20 changes: 17 additions & 3 deletions cli/src/torii/utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
use std::convert::Infallible;

use iroha_version::prelude::*;
use warp::{hyper::body::Bytes, reply::Response, Filter, Rejection, Reply};
use warp::{
http::{header::CONTENT_TYPE, HeaderValue},
hyper::body::Bytes,
reply::Response,
Filter, Rejection, Reply,
};

/// Structure for empty response body
#[derive(Clone, Copy)]
Expand All @@ -13,12 +18,21 @@ impl Reply for Empty {
}
}

/// Structure for response in scale codec in body
/// MIME used in Torii for SCALE encoding
// note: no elegant way to associate it with generic `Scale<T>`
pub const PARITY_SCALE_MIME_TYPE: &'_ str = "application/x-parity-scale";

/// Structure to reply using SCALE encoding
pub struct Scale<T>(pub T);

impl<T: Encode + Send> Reply for Scale<T> {
fn into_response(self) -> Response {
Response::new(self.0.encode().into())
let mut res = Response::new(self.0.encode().into());
res.headers_mut().insert(
CONTENT_TYPE,
HeaderValue::from_static(PARITY_SCALE_MIME_TYPE),
);
res
}
}

Expand Down

0 comments on commit e941a3c

Please sign in to comment.