2024-04-21 15:14:16 -07:00
|
|
|
use async_trait::async_trait;
|
2025-01-20 19:43:21 -08:00
|
|
|
pub use crate::model::{
|
2025-04-28 15:17:58 -07:00
|
|
|
Conversation, Message, ConversationID, MessageID,
|
2025-01-20 19:43:21 -08:00
|
|
|
};
|
2024-06-14 20:23:44 -07:00
|
|
|
use crate::model::JwtToken;
|
2024-04-20 18:17:55 -07:00
|
|
|
|
2024-04-24 23:41:42 -07:00
|
|
|
pub mod http_client;
|
2024-06-14 20:23:44 -07:00
|
|
|
pub use http_client::HTTPAPIClient;
|
|
|
|
|
|
|
|
|
|
use self::http_client::Credentials;
|
2024-04-24 23:41:42 -07:00
|
|
|
|
2024-04-21 15:14:16 -07:00
|
|
|
#[async_trait]
|
2024-04-20 18:17:55 -07:00
|
|
|
pub trait APIInterface {
|
2024-04-21 15:14:16 -07:00
|
|
|
type Error;
|
2024-04-20 18:17:55 -07:00
|
|
|
|
2024-04-21 15:14:16 -07:00
|
|
|
// (GET) /version
|
2024-06-14 20:23:44 -07:00
|
|
|
async fn get_version(&mut self) -> Result<String, Self::Error>;
|
2024-04-21 15:14:16 -07:00
|
|
|
|
|
|
|
|
// (GET) /conversations
|
2024-06-14 20:23:44 -07:00
|
|
|
async fn get_conversations(&mut self) -> Result<Vec<Conversation>, Self::Error>;
|
|
|
|
|
|
2025-01-20 19:43:21 -08:00
|
|
|
// (GET) /messages
|
2025-04-28 15:17:58 -07:00
|
|
|
async fn get_messages(
|
|
|
|
|
&mut self,
|
|
|
|
|
conversation_id: &ConversationID,
|
|
|
|
|
limit: Option<u32>,
|
|
|
|
|
before: Option<MessageID>,
|
|
|
|
|
after: Option<MessageID>,
|
|
|
|
|
) -> Result<Vec<Message>, Self::Error>;
|
2025-01-20 19:43:21 -08:00
|
|
|
|
2024-06-14 20:23:44 -07:00
|
|
|
// (POST) /authenticate
|
|
|
|
|
async fn authenticate(&mut self, credentials: Credentials) -> Result<JwtToken, Self::Error>;
|
2024-04-20 18:17:55 -07:00
|
|
|
}
|
2024-04-21 15:14:16 -07:00
|
|
|
|
2025-04-27 14:01:19 -07:00
|
|
|
#[async_trait]
|
2025-05-01 01:02:36 -07:00
|
|
|
pub trait AuthenticationStore {
|
|
|
|
|
async fn get_credentials(&mut self) -> Option<Credentials>;
|
2025-04-27 13:40:59 -07:00
|
|
|
async fn get_token(&mut self) -> Option<JwtToken>;
|
|
|
|
|
async fn set_token(&mut self, token: JwtToken);
|
2025-04-25 20:02:18 -07:00
|
|
|
}
|
|
|
|
|
|
2025-05-01 01:02:36 -07:00
|
|
|
pub struct InMemoryAuthenticationStore {
|
|
|
|
|
credentials: Option<Credentials>,
|
2025-04-25 20:02:18 -07:00
|
|
|
token: Option<JwtToken>,
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-01 01:02:36 -07:00
|
|
|
impl Default for InMemoryAuthenticationStore {
|
2025-04-28 16:06:51 -07:00
|
|
|
fn default() -> Self {
|
2025-05-01 01:02:36 -07:00
|
|
|
Self::new(None)
|
2025-04-28 16:06:51 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-01 01:02:36 -07:00
|
|
|
impl InMemoryAuthenticationStore {
|
|
|
|
|
pub fn new(credentials: Option<Credentials>) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
credentials,
|
|
|
|
|
token: None,
|
|
|
|
|
}
|
2025-04-25 20:02:18 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-27 14:01:19 -07:00
|
|
|
#[async_trait]
|
2025-05-01 01:02:36 -07:00
|
|
|
impl AuthenticationStore for InMemoryAuthenticationStore {
|
|
|
|
|
async fn get_credentials(&mut self) -> Option<Credentials> {
|
|
|
|
|
self.credentials.clone()
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-27 13:40:59 -07:00
|
|
|
async fn get_token(&mut self) -> Option<JwtToken> {
|
2025-04-25 20:02:18 -07:00
|
|
|
self.token.clone()
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-27 13:40:59 -07:00
|
|
|
async fn set_token(&mut self, token: JwtToken) {
|
2025-04-25 20:02:18 -07:00
|
|
|
self.token = Some(token);
|
|
|
|
|
}
|
|
|
|
|
}
|