use std::{sync::Arc, time::Duration};
use tokio::time::sleep;
use tracing::instrument;
use crate::client::{Error, GeminiClient};
use crate::interactions::model::*;
use crate::interactions::stream::InteractionStream;
#[derive(Clone)]
pub struct InteractionHandle {
id: String,
client: Arc<GeminiClient>,
}
impl InteractionHandle {
pub(crate) fn new(id: String, client: Arc<GeminiClient>) -> Self {
Self { id, client }
}
pub fn id(&self) -> &str {
&self.id
}
#[instrument(skip(self))]
pub async fn get(&self) -> Result<Interaction, Error> {
self.client.get_interaction(&self.id).await
}
#[instrument(skip(self), fields(last_event_id = last_event_id.unwrap_or("")))]
pub async fn get_stream(
&self,
last_event_id: Option<&str>,
) -> Result<InteractionStream, Error> {
self.client
.get_interaction_stream(&self.id, last_event_id)
.await
}
#[instrument(skip(self))]
pub async fn cancel(&self) -> Result<Interaction, Error> {
self.client.cancel_interaction(&self.id).await
}
#[instrument(skip(self))]
pub async fn delete(&self) -> Result<(), Error> {
self.client.delete_interaction(&self.id).await
}
#[instrument(skip(self), fields(poll.interval = ?interval))]
pub async fn poll_until_completed(&self, interval: Duration) -> Result<Interaction, Error> {
loop {
let interaction = self.get().await?;
if interaction.status.is_terminal() {
return Ok(interaction);
}
sleep(interval).await;
}
}
pub fn as_previous_interaction_id(&self) -> &str {
&self.id
}
}