fn response_to_api_result<R>(mut response: Response) -> api::Result<R>
    where
        R: DeserializeOwned + 'static,
    {
        trace!("Response status: {}", response.status());

        fn extract_description(body: &str) -> Option<String> {
            trace!("Error: {}", body);
            match serde_json::from_str::<serde_json::Value>(body).ok()? {
                serde_json::Value::Object(ref object) if object.contains_key("description") => {
                    Some(object["description"].as_str()?.to_owned())
                }
                serde_json::Value::String(string) => Some(string),
                _ => None,
            }
        }

        fn error(mut response: Response) -> String {
            let body = response.text().expect("Unable to get response text");
            extract_description(&body).unwrap_or(body)
        }

        match response.status() {
            StatusCode::OK => Ok({
                let body = response.text().expect("Unable to get response text");
                eprintln!("error body: {}", body);
                serde_json::from_str(&body).expect("Unable to deserialize body")
            }),
            StatusCode::FORBIDDEN => Err(api::Error::Unauthorized),
            StatusCode::BAD_REQUEST => Err(api::Error::BadRequest(400, error(response))),
            StatusCode::NOT_FOUND => Err(api::Error::NotFound(404, error(response))),
            s if s.is_server_error() => {
                Err(api::Error::InternalError(500, format_err!("{}", error(response))))
            }
            s => {
                let body = response.text().expect("Unable to get response text");
                eprintln!("error body: {}", body);
                panic!(
                    "Received non-error response status: {} ({})",
                    s.as_u16(),
                    error(response)
                )
            }
        }
    }
/// Converts reqwest Response to api::Result.
Converts reqwest Response to api::Result.
Converts reqwest Response to api :: Result .
fn response_to_api_result<R>(mut response: Response) -> api::Result<R>
    where
        R: DeserializeOwned + 'static,
    {
        trace!("Response status: {}", response.status());
        fn extract_description(body: &str) -> Option<String> {
            trace!("Error: {}", body);
            match serde_json::from_str::<serde_json::Value>(body).ok()? {
                serde_json::Value::Object(ref object) if object.contains_key("description") => {
                    Some(object["description"].as_str()?.to_owned())
                }
                serde_json::Value::String(string) => Some(string),
                _ => None,
            }
        }
        fn error(mut response: Response) -> String {
            let body = response.text().expect("Unable to get response text");
            extract_description(&body).unwrap_or(body)
        }
        match response.status() {
            StatusCode::OK => Ok({
                let body = response.text().expect("Unable to get response text");
                eprintln!("error body: {}", body);
                serde_json::from_str(&body).expect("Unable to deserialize body")
            }),
            StatusCode::FORBIDDEN => Err(api::Error::Unauthorized),
            StatusCode::BAD_REQUEST => Err(api::Error::BadRequest(400, error(response))),
            StatusCode::NOT_FOUND => Err(api::Error::NotFound(404, error(response))),
            s if s.is_server_error() => {
                Err(api::Error::InternalError(500, format_err!("{}", error(response))))
            }
            s => {
                let body = response.text().expect("Unable to get response text");
                eprintln!("error body: {}", body);
                panic!(
                    "Received non-error response status: {} ({})",
                    s.as_u16(),
                    error(response)
                )
            }
        }
    }
fn response_to_api_result < R > ( mut response : Response ) -> api :: Result < R > where R : DeserializeOwned + ' static , { trace ! ( " " response status ( ) ) ; fn extract_description ( body : & str ) -> Option < String > { trace ! ( " " body ) ; match serde_json :: from_str :: < serde_json :: Value > ( body ) . ok ( ) ? { serde_json :: Value :: Object ( ref object ) if object . contains_key ( " " ) => { Some ( object [ " " ] . as_str ( ) ? . to_owned ( ) ) } serde_json :: Value :: String ( string ) => Some ( string ) , _ => None , } } fn error ( mut response : Response ) -> String { let body = response . text ( ) . expect ( " " ) ; extract_description ( & body ) . unwrap_or ( body ) } match response . status ( ) { StatusCode :: OK => Ok ( { let body = response . text ( ) . expect ( " " ) ; eprintln ! ( " " body ) ; serde_json :: from_str ( & body ) . expect ( " " ) } ) , StatusCode :: FORBIDDEN => Err ( api :: Error :: Unauthorized ) , StatusCode :: BAD_REQUEST => Err ( api :: Error :: BadRequest ( 400 , error ( response ) ) ) , StatusCode :: NOT_FOUND => Err ( api :: Error :: NotFound ( 404 , error ( response ) ) ) , s if s . is_server_error ( ) => { Err ( api :: Error :: InternalError ( 500 , format_err ! ( " " error ( response ) ) ) ) } s => { let body = response . text ( ) . expect ( " " ) ; eprintln ! ( " " body ) ; panic ! ( " " s as_u16 ( ) error ( response ) ) } } }
Converts reqwest Response to api::Result.
Converts reqwest Response to api :: Result .
response
Response
response
Response