kpcli: client: adds printing of conversations
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
extern crate hyper;
|
||||
extern crate serde;
|
||||
|
||||
use std::{path::PathBuf, str};
|
||||
use std::{ffi::OsString, path::PathBuf, str};
|
||||
use log::{error};
|
||||
|
||||
use hyper::{Body, Client, Method, Request, Uri};
|
||||
@@ -34,6 +34,21 @@ pub enum Error {
|
||||
DecodeError,
|
||||
}
|
||||
|
||||
impl std::error::Error for Error {
|
||||
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
||||
match self {
|
||||
Error::HTTPError(ref err) => Some(err),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for Error {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{:?}", self)
|
||||
}
|
||||
}
|
||||
|
||||
impl From <hyper::Error> for Error {
|
||||
fn from(err: hyper::Error) -> Error {
|
||||
Error::HTTPError(err)
|
||||
@@ -75,12 +90,12 @@ impl APIInterface for HTTPAPIClient {
|
||||
type Error = Error;
|
||||
|
||||
async fn get_version(&mut self) -> Result<String, Self::Error> {
|
||||
let version: String = self.request("/version", Method::GET).await?;
|
||||
let version: String = self.request("version", Method::GET).await?;
|
||||
Ok(version)
|
||||
}
|
||||
|
||||
async fn get_conversations(&mut self) -> Result<Vec<Conversation>, Self::Error> {
|
||||
let conversations: Vec<Conversation> = self.request("/conversations", Method::GET).await?;
|
||||
let conversations: Vec<Conversation> = self.request("conversations", Method::GET).await?;
|
||||
Ok(conversations)
|
||||
}
|
||||
|
||||
@@ -91,7 +106,7 @@ impl APIInterface for HTTPAPIClient {
|
||||
}
|
||||
|
||||
let body = || -> Body { serde_json::to_string(&credentials).unwrap().into() };
|
||||
let token: AuthResponse = self.request_with_body_retry("/authenticate", Method::POST, body, false).await?;
|
||||
let token: AuthResponse = self.request_with_body_retry("authenticate", Method::POST, body, false).await?;
|
||||
let token = JwtToken::new(&token.jwt).map_err(|_| Error::DecodeError)?;
|
||||
self.auth_token = Some(token.clone());
|
||||
Ok(token)
|
||||
@@ -140,6 +155,8 @@ impl HTTPAPIClient {
|
||||
use hyper::StatusCode;
|
||||
|
||||
let uri = self.uri_for_endpoint(endpoint);
|
||||
log::debug!("Requesting {:?} {:?}", method, uri);
|
||||
|
||||
let build_request = move |auth: &Option<JwtToken>| {
|
||||
let body = body_fn();
|
||||
Request::builder()
|
||||
@@ -152,6 +169,9 @@ impl HTTPAPIClient {
|
||||
|
||||
let request = build_request(&self.auth_token);
|
||||
let mut response = self.client.request(request).await?;
|
||||
|
||||
log::debug!("-> Response: {:}", response.status());
|
||||
|
||||
match response.status() {
|
||||
StatusCode::OK => { /* cool */ },
|
||||
|
||||
@@ -194,13 +214,6 @@ impl HTTPAPIClient {
|
||||
|
||||
mod test {
|
||||
use super::*;
|
||||
use ctor::ctor;
|
||||
|
||||
#[ctor]
|
||||
fn init() {
|
||||
pretty_env_logger::init();
|
||||
log::set_max_level(log::LevelFilter::Trace);
|
||||
}
|
||||
|
||||
fn local_mock_client() -> HTTPAPIClient {
|
||||
let base_url = "http://localhost:5738".parse().unwrap();
|
||||
|
||||
Reference in New Issue
Block a user