Skip to content
Snippets Groups Projects

Refactor command to make is shorter

1 file
+ 57
165
Compare changes
  • Side-by-side
  • Inline
+ 57
165
//! Define all the commands that can be used by the network
use axum::extract::{Path, State};
use axum::http::StatusCode;
use axum::response::{IntoResponse, Json, Response};
@@ -22,6 +24,8 @@ use crate::app::AppState;
use crate::dragoon_network::{DragoonEvent, FileResponse};
use crate::error::DragoonError;
use crate::to_serialize::{ConvertSer, JsonWrapper};
// Potential other commands:
// - dial
//
@@ -109,7 +113,7 @@ pub(crate) enum DragoonCommand {
peerid: String,
key: String,
sender: oneshot::Sender<Result<Vec<u8>, Box<dyn Error + Send>>>,
}
},
}
impl std::fmt::Display for DragoonCommand {
@@ -139,6 +143,23 @@ impl std::fmt::Display for DragoonCommand {
}
}
async fn command_res_match(
receiver: oneshot::Receiver<Result<impl ConvertSer, Box<dyn Error + Send>>>,
cmd_name: String,
) -> Response {
match receiver.await {
Err(e) => handle_canceled(e, &cmd_name),
Ok(res) => match res {
Err(e) => handle_dragoon_error(e, &cmd_name),
Ok(convertable) => (
StatusCode::OK,
JsonWrapper(Json(convertable.convert_ser())).into_response(),
)
.into_response(),
},
}
}
pub(crate) async fn listen(
Path(multiaddr): Path<String>,
State(state): State<Arc<AppState>>,
@@ -150,13 +171,7 @@ pub(crate) async fn listen(
let cmd_name = cmd.to_string();
send_command(cmd, state).await;
match receiver.await {
Err(e) => handle_canceled(e, &cmd_name),
Ok(res) => match res {
Err(e) => handle_dragoon_error(e, &cmd_name),
Ok(listener_id) => (StatusCode::OK, Json(format!("{:?}", listener_id))).into_response(),
},
}
command_res_match(receiver, cmd_name).await
}
pub(crate) async fn get_listeners(State(state): State<Arc<AppState>>) -> Response {
@@ -167,13 +182,7 @@ pub(crate) async fn get_listeners(State(state): State<Arc<AppState>>) -> Respons
let cmd_name = cmd.to_string();
send_command(cmd, state).await;
match receiver.await {
Err(e) => handle_canceled(e, &cmd_name),
Ok(res) => match res {
Err(e) => handle_dragoon_error(e, &cmd_name),
Ok(listeners) => (StatusCode::OK, Json(listeners)).into_response(),
},
}
command_res_match(receiver, cmd_name).await
}
pub(crate) async fn get_peer_id(State(state): State<Arc<AppState>>) -> Response {
@@ -184,17 +193,11 @@ pub(crate) async fn get_peer_id(State(state): State<Arc<AppState>>) -> Response
let cmd_name = cmd.to_string();
send_command(cmd, state).await;
match receiver.await {
Err(e) => handle_canceled(e, &cmd_name),
Ok(res) => match res {
Err(e) => handle_dragoon_error(e, &cmd_name),
Ok(peer_id) => (StatusCode::OK, Json(peer_id.to_base58())).into_response(),
},
}
command_res_match(receiver, cmd_name).await
}
#[derive(Serialize, Deserialize)]
struct SerNetworkInfo {
pub(crate) struct SerNetworkInfo {
peers: usize,
pending: u32,
connections: u32,
@@ -205,6 +208,22 @@ struct SerNetworkInfo {
established_outgoing: u32,
}
impl SerNetworkInfo {
pub(crate) fn new(network_info: &NetworkInfo) -> Self {
let connections = network_info.connection_counters();
SerNetworkInfo {
peers: network_info.num_peers(),
pending: connections.num_pending(),
connections: connections.num_connections(),
established: connections.num_established(),
pending_incoming: connections.num_pending_incoming(),
pending_outgoing: connections.num_pending_outgoing(),
established_incoming: connections.num_established_incoming(),
established_outgoing: connections.num_established_outgoing(),
}
}
}
pub(crate) async fn get_network_info(State(state): State<Arc<AppState>>) -> Response {
info!("running command `get_network_info`");
let (sender, receiver) = oneshot::channel();
@@ -213,29 +232,7 @@ pub(crate) async fn get_network_info(State(state): State<Arc<AppState>>) -> Resp
let cmd_name = cmd.to_string();
send_command(cmd, state).await;
match receiver.await {
Err(e) => handle_canceled(e, &cmd_name),
Ok(res) => match res {
Err(e) => handle_dragoon_error(e, &cmd_name),
Ok(network_info) => {
let connections = network_info.connection_counters();
(
StatusCode::OK,
Json(SerNetworkInfo {
peers: network_info.num_peers(),
pending: connections.num_pending(),
connections: connections.num_connections(),
established: connections.num_established(),
pending_incoming: connections.num_pending_incoming(),
pending_outgoing: connections.num_pending_outgoing(),
established_incoming: connections.num_established_incoming(),
established_outgoing: connections.num_established_outgoing(),
}),
)
.into_response()
}
},
}
command_res_match(receiver, cmd_name).await
}
pub(crate) async fn remove_listener(
@@ -252,13 +249,7 @@ pub(crate) async fn remove_listener(
let cmd_name = cmd.to_string();
send_command(cmd, state).await;
match receiver.await {
Err(e) => handle_canceled(e, &cmd_name),
Ok(res) => match res {
Err(e) => handle_dragoon_error(e, &cmd_name),
Ok(good) => (StatusCode::OK, Json(good)).into_response(),
},
}
command_res_match(receiver, cmd_name).await
}
pub(crate) async fn get_connected_peers(State(state): State<Arc<AppState>>) -> Response {
@@ -269,22 +260,7 @@ pub(crate) async fn get_connected_peers(State(state): State<Arc<AppState>>) -> R
let cmd_name = cmd.to_string();
send_command(cmd, state).await;
match receiver.await {
Err(e) => handle_canceled(e, &cmd_name),
Ok(res) => match res {
Err(e) => handle_dragoon_error(e, &cmd_name),
Ok(connected_peers) => (
StatusCode::OK,
Json(
connected_peers
.iter()
.map(|p| p.to_base58())
.collect::<Vec<String>>(),
),
)
.into_response(),
},
}
command_res_match(receiver, cmd_name).await
}
pub(crate) async fn dial(
@@ -298,13 +274,7 @@ pub(crate) async fn dial(
let cmd_name = cmd.to_string();
send_command(cmd, state).await;
match receiver.await {
Err(e) => handle_canceled(e, &cmd_name),
Ok(res) => match res {
Err(e) => handle_dragoon_error(e, &cmd_name),
Ok(_) => (StatusCode::OK, Json("")).into_response(),
},
}
command_res_match(receiver, cmd_name).await
}
pub(crate) async fn add_peer(
@@ -318,13 +288,7 @@ pub(crate) async fn add_peer(
let cmd_name = cmd.to_string();
send_command(cmd, state).await;
match receiver.await {
Err(e) => handle_canceled(e, &cmd_name),
Ok(res) => match res {
Err(e) => handle_dragoon_error(e, &cmd_name),
Ok(_) => (StatusCode::OK, Json("")).into_response(),
},
}
command_res_match(receiver, cmd_name).await
}
pub(crate) async fn start_provide(
@@ -338,13 +302,7 @@ pub(crate) async fn start_provide(
let cmd_name = cmd.to_string();
send_command(cmd, state).await;
match receiver.await {
Err(e) => handle_canceled(e, &cmd_name),
Ok(res) => match res {
Err(e) => handle_dragoon_error(e, &cmd_name),
Ok(_) => (StatusCode::OK, Json("")).into_response(),
},
}
command_res_match(receiver, cmd_name).await
}
pub(crate) async fn dragoon_peers(State(state): State<Arc<AppState>>) -> Response {
@@ -353,22 +311,7 @@ pub(crate) async fn dragoon_peers(State(state): State<Arc<AppState>>) -> Respons
let cmd_name = cmd.to_string();
send_command(cmd, state).await;
match receiver.await {
Ok(res) => match res {
Err(e) => handle_dragoon_error(e, &cmd_name),
Ok(peers) => (
StatusCode::OK,
Json(
peers
.iter()
.map(|peer| peer.to_base58())
.collect::<Vec<String>>(),
),
)
.into_response(),
},
Err(e) => handle_canceled(e, &cmd_name),
}
command_res_match(receiver, cmd_name).await
}
pub(crate) async fn dragoon_send(
@@ -384,13 +327,7 @@ pub(crate) async fn dragoon_send(
let cmd_name = cmd.to_string();
send_command(cmd, state).await;
match receiver.await {
Ok(res) => match res {
Ok(_) => (StatusCode::OK, Json("")).into_response(),
Err(e) => handle_dragoon_error(e, &cmd_name),
},
Err(e) => handle_canceled(e, &cmd_name),
}
command_res_match(receiver, cmd_name).await
}
pub(crate) async fn dragoon_get(
@@ -406,13 +343,7 @@ pub(crate) async fn dragoon_get(
let cmd_name = cmd.to_string();
send_command(cmd, state).await;
match receiver.await {
Ok(res) => match res {
Ok(bytes) => (StatusCode::OK, Json(bytes)).into_response(),
Err(e) => handle_dragoon_error(e, &cmd_name),
},
Err(e) => handle_canceled(e, &cmd_name),
}
command_res_match(receiver, cmd_name).await
}
pub(crate) async fn get_providers(
@@ -426,22 +357,7 @@ pub(crate) async fn get_providers(
let cmd_name = cmd.to_string();
send_command(cmd, state).await;
match receiver.await {
Err(e) => handle_canceled(e, &cmd_name),
Ok(res) => match res {
Err(e) => handle_dragoon_error(e, &cmd_name),
Ok(providers) => (
StatusCode::OK,
Json(
providers
.iter()
.map(|peer| peer.to_base58())
.collect::<Vec<String>>(),
),
)
.into_response(),
},
}
command_res_match(receiver, cmd_name).await
}
pub(crate) async fn bootstrap(State(state): State<Arc<AppState>>) -> Response {
@@ -452,13 +368,7 @@ pub(crate) async fn bootstrap(State(state): State<Arc<AppState>>) -> Response {
let cmd_name = cmd.to_string();
send_command(cmd, state).await;
match receiver.await {
Err(e) => handle_canceled(e, &cmd_name),
Ok(res) => match res {
Err(e) => handle_dragoon_error(e, &cmd_name),
Ok(_) => (StatusCode::OK, Json("")).into_response(),
},
}
command_res_match(receiver, cmd_name).await
}
#[cfg(feature = "file-sharing")]
@@ -499,13 +409,7 @@ pub(crate) async fn get_file(
let cmd_name = cmd.to_string();
send_command(cmd, state).await;
match receiver.await {
Err(e) => handle_canceled(e, &cmd_name),
Ok(res) => match res {
Err(e) => handle_dragoon_error(e, &cmd_name),
Ok(content) => Json(content).into_response(),
},
}
command_res_match(receiver, cmd_name).await
}
#[cfg(feature = "file-sharing")]
@@ -552,13 +456,7 @@ pub(crate) async fn put_record(
let cmd_name = cmd.to_string();
send_command(cmd, state).await;
match receiver.await {
Err(e) => handle_canceled(e, &cmd_name),
Ok(res) => match res {
Err(e) => handle_dragoon_error(e, &cmd_name),
Ok(_) => (StatusCode::OK, Json("")).into_response(),
},
}
command_res_match(receiver, cmd_name).await
}
pub(crate) async fn get_record(
@@ -572,13 +470,7 @@ pub(crate) async fn get_record(
let cmd_name = cmd.to_string();
send_command(cmd, state).await;
match receiver.await {
Err(e) => handle_canceled(e, &cmd_name),
Ok(res) => match res {
Err(e) => handle_dragoon_error(e, &cmd_name),
Ok(bytes) => (StatusCode::OK, Json(bytes)).into_response(),
},
}
command_res_match(receiver, cmd_name).await
}
fn handle_dragoon_error(err: Box<dyn Error + Send>, command: &str) -> Response {
@@ -612,5 +504,5 @@ async fn send_command(command: DragoonCommand, state: Arc<AppState>) -> Option<R
return Some(DragoonError::UnexpectedError(err).into_response());
}
return None;
None
}
Loading