2017-05-26 03:21:56 +03:00
|
|
|
// Copyright 2016 The Grin Developers
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
|
|
|
|
use hyper;
|
|
|
|
use hyper::client::Response;
|
2017-06-13 02:41:27 +03:00
|
|
|
use hyper::status::{StatusClass, StatusCode};
|
2017-11-01 02:32:33 +03:00
|
|
|
use serde::{Deserialize, Serialize};
|
2017-05-26 03:21:56 +03:00
|
|
|
use serde_json;
|
2018-01-07 02:27:21 +03:00
|
|
|
use std::io::Read;
|
2017-05-26 03:21:56 +03:00
|
|
|
|
|
|
|
use rest::Error;
|
|
|
|
|
|
|
|
/// 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
|
|
|
{
|
|
|
|
let client = hyper::Client::new();
|
|
|
|
let res = check_error(client.get(url).send())?;
|
2017-11-01 02:32:33 +03:00
|
|
|
serde_json::from_reader(res).map_err(|e| {
|
|
|
|
Error::Internal(format!("Server returned invalid JSON: {}", e))
|
|
|
|
})
|
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.
|
2017-11-01 21:32:34 +03:00
|
|
|
pub fn post<'a, IN>(url: &'a str, input: &IN) -> Result<(), Error>
|
2017-11-01 02:32:33 +03:00
|
|
|
where
|
|
|
|
IN: Serialize,
|
2017-05-26 03:21:56 +03:00
|
|
|
{
|
2017-11-01 02:32:33 +03:00
|
|
|
let in_json = serde_json::to_string(input).map_err(|e| {
|
|
|
|
Error::Internal(format!("Could not serialize data to JSON: {}", e))
|
|
|
|
})?;
|
2017-05-26 03:21:56 +03:00
|
|
|
let client = hyper::Client::new();
|
2017-11-01 21:32:34 +03:00
|
|
|
let _res = check_error(client.post(url).body(&mut in_json.as_bytes()).send())?;
|
|
|
|
Ok(())
|
2017-05-26 03:21:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// convert hyper error and check for non success response codes
|
|
|
|
fn check_error(res: hyper::Result<Response>) -> Result<Response, Error> {
|
|
|
|
if let Err(e) = res {
|
|
|
|
return Err(Error::Internal(format!("Error during request: {}", e)));
|
|
|
|
}
|
2018-01-07 02:27:21 +03:00
|
|
|
let mut response = res.unwrap();
|
2017-05-26 03:21:56 +03:00
|
|
|
match response.status.class() {
|
|
|
|
StatusClass::Success => Ok(response),
|
2018-01-07 02:27:21 +03:00
|
|
|
StatusClass::ServerError => {
|
|
|
|
Err(Error::Internal(format!("Server error: {}", err_msg(&mut response))))
|
|
|
|
}
|
2017-11-01 02:32:33 +03:00
|
|
|
StatusClass::ClientError => if response.status == StatusCode::NotFound {
|
|
|
|
Err(Error::NotFound)
|
|
|
|
} else {
|
2018-01-07 02:27:21 +03:00
|
|
|
Err(Error::Argument(format!("Argument error: {}", err_msg(&mut response))))
|
2017-11-01 02:32:33 +03:00
|
|
|
},
|
2017-05-26 03:21:56 +03:00
|
|
|
_ => Err(Error::Internal(format!("Unrecognized error."))),
|
|
|
|
}
|
|
|
|
}
|
2018-01-07 02:27:21 +03:00
|
|
|
|
|
|
|
fn err_msg(resp: &mut Response) -> String {
|
|
|
|
let mut msg = String::new();
|
|
|
|
if let Err(_) = resp.read_to_string(&mut msg) {
|
|
|
|
"<no message>".to_owned()
|
|
|
|
} else {
|
|
|
|
msg
|
|
|
|
}
|
|
|
|
}
|