2018-03-05 22:33:44 +03:00
|
|
|
// Copyright 2018 The Grin Developers
|
2017-05-26 03:21:56 +03:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
//! High level JSON/HTTP client API
|
|
|
|
|
2018-04-16 12:00:32 +03:00
|
|
|
use failure::{Fail, ResultExt};
|
2018-08-01 12:44:07 +03:00
|
|
|
use http::uri::{InvalidUri, Uri};
|
|
|
|
use hyper::header::{ACCEPT, USER_AGENT};
|
|
|
|
use hyper::rt::{Future, Stream};
|
|
|
|
use hyper::{Body, Client, Request};
|
2017-11-01 02:32:33 +03:00
|
|
|
use serde::{Deserialize, Serialize};
|
2017-05-26 03:21:56 +03:00
|
|
|
use serde_json;
|
2018-08-01 12:44:07 +03:00
|
|
|
|
|
|
|
use futures::future::{err, ok, Either};
|
|
|
|
use tokio_core::reactor::Core;
|
2017-05-26 03:21:56 +03:00
|
|
|
|
2018-04-16 12:00:32 +03:00
|
|
|
use rest::{Error, ErrorKind};
|
2017-05-26 03:21:56 +03:00
|
|
|
|
|
|
|
/// Helper function to easily issue a HTTP GET request against a given URL that
|
|
|
|
/// returns a JSON object. Handles request building, JSON deserialization and
|
|
|
|
/// response code checking.
|
|
|
|
pub fn get<'a, T>(url: &'a str) -> Result<T, Error>
|
2017-11-01 02:32:33 +03:00
|
|
|
where
|
|
|
|
for<'de> T: Deserialize<'de>,
|
2017-05-26 03:21:56 +03:00
|
|
|
{
|
2018-08-01 12:44:07 +03:00
|
|
|
let uri = url.parse::<Uri>().map_err::<Error, _>(|e: InvalidUri| {
|
|
|
|
e.context(ErrorKind::Argument(format!("Invalid url {}", url)))
|
|
|
|
.into()
|
|
|
|
})?;
|
|
|
|
let req = Request::builder()
|
|
|
|
.method("GET")
|
|
|
|
.uri(uri)
|
|
|
|
.header(USER_AGENT, "grin-client")
|
|
|
|
.header(ACCEPT, "application/json")
|
|
|
|
.body(Body::empty())
|
|
|
|
.map_err(|_e| ErrorKind::RequestError("Bad request".to_owned()))?;
|
|
|
|
|
|
|
|
handle_request(req)
|
2017-05-26 03:21:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Helper function to easily issue a HTTP POST request with the provided JSON
|
|
|
|
/// object as body on a given URL that returns a JSON object. Handles request
|
|
|
|
/// building, JSON serialization and deserialization, and response code
|
|
|
|
/// checking.
|
2018-08-01 12:44:07 +03:00
|
|
|
pub fn post<IN, OUT>(url: &str, input: &IN) -> Result<OUT, Error>
|
|
|
|
where
|
|
|
|
IN: Serialize,
|
|
|
|
for<'de> OUT: Deserialize<'de>,
|
|
|
|
{
|
|
|
|
let req = create_post_request(url, input)?;
|
|
|
|
handle_request(req)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Helper function to easily issue a HTTP POST request with the provided JSON
|
|
|
|
/// object as body on a given URL that returns nothing. Handles request
|
|
|
|
/// building, JSON serialization, and response code
|
|
|
|
/// checking.
|
|
|
|
pub fn post_no_ret<IN>(url: &str, input: &IN) -> Result<(), Error>
|
2017-11-01 02:32:33 +03:00
|
|
|
where
|
|
|
|
IN: Serialize,
|
2017-05-26 03:21:56 +03:00
|
|
|
{
|
2018-08-01 12:44:07 +03:00
|
|
|
let req = create_post_request(url, input)?;
|
|
|
|
send_request(req)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_post_request<IN>(url: &str, input: &IN) -> Result<Request<Body>, Error>
|
|
|
|
where
|
|
|
|
IN: Serialize,
|
|
|
|
{
|
|
|
|
let json = serde_json::to_string(input).context(ErrorKind::Internal(
|
2018-04-16 12:00:32 +03:00
|
|
|
"Could not serialize data to JSON".to_owned(),
|
|
|
|
))?;
|
2018-08-01 12:44:07 +03:00
|
|
|
let uri = url.parse::<Uri>().map_err::<Error, _>(|e: InvalidUri| {
|
|
|
|
e.context(ErrorKind::Argument(format!("Invalid url {}", url)))
|
|
|
|
.into()
|
|
|
|
})?;
|
|
|
|
Request::builder()
|
|
|
|
.method("POST")
|
|
|
|
.uri(uri)
|
|
|
|
.header(USER_AGENT, "grin-client")
|
|
|
|
.header(ACCEPT, "application/json")
|
|
|
|
.body(json.into())
|
|
|
|
.map_err::<Error, _>(|e| {
|
|
|
|
e.context(ErrorKind::RequestError("Bad request".to_owned()))
|
|
|
|
.into()
|
|
|
|
})
|
2017-05-26 03:21:56 +03:00
|
|
|
}
|
|
|
|
|
2018-08-01 12:44:07 +03:00
|
|
|
fn handle_request<T>(req: Request<Body>) -> Result<T, Error>
|
|
|
|
where
|
|
|
|
for<'de> T: Deserialize<'de>,
|
|
|
|
{
|
|
|
|
let data = send_request(req)?;
|
|
|
|
serde_json::from_str(&data).map_err(|e| {
|
|
|
|
e.context(ErrorKind::ResponseError("Cannot parse response".to_owned()))
|
|
|
|
.into()
|
|
|
|
})
|
2017-05-26 03:21:56 +03:00
|
|
|
}
|
2018-01-07 02:27:21 +03:00
|
|
|
|
2018-08-01 12:44:07 +03:00
|
|
|
fn send_request(req: Request<Body>) -> Result<String, Error> {
|
|
|
|
let mut event_loop = Core::new().unwrap();
|
|
|
|
let client = Client::new();
|
|
|
|
|
|
|
|
let task = client
|
|
|
|
.request(req)
|
|
|
|
.map_err(|_e| ErrorKind::RequestError("Cannot make request".to_owned()))
|
|
|
|
.and_then(|resp| {
|
|
|
|
if !resp.status().is_success() {
|
|
|
|
Either::A(err(ErrorKind::RequestError(
|
|
|
|
"Wrong response code".to_owned(),
|
|
|
|
)))
|
|
|
|
} else {
|
|
|
|
Either::B(
|
|
|
|
resp.into_body()
|
|
|
|
.map_err(|_e| {
|
|
|
|
ErrorKind::RequestError("Cannot read response body".to_owned())
|
|
|
|
})
|
|
|
|
.concat2()
|
|
|
|
.and_then(|ch| ok(String::from_utf8_lossy(&ch.to_vec()).to_string())),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Ok(event_loop.run(task)?)
|
2018-01-07 02:27:21 +03:00
|
|
|
}
|