Private
Public Access
1
0

kordophone: add support for /messages

This commit is contained in:
2025-01-20 19:43:21 -08:00
parent 793faab721
commit a8104c379c
8 changed files with 206 additions and 6 deletions

View File

@@ -9,7 +9,10 @@ use hyper::{Body, Client, Method, Request, Uri};
use async_trait::async_trait;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use crate::{model::{Conversation, JwtToken}, APIInterface};
use crate::{
model::{Conversation, ConversationID, JwtToken, Message},
APIInterface
};
type HttpClient = Client<hyper::client::HttpConnector>;
@@ -111,6 +114,12 @@ impl APIInterface for HTTPAPIClient {
self.auth_token = Some(token.clone());
Ok(token)
}
async fn get_messages(&mut self, conversation_id: &ConversationID) -> Result<Vec<Message>, Self::Error> {
let endpoint = format!("messages?guid={}", conversation_id);
let messages: Vec<Message> = self.request(&endpoint, Method::GET).await?;
Ok(messages)
}
}
impl HTTPAPIClient {
@@ -261,4 +270,18 @@ mod test {
let conversations = client.get_conversations().await.unwrap();
assert!(!conversations.is_empty());
}
#[tokio::test]
async fn test_messages() {
if !mock_client_is_reachable().await {
log::warn!("Skipping http_client tests (mock server not reachable)");
return;
}
let mut client = local_mock_client();
let conversations = client.get_conversations().await.unwrap();
let conversation = conversations.first().unwrap();
let messages = client.get_messages(&conversation.guid).await.unwrap();
assert!(!messages.is_empty());
}
}

View File

@@ -1,5 +1,7 @@
use async_trait::async_trait;
pub use crate::model::Conversation;
pub use crate::model::{
Conversation, Message, ConversationID
};
use crate::model::JwtToken;
pub mod http_client;
@@ -17,6 +19,9 @@ pub trait APIInterface {
// (GET) /conversations
async fn get_conversations(&mut self) -> Result<Vec<Conversation>, Self::Error>;
// (GET) /messages
async fn get_messages(&mut self, conversation_id: &ConversationID) -> Result<Vec<Message>, Self::Error>;
// (POST) /authenticate
async fn authenticate(&mut self, credentials: Credentials) -> Result<JwtToken, Self::Error>;
}

View File

@@ -2,6 +2,10 @@ use serde::Deserialize;
use time::OffsetDateTime;
use uuid::Uuid;
use super::Identifiable;
pub type ConversationID = <Conversation as Identifiable>::ID;
#[derive(Debug, Clone, Deserialize)]
pub struct Conversation {
pub guid: String,
@@ -28,6 +32,14 @@ impl Conversation {
}
}
impl Identifiable for Conversation {
type ID = String;
fn id(&self) -> &Self::ID {
&self.guid
}
}
#[derive(Default)]
pub struct ConversationBuilder {
guid: Option<String>,

View File

@@ -0,0 +1,67 @@
use serde::Deserialize;
use time::OffsetDateTime;
use uuid::Uuid;
#[derive(Debug, Clone, Deserialize)]
pub struct Message {
pub guid: String,
#[serde(rename = "text")]
pub text: String,
#[serde(rename = "sender")]
pub sender: Option<String>,
#[serde(with = "time::serde::iso8601")]
pub date: OffsetDateTime,
}
impl Message {
pub fn builder() -> MessageBuilder {
MessageBuilder::new()
}
}
#[derive(Default)]
pub struct MessageBuilder {
guid: Option<String>,
text: Option<String>,
sender: Option<String>,
date: Option<OffsetDateTime>,
}
impl MessageBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn guid(mut self, guid: String) -> Self {
self.guid = Some(guid);
self
}
pub fn text(mut self, text: String) -> Self {
self.text = Some(text);
self
}
pub fn sender(mut self, sender: String) -> Self {
self.sender = Some(sender);
self
}
pub fn date(mut self, date: OffsetDateTime) -> Self {
self.date = Some(date);
self
}
pub fn build(self) -> Message {
Message {
guid: self.guid.unwrap_or(Uuid::new_v4().to_string()),
text: self.text.unwrap_or("".to_string()),
sender: self.sender,
date: self.date.unwrap_or(OffsetDateTime::now_utc()),
}
}
}

View File

@@ -1,5 +1,15 @@
pub mod conversation;
pub mod message;
pub use conversation::Conversation;
pub use conversation::ConversationID;
pub use message::Message;
pub mod jwt;
pub use jwt::JwtToken;
pub use jwt::JwtToken;
pub trait Identifiable {
type ID;
fn id(&self) -> &Self::ID;
}

View File

@@ -1,21 +1,29 @@
use async_trait::async_trait;
use std::collections::HashMap;
pub use crate::APIInterface;
use crate::{api::http_client::Credentials, model::{Conversation, JwtToken}};
use crate::{
api::http_client::Credentials,
model::{conversation, Conversation, ConversationID, JwtToken, Message}
};
pub struct TestClient {
pub version: &'static str,
pub conversations: Vec<Conversation>,
pub messages: HashMap<ConversationID, Vec<Message>>,
}
#[derive(Debug)]
pub enum TestError {}
pub enum TestError {
ConversationNotFound,
}
impl TestClient {
pub fn new() -> TestClient {
TestClient {
version: "KordophoneTest-1.0",
conversations: vec![],
messages: HashMap::<ConversationID, Vec<Message>>::new(),
}
}
}
@@ -35,4 +43,12 @@ impl APIInterface for TestClient {
async fn get_conversations(&mut self) -> Result<Vec<Conversation>, Self::Error> {
Ok(self.conversations.clone())
}
async fn get_messages(&mut self, conversation: Conversation) -> Result<Vec<Message>, Self::Error> {
if let Some(messages) = self.messages.get(&conversation.guid) {
return Ok(messages.clone())
}
Err(TestError::ConversationNotFound)
}
}