Private
Public Access
1
0

kpcli: client: adds printing of conversations

This commit is contained in:
2024-11-10 19:40:39 -08:00
parent 6b9f528cbf
commit 0e8b8f339a
9 changed files with 492 additions and 59 deletions

View File

@@ -10,10 +10,10 @@ async-trait = "0.1.80"
base64 = "0.22.1"
chrono = "0.4.38"
ctor = "0.2.8"
env_logger = "0.11.5"
hyper = { version = "0.14", features = ["full"] }
hyper-tls = "0.5.0"
log = { version = "0.4.21", features = [] }
pretty_env_logger = "0.5.0"
serde = { version = "1.0.152", features = ["derive"] }
serde_json = "1.0.91"
serde_plain = "1.0.2"

View File

@@ -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();

View File

@@ -1,7 +1,21 @@
mod api;
pub mod api;
pub mod model;
pub use self::api::APIInterface;
use ctor::ctor;
#[cfg(test)]
pub mod tests;
extern crate env_logger;
fn initialize_logging() {
env_logger::Builder::from_default_env()
.format_timestamp_secs()
.init();
}
#[ctor]
fn init() {
initialize_logging();
}