55 lines
1.4 KiB
Rust
55 lines
1.4 KiB
Rust
use async_trait::async_trait;
|
|
use std::collections::HashMap;
|
|
|
|
pub use crate::APIInterface;
|
|
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 {
|
|
ConversationNotFound,
|
|
}
|
|
|
|
impl TestClient {
|
|
pub fn new() -> TestClient {
|
|
TestClient {
|
|
version: "KordophoneTest-1.0",
|
|
conversations: vec![],
|
|
messages: HashMap::<ConversationID, Vec<Message>>::new(),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[async_trait]
|
|
impl APIInterface for TestClient {
|
|
type Error = TestError;
|
|
|
|
async fn authenticate(&mut self, _credentials: Credentials) -> Result<JwtToken, Self::Error> {
|
|
Ok(JwtToken::dummy())
|
|
}
|
|
|
|
async fn get_version(&mut self) -> Result<String, Self::Error> {
|
|
Ok(self.version.to_string())
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|