1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2025-01-07 11:22:36 +00:00
comunicapiv3/src/data/user_ws_request_handler.rs

94 lines
2.6 KiB
Rust
Raw Normal View History

2021-02-05 12:21:10 +00:00
//! # User Web Socket Request handler
use std::collections::HashMap;
use serde::Serialize;
use crate::api_data::http_error::HttpError;
2021-02-13 15:15:25 +00:00
use crate::routes::RequestResult;
2021-02-05 12:21:10 +00:00
use crate::data::base_request_handler::{BaseRequestHandler, RequestValue};
2021-02-11 17:29:29 +00:00
use crate::data::error::ResultBoxError;
2021-02-13 13:37:15 +00:00
use crate::data::user_token::UserAccessToken;
2021-02-10 16:16:52 +00:00
use crate::data::user_ws_connection::UserWsConnection;
2021-02-05 12:21:10 +00:00
2021-02-05 13:24:00 +00:00
pub enum UserWsResponseType {
2021-02-05 12:21:10 +00:00
SUCCESS,
ERROR,
}
2021-02-05 13:24:00 +00:00
pub struct UserWsResponse {
pub r#type: UserWsResponseType,
2021-02-05 12:21:10 +00:00
pub content: serde_json::Value,
}
2021-02-05 13:24:00 +00:00
pub struct UserWsRequestHandler {
2021-02-10 16:16:52 +00:00
connection: UserWsConnection,
2021-02-05 12:21:10 +00:00
args: HashMap<String, RequestValue>,
2021-02-05 13:24:00 +00:00
response: Option<UserWsResponse>,
2021-02-05 12:21:10 +00:00
}
2021-02-05 13:24:00 +00:00
impl UserWsRequestHandler {
2021-02-10 16:16:52 +00:00
pub fn new(connection: &UserWsConnection, args: HashMap<String, String>) -> UserWsRequestHandler {
2021-02-05 13:24:00 +00:00
UserWsRequestHandler {
2021-02-05 12:21:10 +00:00
connection: connection.clone(),
args: args.into_iter().map(|f| (f.0, RequestValue::String(f.1))).collect(),
response: None,
}
}
/// Check if a response has been set
pub fn has_response(&self) -> bool {
self.response.is_some()
}
/// Get the response to the request
2021-02-05 13:24:00 +00:00
pub fn response(mut self) -> UserWsResponse {
2021-02-05 12:21:10 +00:00
if !self.has_response() {
self.success("Request done.").unwrap();
}
return self.response.unwrap();
}
2021-02-05 13:24:00 +00:00
2021-02-10 17:04:03 +00:00
/// Get a pointer to the websocket connection
pub fn get_conn(&self) -> &UserWsConnection {
&self.connection
}
2021-02-05 13:24:00 +00:00
/// Update information about the WebSocket connection
2021-02-10 16:16:52 +00:00
pub fn update_conn<H>(&mut self, do_updates: H) -> ResultBoxError where H: FnOnce(&mut UserWsConnection) {
2021-02-05 13:24:00 +00:00
self.connection = self.connection.clone().replace(do_updates);
Ok(())
}
2021-02-05 12:21:10 +00:00
}
2021-02-05 13:24:00 +00:00
impl BaseRequestHandler for UserWsRequestHandler {
2021-02-05 12:21:10 +00:00
fn post_parameter_opt(&self, name: &str) -> Option<&RequestValue> {
self.args.get(name)
}
fn set_response<T: Serialize>(&mut self, response: T) -> RequestResult {
2021-02-05 13:24:00 +00:00
self.response = Some(UserWsResponse {
r#type: UserWsResponseType::SUCCESS,
2021-02-05 12:21:10 +00:00
content: serde_json::to_value(response)?,
});
Ok(())
}
fn set_error(&mut self, error: HttpError) {
2021-02-05 13:24:00 +00:00
self.response = Some(UserWsResponse {
r#type: UserWsResponseType::ERROR,
2021-02-05 12:21:10 +00:00
content: serde_json::Value::String(error.error.message),
});
}
fn remote_ip(&self) -> String {
self.connection.remote_ip.to_string()
}
2021-02-13 13:37:15 +00:00
fn user_access_token(&self) -> Option<&UserAccessToken> {
Some(&self.connection.user_token)
2021-02-05 12:21:10 +00:00
}
}