Private
Public Access
1
0

Started working on attachment store

This commit is contained in:
2025-05-15 20:11:10 -07:00
parent 77177e07aa
commit 0d4c2e5104
10 changed files with 721 additions and 307 deletions

View File

@@ -1,10 +1,12 @@
extern crate hyper;
extern crate serde;
use std::{path::PathBuf, str};
use std::{path::PathBuf, pin::Pin, str, task::Poll};
use crate::api::AuthenticationStore;
use crate::api::event_socket::EventSocket;
use crate::api::AuthenticationStore;
use bytes::Bytes;
use hyper::body::HttpBody;
use hyper::{Body, Client, Method, Request, Uri};
use async_trait::async_trait;
@@ -12,26 +14,19 @@ use serde::{de::DeserializeOwned, Deserialize, Serialize};
use tokio::net::TcpStream;
use futures_util::stream::{BoxStream, Stream};
use futures_util::task::Context;
use futures_util::{SinkExt, StreamExt, TryStreamExt};
use futures_util::stream::{SplitStream, SplitSink, Stream};
use futures_util::stream::BoxStream;
use tokio_tungstenite::connect_async;
use tokio_tungstenite::{MaybeTlsStream, WebSocketStream};
use crate::{
model::{
Conversation,
ConversationID,
JwtToken,
Message,
MessageID,
UpdateItem,
Event,
OutgoingMessage,
},
APIInterface
Conversation, ConversationID, Event, JwtToken, Message, MessageID, OutgoingMessage,
UpdateItem,
},
APIInterface,
};
type HttpClient = Client<hyper::client::HttpConnector>;
@@ -72,19 +67,19 @@ impl std::fmt::Display for Error {
}
}
impl From <hyper::Error> for Error {
impl From<hyper::Error> for Error {
fn from(err: hyper::Error) -> Error {
Error::HTTPError(err)
}
}
impl From <serde_json::Error> for Error {
impl From<serde_json::Error> for Error {
fn from(err: serde_json::Error) -> Error {
Error::SerdeError(err)
}
}
impl From <tungstenite::Error> for Error {
impl From<tungstenite::Error> for Error {
fn from(err: tungstenite::Error) -> Error {
Error::ClientError(err.to_string())
}
@@ -99,13 +94,17 @@ impl AuthBuilder for hyper::http::request::Builder {
fn with_auth(self, token: &Option<JwtToken>) -> Self {
if let Some(token) = &token {
self.header("Authorization", token.to_header_value())
} else { self }
} else {
self
}
}
fn with_auth_string(self, token: &Option<String>) -> Self {
if let Some(token) = &token {
self.header("Authorization", format!("Bearer: {}", token))
} else { self }
} else {
self
}
}
}
@@ -119,7 +118,8 @@ trait AuthSetting {
impl<B> AuthSetting for hyper::http::Request<B> {
fn authenticate(&mut self, token: &Option<JwtToken>) {
if let Some(token) = &token {
self.headers_mut().insert("Authorization", token.to_header_value());
self.headers_mut()
.insert("Authorization", token.to_header_value());
}
}
}
@@ -156,7 +156,7 @@ impl WebsocketEventSocket {
// Connection was closed cleanly
Err(Error::ClientError("WebSocket connection closed".into()))
}
_ => Ok(None)
_ => Ok(None),
}
})
}
@@ -182,17 +182,40 @@ impl EventSocket for WebsocketEventSocket {
}
}
pub struct ResponseStream {
body: hyper::Body,
}
impl Stream for ResponseStream {
type Item = Result<Bytes, Error>;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
self.body
.poll_next_unpin(cx)
.map_err(|e| Error::HTTPError(e))
}
}
impl From<hyper::Body> for ResponseStream {
fn from(value: hyper::Body) -> Self {
ResponseStream { body: value }
}
}
#[async_trait]
impl<K: AuthenticationStore + Send + Sync> APIInterface for HTTPAPIClient<K> {
type Error = Error;
type ResponseStream = ResponseStream;
async fn get_version(&mut self) -> Result<String, Self::Error> {
let version: String = self.request("version", Method::GET).await?;
let version: String = self.deserialized_response("version", Method::GET).await?;
Ok(version)
}
async fn get_conversations(&mut self) -> Result<Vec<Conversation>, Self::Error> {
let conversations: Vec<Conversation> = self.request("conversations", Method::GET).await?;
let conversations: Vec<Conversation> = self
.deserialized_response("conversations", Method::GET)
.await?;
Ok(conversations)
}
@@ -205,7 +228,9 @@ impl<K: AuthenticationStore + Send + Sync> APIInterface for HTTPAPIClient<K> {
log::debug!("Authenticating with username: {:?}", credentials.username);
let body = || -> Body { serde_json::to_string(&credentials).unwrap().into() };
let token: AuthResponse = self.request_with_body_retry("authenticate", Method::POST, body, false).await?;
let token: AuthResponse = self
.deserialized_response_with_body_retry("authenticate", Method::POST, body, false)
.await?;
let token = JwtToken::new(&token.jwt).map_err(|e| Error::DecodeError(e.to_string()))?;
log::debug!("Saving token: {:?}", token);
@@ -215,46 +240,60 @@ impl<K: AuthenticationStore + Send + Sync> APIInterface for HTTPAPIClient<K> {
}
async fn get_messages(
&mut self,
&mut self,
conversation_id: &ConversationID,
limit: Option<u32>,
before: Option<MessageID>,
after: Option<MessageID>,
) -> Result<Vec<Message>, Self::Error> {
let mut endpoint = format!("messages?guid={}", conversation_id);
if let Some(limit_val) = limit {
endpoint.push_str(&format!("&limit={}", limit_val));
}
if let Some(before_id) = before {
endpoint.push_str(&format!("&beforeMessageGUID={}", before_id));
}
if let Some(after_id) = after {
endpoint.push_str(&format!("&afterMessageGUID={}", after_id));
}
let messages: Vec<Message> = self.request(&endpoint, Method::GET).await?;
let messages: Vec<Message> = self.deserialized_response(&endpoint, Method::GET).await?;
Ok(messages)
}
async fn send_message(
&mut self,
&mut self,
outgoing_message: &OutgoingMessage,
) -> Result<Message, Self::Error> {
let message: Message = self.request_with_body(
"sendMessage",
Method::POST,
|| serde_json::to_string(&outgoing_message).unwrap().into()
).await?;
let message: Message = self
.deserialized_response_with_body("sendMessage", Method::POST, || {
serde_json::to_string(&outgoing_message).unwrap().into()
})
.await?;
Ok(message)
}
async fn open_event_socket(&mut self, update_seq: Option<u64>) -> Result<WebsocketEventSocket, Self::Error> {
use tungstenite::handshake::client::Request as TungsteniteRequest;
async fn fetch_attachment_data(
&mut self,
guid: &String,
) -> Result<ResponseStream, Self::Error> {
let endpoint = format!("attachment?guid={}", guid);
self.response_with_body_retry(&endpoint, Method::GET, Body::empty, true)
.await
.map(hyper::Response::into_body)
.map(ResponseStream::from)
}
async fn open_event_socket(
&mut self,
update_seq: Option<u64>,
) -> Result<WebsocketEventSocket, Self::Error> {
use tungstenite::handshake::client::generate_key;
use tungstenite::handshake::client::Request as TungsteniteRequest;
let endpoint = match update_seq {
Some(seq) => format!("updates?seq={}", seq),
@@ -279,7 +318,10 @@ impl<K: AuthenticationStore + Send + Sync> APIInterface for HTTPAPIClient<K> {
match &auth {
Some(token) => {
request.headers_mut().insert("Authorization", format!("Bearer: {}", token).parse().unwrap());
request.headers_mut().insert(
"Authorization",
format!("Bearer: {}", token).parse().unwrap(),
);
}
None => {
log::warn!(target: "websocket", "Proceeding without auth token.");
@@ -306,21 +348,23 @@ impl<K: AuthenticationStore + Send + Sync> APIInterface for HTTPAPIClient<K> {
return Err(Error::Unauthorized);
} else {
log::error!("Websocket unauthorized, no credentials provided");
return Err(Error::ClientError("Unauthorized, no credentials provided".into()));
return Err(Error::ClientError(
"Unauthorized, no credentials provided".into(),
));
}
}
_ => Err(e)
}
_ => Err(e),
},
_ => Err(e)
}
_ => Err(e),
},
}
}
}
impl<K: AuthenticationStore + Send + Sync> HTTPAPIClient<K> {
pub fn new(base_url: Uri, auth_store: K) -> HTTPAPIClient<K> {
HTTPAPIClient {
HTTPAPIClient {
base_url,
auth_store,
client: Client::new(),
@@ -348,25 +392,67 @@ impl<K: AuthenticationStore + Send + Sync> HTTPAPIClient<K> {
}
}
async fn request<T: DeserializeOwned>(&mut self, endpoint: &str, method: Method) -> Result<T, Error> {
self.request_with_body(endpoint, method, Body::empty).await
async fn deserialized_response<T: DeserializeOwned>(
&mut self,
endpoint: &str,
method: Method,
) -> Result<T, Error> {
self.deserialized_response_with_body(endpoint, method, Body::empty)
.await
}
async fn request_with_body<T>(&mut self, endpoint: &str, method: Method, body_fn: impl Fn() -> Body) -> Result<T, Error>
where T: DeserializeOwned
async fn deserialized_response_with_body<T>(
&mut self,
endpoint: &str,
method: Method,
body_fn: impl Fn() -> Body,
) -> Result<T, Error>
where
T: DeserializeOwned,
{
self.request_with_body_retry(endpoint, method, body_fn, true).await
self.deserialized_response_with_body_retry(endpoint, method, body_fn, true)
.await
}
async fn request_with_body_retry<T>(
&mut self,
endpoint: &str,
method: Method,
body_fn: impl Fn() -> Body,
retry_auth: bool) -> Result<T, Error>
where
T: DeserializeOwned,
async fn deserialized_response_with_body_retry<T>(
&mut self,
endpoint: &str,
method: Method,
body_fn: impl Fn() -> Body,
retry_auth: bool,
) -> Result<T, Error>
where
T: DeserializeOwned,
{
let response = self
.response_with_body_retry(endpoint, method, body_fn, retry_auth)
.await?;
// Read and parse response body
let body = hyper::body::to_bytes(response.into_body()).await?;
let parsed: T = match serde_json::from_slice(&body) {
Ok(result) => Ok(result),
Err(json_err) => {
log::error!("Error deserializing JSON: {:?}", json_err);
log::error!("Body: {:?}", String::from_utf8_lossy(&body));
// If JSON deserialization fails, try to interpret it as plain text
// Unfortunately the server does return things like this...
let s = str::from_utf8(&body).map_err(|e| Error::DecodeError(e.to_string()))?;
serde_plain::from_str(s).map_err(|_| json_err)
}
}?;
Ok(parsed)
}
async fn response_with_body_retry(
&mut self,
endpoint: &str,
method: Method,
body_fn: impl Fn() -> Body,
retry_auth: bool,
) -> Result<hyper::Response<Body>, Error> {
use hyper::StatusCode;
let uri = self.uri_for_endpoint(endpoint, None);
@@ -389,48 +475,38 @@ impl<K: AuthenticationStore + Send + Sync> HTTPAPIClient<K> {
log::debug!("-> Response: {:}", response.status());
match response.status() {
StatusCode::OK => { /* cool */ },
StatusCode::OK => { /* cool */ }
// 401: Unauthorized. Token may have expired or is invalid. Attempt to renew.
// 401: Unauthorized. Token may have expired or is invalid. Attempt to renew.
StatusCode::UNAUTHORIZED => {
if !retry_auth {
return Err(Error::ClientError("Unauthorized".into()));
}
if let Some(credentials) = &self.auth_store.get_credentials().await {
log::debug!("Renewing token using credentials: u: {:?}", credentials.username);
log::debug!(
"Renewing token using credentials: u: {:?}",
credentials.username
);
let new_token = self.authenticate(credentials.clone()).await?;
let request = build_request(&Some(new_token.to_string()));
response = self.client.request(request).await?;
} else {
return Err(Error::ClientError("Unauthorized, no credentials provided".into()));
return Err(Error::ClientError(
"Unauthorized, no credentials provided".into(),
));
}
},
}
// Other errors: bubble up.
// Other errors: bubble up.
_ => {
let message = format!("Request failed ({:})", response.status());
return Err(Error::ClientError(message));
return Err(Error::ClientError(message));
}
}
// Read and parse response body
let body = hyper::body::to_bytes(response.into_body()).await?;
let parsed: T = match serde_json::from_slice(&body) {
Ok(result) => Ok(result),
Err(json_err) => {
log::error!("Error deserializing JSON: {:?}", json_err);
log::error!("Body: {:?}", String::from_utf8_lossy(&body));
// If JSON deserialization fails, try to interpret it as plain text
// Unfortunately the server does return things like this...
let s = str::from_utf8(&body).map_err(|e| Error::DecodeError(e.to_string()))?;
serde_plain::from_str(s).map_err(|_| json_err)
}
}?;
Ok(parsed)
Ok(response)
}
}
@@ -438,7 +514,7 @@ impl<K: AuthenticationStore + Send + Sync> HTTPAPIClient<K> {
mod test {
use super::*;
use crate::api::InMemoryAuthenticationStore;
#[cfg(test)]
fn local_mock_client() -> HTTPAPIClient<InMemoryAuthenticationStore> {
let base_url = "http://localhost:5738".parse().unwrap();
@@ -447,7 +523,10 @@ mod test {
password: "test".to_string(),
};
HTTPAPIClient::new(base_url, InMemoryAuthenticationStore::new(Some(credentials)))
HTTPAPIClient::new(
base_url,
InMemoryAuthenticationStore::new(Some(credentials)),
)
}
#[cfg(test)]
@@ -459,7 +538,7 @@ mod test {
Ok(_) => true,
Err(e) => {
log::error!("Mock client error: {:?}", e);
false
false
}
}
}
@@ -498,7 +577,10 @@ mod test {
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, None, None, None).await.unwrap();
let messages = client
.get_messages(&conversation.guid, None, None, None)
.await
.unwrap();
assert!(!messages.is_empty());
}

View File

@@ -1,7 +1,8 @@
pub use crate::model::{Conversation, ConversationID, Message, MessageID, OutgoingMessage};
use async_trait::async_trait;
pub use crate::model::{
Conversation, Message, ConversationID, MessageID, OutgoingMessage,
};
use bytes::Bytes;
use futures_util::Stream;
pub mod auth;
pub use crate::api::auth::{AuthenticationStore, InMemoryAuthenticationStore};
@@ -15,21 +16,23 @@ pub mod event_socket;
pub use event_socket::EventSocket;
use self::http_client::Credentials;
use std::fmt::Debug;
use core::error::Error as StdError;
use std::{fmt::Debug, io::BufRead};
#[async_trait]
pub trait APIInterface {
type Error: Debug;
type ResponseStream: Stream<Item = Result<Bytes, Self::Error>>;
// (GET) /version
async fn get_version(&mut self) -> Result<String, Self::Error>;
// (GET) /conversations
async fn get_conversations(&mut self) -> Result<Vec<Conversation>, Self::Error>;
// (GET) /messages
async fn get_messages(
&mut self,
&mut self,
conversation_id: &ConversationID,
limit: Option<u32>,
before: Option<MessageID>,
@@ -38,13 +41,22 @@ pub trait APIInterface {
// (POST) /sendMessage
async fn send_message(
&mut self,
&mut self,
outgoing_message: &OutgoingMessage,
) -> Result<Message, Self::Error>;
// (GET) /attachment
async fn fetch_attachment_data(
&mut self,
guid: &String,
) -> Result<Self::ResponseStream, Self::Error>;
// (POST) /authenticate
async fn authenticate(&mut self, credentials: Credentials) -> Result<JwtToken, Self::Error>;
// (WS) /updates
async fn open_event_socket(&mut self, update_seq: Option<u64>) -> Result<impl EventSocket, Self::Error>;
async fn open_event_socket(
&mut self,
update_seq: Option<u64>,
) -> Result<impl EventSocket, Self::Error>;
}

View File

@@ -6,13 +6,16 @@ use uuid::Uuid;
pub use crate::APIInterface;
use crate::{
api::http_client::Credentials,
model::{Conversation, ConversationID, JwtToken, Message, MessageID, UpdateItem, Event, OutgoingMessage},
api::event_socket::EventSocket,
};
api::http_client::Credentials,
model::{
Conversation, ConversationID, Event, JwtToken, Message, MessageID, OutgoingMessage,
UpdateItem,
},
};
use futures_util::StreamExt;
use futures_util::stream::BoxStream;
use futures_util::StreamExt;
pub struct TestClient {
pub version: &'static str,
@@ -59,7 +62,7 @@ impl EventSocket for TestEventSocket {
let results: Vec<Result<Vec<UpdateItem>, TestError>> = vec![];
futures_util::stream::iter(results.into_iter()).boxed()
}
}
}
#[async_trait]
impl APIInterface for TestClient {
@@ -78,21 +81,21 @@ impl APIInterface for TestClient {
}
async fn get_messages(
&mut self,
conversation_id: &ConversationID,
limit: Option<u32>,
before: Option<MessageID>,
after: Option<MessageID>
&mut self,
conversation_id: &ConversationID,
limit: Option<u32>,
before: Option<MessageID>,
after: Option<MessageID>,
) -> Result<Vec<Message>, Self::Error> {
if let Some(messages) = self.messages.get(conversation_id) {
return Ok(messages.clone())
return Ok(messages.clone());
}
Err(TestError::ConversationNotFound)
}
async fn send_message(
&mut self,
&mut self,
outgoing_message: &OutgoingMessage,
) -> Result<Message, Self::Error> {
let message = Message::builder()
@@ -101,13 +104,21 @@ impl APIInterface for TestClient {
.date(OffsetDateTime::now_utc())
.build();
self.messages.entry(outgoing_message.conversation_id.clone()).or_insert(vec![]).push(message.clone());
self.messages
.entry(outgoing_message.conversation_id.clone())
.or_insert(vec![])
.push(message.clone());
Ok(message)
}
async fn open_event_socket(&mut self, _update_seq: Option<u64>) -> Result<impl EventSocket, Self::Error> {
async fn open_event_socket(
&mut self,
_update_seq: Option<u64>,
) -> Result<impl EventSocket, Self::Error> {
Ok(TestEventSocket::new())
}
async fn fetch_attachment_data(&mut self, guid: &String) -> Result<Vec<u8>, Self::Error> {
Ok(vec![])
}
}