client: implements send_message
This commit is contained in:
@@ -20,7 +20,17 @@ use tokio_tungstenite::connect_async;
|
||||
use tokio_tungstenite::{MaybeTlsStream, WebSocketStream};
|
||||
|
||||
use crate::{
|
||||
model::{Conversation, ConversationID, JwtToken, Message, MessageID, UpdateItem, Event},
|
||||
model::{
|
||||
Conversation,
|
||||
ConversationID,
|
||||
JwtToken,
|
||||
Message,
|
||||
MessageID,
|
||||
UpdateItem,
|
||||
Event,
|
||||
OutgoingMessage,
|
||||
},
|
||||
|
||||
APIInterface
|
||||
};
|
||||
|
||||
@@ -215,6 +225,19 @@ impl<K: AuthenticationStore + Send + Sync> APIInterface for HTTPAPIClient<K> {
|
||||
Ok(messages)
|
||||
}
|
||||
|
||||
async fn send_message(
|
||||
&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?;
|
||||
|
||||
Ok(message)
|
||||
}
|
||||
|
||||
async fn open_event_socket(&mut self) -> Result<WebsocketEventSocket, Self::Error> {
|
||||
use tungstenite::http::StatusCode;
|
||||
use tungstenite::handshake::client::Request as TungsteniteRequest;
|
||||
@@ -285,24 +308,23 @@ 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
|
||||
self.request_with_body(endpoint, method, Body::empty).await
|
||||
}
|
||||
|
||||
async fn request_with_body<T, B>(&mut self, endpoint: &str, method: Method, body_fn: B) -> Result<T, Error>
|
||||
where T: DeserializeOwned, B: Fn() -> Body
|
||||
async fn request_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
|
||||
}
|
||||
|
||||
async fn request_with_body_retry<T, B>(
|
||||
async fn request_with_body_retry<T>(
|
||||
&mut self,
|
||||
endpoint: &str,
|
||||
method: Method,
|
||||
body_fn: B,
|
||||
body_fn: impl Fn() -> Body,
|
||||
retry_auth: bool) -> Result<T, Error>
|
||||
where
|
||||
T: DeserializeOwned,
|
||||
B: Fn() -> Body
|
||||
{
|
||||
use hyper::StatusCode;
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use async_trait::async_trait;
|
||||
pub use crate::model::{
|
||||
Conversation, Message, ConversationID, MessageID,
|
||||
Conversation, Message, ConversationID, MessageID, OutgoingMessage,
|
||||
};
|
||||
|
||||
pub mod auth;
|
||||
@@ -35,6 +35,12 @@ pub trait APIInterface {
|
||||
after: Option<MessageID>,
|
||||
) -> Result<Vec<Message>, Self::Error>;
|
||||
|
||||
// (POST) /sendMessage
|
||||
async fn send_message(
|
||||
&mut self,
|
||||
outgoing_message: OutgoingMessage,
|
||||
) -> Result<Message, Self::Error>;
|
||||
|
||||
// (POST) /authenticate
|
||||
async fn authenticate(&mut self, credentials: Credentials) -> Result<JwtToken, Self::Error>;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user