client: implements event/updates websocket
This commit is contained in:
@@ -2,10 +2,14 @@ use kordophone::APIInterface;
|
||||
use kordophone::api::http_client::HTTPAPIClient;
|
||||
use kordophone::api::http_client::Credentials;
|
||||
use kordophone::api::InMemoryAuthenticationStore;
|
||||
use kordophone::api::event_socket::EventSocket;
|
||||
|
||||
use anyhow::Result;
|
||||
use clap::Subcommand;
|
||||
use crate::printers::{ConversationPrinter, MessagePrinter};
|
||||
use kordophone::model::event::Event;
|
||||
|
||||
use futures_util::StreamExt;
|
||||
|
||||
pub fn make_api_client_from_env() -> HTTPAPIClient<InMemoryAuthenticationStore> {
|
||||
dotenv::dotenv().ok();
|
||||
@@ -37,6 +41,12 @@ pub enum Commands {
|
||||
|
||||
/// Prints the server Kordophone version.
|
||||
Version,
|
||||
|
||||
/// Prints all events from the server.
|
||||
Events,
|
||||
|
||||
/// Prints all raw updates from the server.
|
||||
RawUpdates,
|
||||
}
|
||||
|
||||
impl Commands {
|
||||
@@ -46,6 +56,8 @@ impl Commands {
|
||||
Commands::Version => client.print_version().await,
|
||||
Commands::Conversations => client.print_conversations().await,
|
||||
Commands::Messages { conversation_id } => client.print_messages(conversation_id).await,
|
||||
Commands::RawUpdates => client.print_raw_updates().await,
|
||||
Commands::Events => client.print_events().await,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -82,6 +94,35 @@ impl ClientCli {
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn print_events(&mut self) -> Result<()> {
|
||||
let socket = self.api.open_event_socket().await?;
|
||||
|
||||
let mut stream = socket.events().await;
|
||||
while let Some(Ok(event)) = stream.next().await {
|
||||
match event {
|
||||
Event::ConversationChanged(conversation) => {
|
||||
println!("Conversation changed: {}", conversation.guid);
|
||||
}
|
||||
Event::MessageReceived(conversation, message) => {
|
||||
println!("Message received: msg: {} conversation: {}", message.guid, conversation.guid);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn print_raw_updates(&mut self) -> Result<()> {
|
||||
let socket = self.api.open_event_socket().await?;
|
||||
|
||||
println!("Listening for raw updates...");
|
||||
let mut stream = socket.raw_updates().await;
|
||||
while let Some(update) = stream.next().await {
|
||||
println!("Got update: {:?}", update);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user