2017-01-25 18:51:41 +01:00
|
|
|
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
|
2016-04-14 20:38:48 +02:00
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-05-17 16:55:08 +02:00
|
|
|
use serde::Serialize;
|
|
|
|
use serde_json;
|
2017-10-05 12:35:01 +02:00
|
|
|
use hyper::{self, mime, StatusCode};
|
2017-07-11 12:23:46 +02:00
|
|
|
|
2016-07-07 09:42:49 +02:00
|
|
|
use handlers::{ContentHandler, EchoHandler};
|
2016-04-14 20:38:48 +02:00
|
|
|
|
2017-10-05 12:35:01 +02:00
|
|
|
pub fn empty() -> hyper::Response {
|
|
|
|
ContentHandler::ok("".into(), mime::TEXT_PLAIN).into()
|
2016-10-22 15:21:41 +02:00
|
|
|
}
|
|
|
|
|
2017-10-05 12:35:01 +02:00
|
|
|
pub fn as_json<T: Serialize>(status: StatusCode, val: &T) -> hyper::Response {
|
2017-07-11 12:23:46 +02:00
|
|
|
let json = serde_json::to_string(val)
|
|
|
|
.expect("serialization to string is infallible; qed");
|
2017-10-05 12:35:01 +02:00
|
|
|
ContentHandler::new(status, json, mime::APPLICATION_JSON).into()
|
2017-07-11 12:23:46 +02:00
|
|
|
}
|
|
|
|
|
2017-10-05 12:35:01 +02:00
|
|
|
pub fn ping(req: hyper::Request) -> hyper::Response {
|
|
|
|
EchoHandler::new(req).into()
|
2016-04-14 20:38:48 +02:00
|
|
|
}
|
2016-07-07 09:42:49 +02:00
|
|
|
|
2017-10-05 12:35:01 +02:00
|
|
|
pub fn not_found() -> hyper::Response {
|
|
|
|
as_json(StatusCode::NotFound, &::api::types::ApiError {
|
|
|
|
code: "404".into(),
|
|
|
|
title: "Not Found".into(),
|
|
|
|
detail: "Resource you requested has not been found.".into(),
|
|
|
|
})
|
2016-07-07 09:42:49 +02:00
|
|
|
}
|