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;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user