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

48
kpcli/src/client_cli.rs Normal file
View File

@@ -0,0 +1,48 @@
use kordophone::APIInterface;
use kordophone::api::http_client::HTTPAPIClient;
use kordophone::api::http_client::Credentials;
use dotenv;
use crate::printers::ConversationPrinter;
pub struct ClientCli {
api: HTTPAPIClient,
}
impl ClientCli {
pub fn new() -> Self {
dotenv::dotenv().ok();
// read from env
let base_url = std::env::var("KORDOPHONE_API_URL")
.expect("KORDOPHONE_API_URL must be set");
let credentials = Credentials {
username: std::env::var("KORDOPHONE_USERNAME")
.expect("KORDOPHONE_USERNAME must be set"),
password: std::env::var("KORDOPHONE_PASSWORD")
.expect("KORDOPHONE_PASSWORD must be set"),
};
let api = HTTPAPIClient::new(base_url.parse().unwrap(), credentials.into());
Self { api: api }
}
pub async fn print_version(&mut self) -> Result<(), Box<dyn std::error::Error>> {
let version = self.api.get_version().await?;
println!("Version: {}", version);
Ok(())
}
pub async fn print_conversations(&mut self) -> Result<(), Box<dyn std::error::Error>> {
let conversations = self.api.get_conversations().await?;
for conversation in conversations {
println!("{}", ConversationPrinter::new(&conversation));
}
Ok(())
}
}