Private
Public Access
1
0
Files
Kordophone/kordophone/src/api/mod.rs

61 lines
1.4 KiB
Rust
Raw Normal View History

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::{
Conversation, Message, ConversationID, MessageID,
2025-01-20 19:43:21 -08: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;
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
async fn get_version(&mut self) -> Result<String, Self::Error>;
2024-04-21 15:14:16 -07:00
// (GET) /conversations
async fn get_conversations(&mut self) -> Result<Vec<Conversation>, Self::Error>;
2025-01-20 19:43:21 -08:00
// (GET) /messages
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
// (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]
pub trait TokenStore {
async fn get_token(&mut self) -> Option<JwtToken>;
async fn set_token(&mut self, token: JwtToken);
}
2025-04-27 14:01:19 -07:00
pub struct InMemoryTokenStore {
token: Option<JwtToken>,
}
2025-04-27 14:01:19 -07:00
impl InMemoryTokenStore {
pub fn new() -> Self {
Self { token: None }
}
}
2025-04-27 14:01:19 -07:00
#[async_trait]
impl TokenStore for InMemoryTokenStore {
async fn get_token(&mut self) -> Option<JwtToken> {
self.token.clone()
}
async fn set_token(&mut self, token: JwtToken) {
self.token = Some(token);
}
}