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

602 lines
18 KiB
Rust
Raw Normal View History

2024-04-24 23:41:42 -07:00
extern crate hyper;
extern crate serde;
2025-05-15 20:11:10 -07:00
use std::{path::PathBuf, pin::Pin, str, task::Poll};
2024-04-24 23:41:42 -07:00
use crate::api::event_socket::EventSocket;
2025-05-15 20:11:10 -07:00
use crate::api::AuthenticationStore;
use bytes::Bytes;
use hyper::body::HttpBody;
2024-06-14 20:26:56 -07:00
use hyper::{Body, Client, Method, Request, Uri};
2024-06-01 18:16:25 -07:00
2024-04-24 23:41:42 -07:00
use async_trait::async_trait;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
2024-06-01 18:17:57 -07:00
use tokio::net::TcpStream;
2025-05-15 20:11:10 -07:00
use futures_util::stream::{BoxStream, Stream};
use futures_util::task::Context;
2025-06-06 16:35:51 -07:00
use futures_util::{StreamExt, TryStreamExt};
use tokio_tungstenite::connect_async;
use tokio_tungstenite::{MaybeTlsStream, WebSocketStream};
2025-01-20 19:43:21 -08:00
use crate::{
2025-05-02 12:03:56 -07:00
model::{
2025-05-15 20:11:10 -07:00
Conversation, ConversationID, Event, JwtToken, Message, MessageID, OutgoingMessage,
UpdateItem,
},
APIInterface,
2025-01-20 19:43:21 -08:00
};
2024-04-24 23:41:42 -07:00
type HttpClient = Client<hyper::client::HttpConnector>;
pub struct HTTPAPIClient<K: AuthenticationStore + Send + Sync> {
2024-04-24 23:41:42 -07:00
pub base_url: Uri,
pub auth_store: K,
2024-04-24 23:41:42 -07:00
client: HttpClient,
}
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct Credentials {
pub username: String,
pub password: String,
}
2024-04-24 23:41:42 -07:00
#[derive(Debug)]
pub enum Error {
ClientError(String),
HTTPError(hyper::Error),
SerdeError(serde_json::Error),
DecodeError(String),
Unauthorized,
2024-04-24 23:41:42 -07:00
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::HTTPError(ref err) => Some(err),
_ => None,
}
}
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self)
}
}
2025-05-15 20:11:10 -07:00
impl From<hyper::Error> for Error {
2024-04-24 23:41:42 -07:00
fn from(err: hyper::Error) -> Error {
Error::HTTPError(err)
}
}
2025-05-15 20:11:10 -07:00
impl From<serde_json::Error> for Error {
2024-04-24 23:41:42 -07:00
fn from(err: serde_json::Error) -> Error {
Error::SerdeError(err)
}
}
2025-05-15 20:11:10 -07:00
impl From<tungstenite::Error> for Error {
fn from(err: tungstenite::Error) -> Error {
Error::ClientError(err.to_string())
}
}
trait AuthBuilder {
fn with_auth(self, token: &Option<JwtToken>) -> Self;
fn with_auth_string(self, token: &Option<String>) -> Self;
}
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())
2025-05-15 20:11:10 -07:00
} else {
self
}
}
fn with_auth_string(self, token: &Option<String>) -> Self {
if let Some(token) = &token {
self.header("Authorization", format!("Bearer: {}", token))
2025-05-15 20:11:10 -07:00
} else {
self
}
}
}
2025-01-20 22:13:44 -08:00
#[cfg(test)]
#[allow(dead_code)]
trait AuthSetting {
fn authenticate(&mut self, token: &Option<JwtToken>);
}
2025-01-20 22:13:44 -08:00
#[cfg(test)]
impl<B> AuthSetting for hyper::http::Request<B> {
fn authenticate(&mut self, token: &Option<JwtToken>) {
if let Some(token) = &token {
2025-05-15 20:11:10 -07:00
self.headers_mut()
.insert("Authorization", token.to_header_value());
}
}
}
pub struct WebsocketEventSocket {
socket: WebSocketStream<MaybeTlsStream<TcpStream>>,
}
impl WebsocketEventSocket {
pub fn new(socket: WebSocketStream<MaybeTlsStream<TcpStream>>) -> Self {
Self { socket }
}
}
impl WebsocketEventSocket {
fn raw_update_stream(self) -> impl Stream<Item = Result<Vec<UpdateItem>, Error>> {
let (_, stream) = self.socket.split();
stream
.map_err(Error::from)
.try_filter_map(|msg| async move {
match msg {
tungstenite::Message::Text(text) => {
serde_json::from_str::<Vec<UpdateItem>>(&text)
.map(Some)
.map_err(Error::from)
}
tungstenite::Message::Ping(_) => {
// Borrowing issue here with the sink, need to handle pings at the client level (whomever
// is consuming these updateitems, should be a union type of updateitem | ping).
Ok(None)
}
tungstenite::Message::Close(_) => {
// Connection was closed cleanly
Err(Error::ClientError("WebSocket connection closed".into()))
}
2025-05-15 20:11:10 -07:00
_ => Ok(None),
}
})
}
}
#[async_trait]
impl EventSocket for WebsocketEventSocket {
type Error = Error;
type EventStream = BoxStream<'static, Result<Event, Error>>;
type UpdateStream = BoxStream<'static, Result<Vec<UpdateItem>, Error>>;
async fn events(self) -> Self::EventStream {
use futures_util::stream::iter;
self.raw_update_stream()
.map_ok(|updates| iter(updates.into_iter().map(|update| Ok(Event::from(update)))))
.try_flatten()
.boxed()
}
async fn raw_updates(self) -> Self::UpdateStream {
self.raw_update_stream().boxed()
}
}
2025-05-15 20:11:10 -07:00
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 }
}
}
2024-04-24 23:41:42 -07:00
#[async_trait]
impl<K: AuthenticationStore + Send + Sync> APIInterface for HTTPAPIClient<K> {
2024-04-24 23:41:42 -07:00
type Error = Error;
2025-05-15 20:11:10 -07:00
type ResponseStream = ResponseStream;
2024-04-24 23:41:42 -07:00
async fn get_version(&mut self) -> Result<String, Self::Error> {
2025-05-15 20:11:10 -07:00
let version: String = self.deserialized_response("version", Method::GET).await?;
2024-04-24 23:41:42 -07:00
Ok(version)
}
async fn get_conversations(&mut self) -> Result<Vec<Conversation>, Self::Error> {
2025-05-15 20:11:10 -07:00
let conversations: Vec<Conversation> = self
.deserialized_response("conversations", Method::GET)
.await?;
2024-04-24 23:41:42 -07:00
Ok(conversations)
}
async fn authenticate(&mut self, credentials: Credentials) -> Result<JwtToken, Self::Error> {
#[derive(Deserialize, Debug)]
struct AuthResponse {
jwt: String,
}
log::debug!("Authenticating with username: {:?}", credentials.username);
let body = || -> Body { serde_json::to_string(&credentials).unwrap().into() };
2025-05-15 20:11:10 -07:00
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);
self.auth_store.set_token(token.to_string()).await;
Ok(token)
}
2025-01-20 19:43:21 -08:00
async fn get_messages(
2025-05-15 20:11:10 -07:00
&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);
2025-05-15 20:11:10 -07:00
if let Some(limit_val) = limit {
endpoint.push_str(&format!("&limit={}", limit_val));
}
2025-05-15 20:11:10 -07:00
if let Some(before_id) = before {
endpoint.push_str(&format!("&beforeMessageGUID={}", before_id));
}
2025-05-15 20:11:10 -07:00
if let Some(after_id) = after {
endpoint.push_str(&format!("&afterMessageGUID={}", after_id));
}
2025-05-15 20:11:10 -07:00
let messages: Vec<Message> = self.deserialized_response(&endpoint, Method::GET).await?;
2025-01-20 19:43:21 -08:00
Ok(messages)
}
2025-05-02 12:03:56 -07:00
async fn send_message(
2025-05-15 20:11:10 -07:00
&mut self,
2025-05-02 14:22:43 -07:00
outgoing_message: &OutgoingMessage,
2025-05-02 12:03:56 -07:00
) -> Result<Message, Self::Error> {
2025-05-15 20:11:10 -07:00
let message: Message = self
.deserialized_response_with_body("sendMessage", Method::POST, || {
serde_json::to_string(&outgoing_message).unwrap().into()
})
.await?;
2025-05-02 12:03:56 -07:00
Ok(message)
}
2025-05-15 20:11:10 -07:00
async fn fetch_attachment_data(
&mut self,
guid: &String,
preview: bool,
2025-05-15 20:11:10 -07:00
) -> Result<ResponseStream, Self::Error> {
let endpoint = format!("attachment?guid={}&preview={}", guid, preview);
2025-05-15 20:11:10 -07:00
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;
2025-05-15 20:11:10 -07:00
use tungstenite::handshake::client::Request as TungsteniteRequest;
let endpoint = match update_seq {
Some(seq) => format!("updates?seq={}", seq),
None => "updates".to_string(),
};
let uri = self.uri_for_endpoint(&endpoint, Some(self.websocket_scheme()));
log::debug!("Connecting to websocket: {:?}", uri);
let auth = self.auth_store.get_token().await;
let host = uri.authority().unwrap().host();
let mut request = TungsteniteRequest::builder()
.header("Host", host)
.header("Connection", "Upgrade")
.header("Upgrade", "websocket")
.header("Sec-WebSocket-Version", "13")
.header("Sec-WebSocket-Key", generate_key())
.uri(uri.to_string())
.body(())
.expect("Unable to build websocket request");
match &auth {
Some(token) => {
2025-05-15 20:11:10 -07:00
request.headers_mut().insert(
"Authorization",
format!("Bearer: {}", token).parse().unwrap(),
);
}
None => {
log::warn!(target: "websocket", "Proceeding without auth token.");
}
}
log::debug!("Websocket request: {:?}", request);
match connect_async(request).await.map_err(Error::from) {
Ok((socket, response)) => {
log::debug!("Websocket connected: {:?}", response.status());
Ok(WebsocketEventSocket::new(socket))
}
Err(e) => match &e {
Error::ClientError(ce) => match ce.as_str() {
"HTTP error: 401 Unauthorized" | "Unauthorized" => {
// Try to authenticate
if let Some(credentials) = &self.auth_store.get_credentials().await {
log::warn!("Websocket connection failed, attempting to authenticate");
let new_token = self.authenticate(credentials.clone()).await?;
self.auth_store.set_token(new_token.to_string()).await;
// try again on the next attempt.
return Err(Error::Unauthorized);
} else {
log::error!("Websocket unauthorized, no credentials provided");
2025-05-15 20:11:10 -07:00
return Err(Error::ClientError(
"Unauthorized, no credentials provided".into(),
));
}
}
2025-05-15 20:11:10 -07:00
_ => Err(e),
},
2025-05-15 20:11:10 -07:00
_ => Err(e),
},
}
}
}
2024-06-01 18:16:25 -07:00
impl<K: AuthenticationStore + Send + Sync> HTTPAPIClient<K> {
pub fn new(base_url: Uri, auth_store: K) -> HTTPAPIClient<K> {
2025-05-15 20:11:10 -07:00
HTTPAPIClient {
2024-06-14 20:26:56 -07:00
base_url,
auth_store,
client: Client::new(),
2024-06-01 18:16:25 -07:00
}
}
fn uri_for_endpoint(&self, endpoint: &str, scheme: Option<&str>) -> Uri {
2024-04-24 23:41:42 -07:00
let mut parts = self.base_url.clone().into_parts();
let root_path: PathBuf = parts.path_and_query.unwrap().path().into();
let path = root_path.join(endpoint);
parts.path_and_query = Some(path.to_str().unwrap().parse().unwrap());
if let Some(scheme) = scheme {
parts.scheme = Some(scheme.parse().unwrap());
}
2024-04-24 23:41:42 -07:00
Uri::try_from(parts).unwrap()
}
fn websocket_scheme(&self) -> &str {
if self.base_url.scheme().unwrap() == "https" {
"wss"
} else {
"ws"
}
}
2025-05-15 20:11:10 -07:00
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
2024-04-24 23:41:42 -07:00
}
2025-05-15 20:11:10 -07:00
async fn deserialized_response_with_body<T>(
&mut self,
endpoint: &str,
method: Method,
body_fn: impl Fn() -> Body,
) -> Result<T, Error>
where
T: DeserializeOwned,
{
2025-05-15 20:11:10 -07:00
self.deserialized_response_with_body_retry(endpoint, method, body_fn, true)
.await
}
2025-05-15 20:11:10 -07:00
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,
{
2025-05-15 20:11:10 -07:00
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);
log::debug!("Requesting {:?} {:?}", method, uri);
let build_request = move |auth: &Option<String>| {
let body = body_fn();
Request::builder()
.method(&method)
.uri(&uri)
.with_auth_string(auth)
.body(body)
.expect("Unable to build request")
};
let token = self.auth_store.get_token().await;
2025-04-27 14:01:19 -07:00
let request = build_request(&token);
let mut response = self.client.request(request).await?;
log::debug!("-> Response: {:}", response.status());
match response.status() {
2025-05-15 20:11:10 -07:00
StatusCode::OK => { /* cool */ }
2025-05-15 20:11:10 -07:00
// 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 {
2025-05-15 20:11:10 -07:00
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 {
2025-05-15 20:11:10 -07:00
return Err(Error::ClientError(
"Unauthorized, no credentials provided".into(),
));
}
2025-05-15 20:11:10 -07:00
}
2025-05-15 20:11:10 -07:00
// Other errors: bubble up.
_ => {
let message = format!("Request failed ({:})", response.status());
2025-05-15 20:11:10 -07:00
return Err(Error::ClientError(message));
}
2024-04-24 23:41:42 -07:00
}
2025-05-15 20:11:10 -07:00
Ok(response)
2024-04-24 23:41:42 -07:00
}
}
2025-01-20 22:13:44 -08:00
#[cfg(test)]
2024-04-24 23:41:42 -07:00
mod test {
use super::*;
use crate::api::InMemoryAuthenticationStore;
2025-05-15 20:11:10 -07:00
2025-01-20 22:13:44 -08:00
#[cfg(test)]
fn local_mock_client() -> HTTPAPIClient<InMemoryAuthenticationStore> {
2024-04-24 23:41:42 -07:00
let base_url = "http://localhost:5738".parse().unwrap();
let credentials = Credentials {
username: "test".to_string(),
password: "test".to_string(),
};
2025-05-15 20:11:10 -07:00
HTTPAPIClient::new(
base_url,
InMemoryAuthenticationStore::new(Some(credentials)),
)
2024-04-24 23:41:42 -07:00
}
2025-01-20 22:13:44 -08:00
#[cfg(test)]
2024-04-24 23:41:42 -07:00
async fn mock_client_is_reachable() -> bool {
let mut client = local_mock_client();
2024-04-24 23:41:42 -07:00
let version = client.get_version().await;
2024-06-01 18:16:25 -07:00
match version {
Ok(_) => true,
Err(e) => {
2025-01-20 22:13:44 -08:00
log::error!("Mock client error: {:?}", e);
2025-05-15 20:11:10 -07:00
false
2024-06-01 18:16:25 -07:00
}
}
2024-04-24 23:41:42 -07:00
}
#[tokio::test]
async fn test_version() {
if !mock_client_is_reachable().await {
2024-06-01 18:16:25 -07:00
log::warn!("Skipping http_client tests (mock server not reachable)");
2024-04-24 23:41:42 -07:00
return;
}
let mut client = local_mock_client();
2024-04-24 23:41:42 -07:00
let version = client.get_version().await.unwrap();
assert!(version.starts_with("KordophoneMock-"));
}
#[tokio::test]
async fn test_conversations() {
if !mock_client_is_reachable().await {
2024-06-01 18:16:25 -07:00
log::warn!("Skipping http_client tests (mock server not reachable)");
2024-04-24 23:41:42 -07:00
return;
}
let mut client = local_mock_client();
2024-04-24 23:41:42 -07:00
let conversations = client.get_conversations().await.unwrap();
2024-06-01 18:17:57 -07:00
assert!(!conversations.is_empty());
2024-04-24 23:41:42 -07:00
}
2025-01-20 19:43:21 -08:00
#[tokio::test]
async fn test_messages() {
if !mock_client_is_reachable().await {
log::warn!("Skipping http_client tests (mock server not reachable)");
return;
}
let mut client = local_mock_client();
let conversations = client.get_conversations().await.unwrap();
let conversation = conversations.first().unwrap();
2025-05-15 20:11:10 -07:00
let messages = client
.get_messages(&conversation.guid, None, None, None)
.await
.unwrap();
2025-01-20 19:43:21 -08:00
assert!(!messages.is_empty());
}
#[tokio::test]
async fn test_updates() {
if !mock_client_is_reachable().await {
log::warn!("Skipping http_client tests (mock server not reachable)");
return;
}
let mut client = local_mock_client();
// We just want to see if the connection is established, we won't wait for any events
let _ = client.open_event_socket(None).await.unwrap();
assert!(true);
}
2024-04-24 23:41:42 -07:00
}