Rename network-monitor (#662)

This commit is contained in:
Drazen Urch
2021-06-29 14:48:15 +02:00
committed by GitHub
parent 2bdee705b7
commit b30f11ed7a
21 changed files with 3128 additions and 28 deletions
+110
View File
@@ -0,0 +1,110 @@
// Copyright 2021 - Nym Technologies SA <contact@nymtech.net>
// SPDX-License-Identifier: Apache-2.0
use crate::node_status_api::models::{BatchGatewayStatus, BatchMixStatus, DefaultRestResponse};
use crate::node_status_api::NodeStatusApiClientError;
pub(crate) struct Config {
base_url: String,
}
impl Config {
pub(crate) fn new<S: Into<String>>(base_url: S) -> Self {
Config {
base_url: base_url.into(),
}
}
}
pub(crate) struct Client {
config: Config,
reqwest_client: reqwest::Client,
}
impl Client {
pub(crate) fn new(config: Config) -> Self {
let reqwest_client = reqwest::Client::new();
Client {
config,
reqwest_client,
}
}
// Potentially, down the line, this could be moved to /common/client-libs
// and additional methods could be added like GET for report data, but currently
// we have absolutely no use for that in Rust.
pub(crate) async fn post_batch_mix_status(
&self,
batch_status: BatchMixStatus,
) -> Result<(), NodeStatusApiClientError> {
const RELATIVE_PATH: &str = "api/status/mixnode/batch";
let url = format!("{}/{}", self.config.base_url, RELATIVE_PATH);
let response = self
.reqwest_client
.post(url)
.json(&batch_status)
.send()
.await?;
if response.status().is_success() {
let response_content: DefaultRestResponse = response.json().await?;
match response_content {
DefaultRestResponse::Ok(ok_response) => {
if ok_response.ok {
Ok(())
} else {
Err(NodeStatusApiClientError::NodeStatusApiError(
"received an ok response with false status".into(),
))
}
}
DefaultRestResponse::Error(err_response) => Err(err_response.into()),
}
} else {
Err(NodeStatusApiClientError::NodeStatusApiError(format!(
"received response with status {}",
response.status()
)))
}
}
pub(crate) async fn post_batch_gateway_status(
&self,
batch_status: BatchGatewayStatus,
) -> Result<(), NodeStatusApiClientError> {
const RELATIVE_PATH: &str = "api/status/gateway/batch";
let url = format!("{}/{}", self.config.base_url, RELATIVE_PATH);
let response = self
.reqwest_client
.post(url)
.json(&batch_status)
.send()
.await?;
if response.status().is_success() {
let response_content: DefaultRestResponse = response.json().await?;
match response_content {
DefaultRestResponse::Ok(ok_response) => {
if ok_response.ok {
Ok(())
} else {
Err(NodeStatusApiClientError::NodeStatusApiError(
"received an ok response with false status".into(),
))
}
}
DefaultRestResponse::Error(err_response) => Err(err_response.into()),
}
} else {
Err(NodeStatusApiClientError::NodeStatusApiError(format!(
"received response with status {}",
response.status()
)))
}
}
}
+73
View File
@@ -0,0 +1,73 @@
// Copyright 2021 - Nym Technologies SA <contact@nymtech.net>
// SPDX-License-Identifier: Apache-2.0
use crate::node_status_api::models::ErrorResponses;
use std::fmt::{self, Display, Formatter};
mod client;
pub(crate) mod models;
pub(crate) use client::{Client, Config};
const MAX_SANE_UNEXPECTED_PRINT: usize = 100;
#[derive(Debug)]
pub enum NodeStatusApiClientError {
ReqwestClientError(reqwest::Error),
NodeStatusApiError(String),
UnexpectedResponse(String),
}
impl From<reqwest::Error> for NodeStatusApiClientError {
fn from(err: reqwest::Error) -> Self {
NodeStatusApiClientError::ReqwestClientError(err)
}
}
impl From<ErrorResponses> for NodeStatusApiClientError {
fn from(err: ErrorResponses) -> Self {
match err {
ErrorResponses::Error(err_message) => {
NodeStatusApiClientError::NodeStatusApiError(err_message.error)
}
ErrorResponses::Unexpected(received) => {
NodeStatusApiClientError::UnexpectedResponse(received.to_string())
}
}
}
}
impl Display for NodeStatusApiClientError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
NodeStatusApiClientError::ReqwestClientError(err) => {
write!(f, "there was an issue with the REST request - {}", err)
}
NodeStatusApiClientError::NodeStatusApiError(err) => {
write!(
f,
"there was an issue with the node status api client - {}",
err
)
}
NodeStatusApiClientError::UnexpectedResponse(received) => {
if received.len() < MAX_SANE_UNEXPECTED_PRINT {
write!(
f,
"received data was completely unexpected. got: {}",
received
)
} else {
write!(
f,
"received data was completely unexpected. got: {}...",
received
.chars()
.take(MAX_SANE_UNEXPECTED_PRINT)
.collect::<String>()
)
}
}
}
}
}
@@ -0,0 +1,68 @@
// Copyright 2021 - Nym Technologies SA <contact@nymtech.net>
// SPDX-License-Identifier: Apache-2.0
use serde::{Deserialize, Serialize};
#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
/// A notification sent to the validators to let them know whether a given mix is
/// currently up or down (based on whether it's mixing packets)
pub struct MixStatus {
pub pub_key: String,
pub owner: String,
pub ip_version: String,
pub up: bool,
}
#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
/// A notification sent to the validators to let them know whether a given set of mixes is
/// currently up or down (based on whether it's mixing packets)
pub struct BatchMixStatus {
pub status: Vec<MixStatus>,
}
#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
/// A notification sent to the validators to let them know whether a given gateway is
/// currently up or down (based on whether it's mixing packets)
pub struct GatewayStatus {
pub pub_key: String,
pub owner: String,
pub ip_version: String,
pub up: bool,
}
#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
/// A notification sent to the validators to let them know whether a given set of gateways is
/// currently up or down (based on whether it's mixing packets)
pub struct BatchGatewayStatus {
pub status: Vec<GatewayStatus>,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase", untagged)]
pub(crate) enum ErrorResponses {
Error(ErrorResponse),
Unexpected(serde_json::Value),
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub(crate) struct ErrorResponse {
pub(crate) error: String,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub(crate) struct OkResponse {
pub(crate) ok: bool,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase", untagged)]
pub(crate) enum DefaultRestResponse {
Ok(OkResponse),
Error(ErrorResponses),
}