removed unused dependencies, refactored some code

This commit is contained in:
Ishan Jain 2022-04-18 19:54:38 +05:30
parent f989c91eec
commit 861bc05d7e
10 changed files with 60 additions and 293 deletions

174
Cargo.lock generated
View File

@ -130,21 +130,6 @@ dependencies = [
"percent-encoding",
]
[[package]]
name = "futures"
version = "0.3.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f73fe65f54d1e12b726f517d3e2135ca3125a437b6d998caf1962961f7172d9e"
dependencies = [
"futures-channel",
"futures-core",
"futures-executor",
"futures-io",
"futures-sink",
"futures-task",
"futures-util",
]
[[package]]
name = "futures-channel"
version = "0.3.21"
@ -152,7 +137,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3083ce4b914124575708913bca19bfe887522d6e2e6d0952943f5eac4a74010"
dependencies = [
"futures-core",
"futures-sink",
]
[[package]]
@ -161,34 +145,6 @@ version = "0.3.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c09fd04b7e4073ac7156a9539b57a484a8ea920f79c7c675d05d289ab6110d3"
[[package]]
name = "futures-executor"
version = "0.3.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9420b90cfa29e327d0429f19be13e7ddb68fa1cccb09d65e5706b8c7a749b8a6"
dependencies = [
"futures-core",
"futures-task",
"futures-util",
]
[[package]]
name = "futures-io"
version = "0.3.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc4045962a5a5e935ee2fdedaa4e08284547402885ab326734432bed5d12966b"
[[package]]
name = "futures-macro"
version = "0.3.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "33c1e13800337f4d4d7a316bf45a567dbcb6ffe087f16424852d97e97a91f512"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "futures-sink"
version = "0.3.21"
@ -207,16 +163,10 @@ version = "0.3.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d8b7abd5d659d9b90c8cba917f6ec750a74e2dc23902ef9cd4cc8c8b22e6036a"
dependencies = [
"futures-channel",
"futures-core",
"futures-io",
"futures-macro",
"futures-sink",
"futures-task",
"memchr",
"pin-project-lite",
"pin-utils",
"slab",
]
[[package]]
@ -244,15 +194,6 @@ version = "0.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e"
[[package]]
name = "hermit-abi"
version = "0.1.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
dependencies = [
"libc",
]
[[package]]
name = "http"
version = "0.2.6"
@ -400,16 +341,6 @@ version = "0.2.123"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb691a747a7ab48abc15c5b42066eaafde10dc427e3b6ee2a1cf43db04c763bd"
[[package]]
name = "lock_api"
version = "0.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "327fa5b6a6940e4699ec49a9beae1ea4845c6bab9314e4f84ac68742139d8c53"
dependencies = [
"autocfg",
"scopeguard",
]
[[package]]
name = "log"
version = "0.4.16"
@ -436,7 +367,6 @@ name = "mikrotik"
version = "0.0.4"
dependencies = [
"base64",
"futures",
"reqwest",
"serde",
"serde_derive",
@ -502,16 +432,6 @@ dependencies = [
"winapi",
]
[[package]]
name = "num_cpus"
version = "1.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "19e64526ebdee182341572e50e9ad03965aa510cd94427a4549448f285e957a1"
dependencies = [
"hermit-abi",
"libc",
]
[[package]]
name = "once_cell"
version = "1.10.0"
@ -551,29 +471,6 @@ dependencies = [
"vcpkg",
]
[[package]]
name = "parking_lot"
version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87f5ec2493a61ac0506c0f4199f99070cbe83857b0337006a30f3e6719b8ef58"
dependencies = [
"lock_api",
"parking_lot_core",
]
[[package]]
name = "parking_lot_core"
version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "995f667a6c822200b0433ac218e05582f0e2efa1b922a3fd2fbaadc5f87bab37"
dependencies = [
"cfg-if",
"libc",
"redox_syscall",
"smallvec",
"windows-sys",
]
[[package]]
name = "percent-encoding"
version = "2.1.0"
@ -727,12 +624,6 @@ dependencies = [
"winapi",
]
[[package]]
name = "scopeguard"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
[[package]]
name = "sct"
version = "0.7.0"
@ -771,9 +662,6 @@ name = "serde"
version = "1.0.136"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce31e24b01e1e524df96f1c2fdd054405f8d7376249a5110886fb4b658484789"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
@ -809,27 +697,12 @@ dependencies = [
"serde",
]
[[package]]
name = "signal-hook-registry"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e51e73328dc4ac0c7ccbda3a494dfa03df1de2f46018127f60c693f2648455b0"
dependencies = [
"libc",
]
[[package]]
name = "slab"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eb703cfe953bccee95685111adeedb76fabe4e97549a58d16f03ea7b9367bb32"
[[package]]
name = "smallvec"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2dd574626839106c320a323308629dcb1acfc96e32a8cba364ddc61ac23ee83"
[[package]]
name = "socket2"
version = "0.4.4"
@ -916,11 +789,7 @@ dependencies = [
"libc",
"memchr",
"mio",
"num_cpus",
"once_cell",
"parking_lot",
"pin-project-lite",
"signal-hook-registry",
"socket2",
"tokio-macros",
"winapi",
@ -1218,49 +1087,6 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "windows-sys"
version = "0.34.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5acdd78cb4ba54c0045ac14f62d8f94a03d10047904ae2a40afa1e99d8f70825"
dependencies = [
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_msvc"
version = "0.34.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "17cffbe740121affb56fad0fc0e421804adf0ae00891205213b5cecd30db881d"
[[package]]
name = "windows_i686_gnu"
version = "0.34.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2564fde759adb79129d9b4f54be42b32c89970c18ebf93124ca8870a498688ed"
[[package]]
name = "windows_i686_msvc"
version = "0.34.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9cd9d32ba70453522332c14d38814bceeb747d80b3958676007acadd7e166956"
[[package]]
name = "windows_x86_64_gnu"
version = "0.34.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cfce6deae227ee8d356d19effc141a509cc503dfd1f850622ec4b0f84428e1f4"
[[package]]
name = "windows_x86_64_msvc"
version = "0.34.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d19538ccc21819d01deaf88d6a17eae6596a12e9aafdbb97916fb49896d89de9"
[[package]]
name = "winreg"
version = "0.10.1"

View File

@ -5,16 +5,14 @@ edition = "2021"
description = "REST API Wrapper for Mikrotik API"
license = "MIT"
[dev-dependencies]
tokio-test = "0.4.2"
tokio = { version = "1.17.0", features = ["macros"] }
[dependencies]
base64 = "0.13.0"
futures = "0.3.17"
reqwest = { version = "0.11.4", features = ["rustls-tls", "serde_json", "json"] }
serde = { version = "1.0.130", features = ["derive"] }
serde = "1.0.130"
serde_derive = "1.0.136"
thiserror = "1.0.30"
tokio = { version = "1.10.1", features = ["full"] }
url = "2.2.2"

View File

@ -1,12 +1,13 @@
use reqwest::{
header::{HeaderMap, HeaderValue},
Request, Url,
Method, Request, Url,
};
use serde::de::DeserializeOwned;
use thiserror::Error;
pub struct Client {
pub client: reqwest::Client,
pub base_url: Url,
base_url: Url,
pub basic_auth: HeaderValue,
pub self_signed_cert: bool,
}
@ -21,7 +22,8 @@ impl Client {
let value = format!("{}:{}", username, password);
let value = base64::encode(value);
let basic_auth = HeaderValue::from_str(&format!("Basic {}", value))?;
let basic_auth = HeaderValue::from_str(&format!("Basic {}", value))
.expect("invalid value for Authorization header");
let client = reqwest::Client::builder()
.danger_accept_invalid_certs(self_signed_cert)
@ -48,13 +50,20 @@ impl Client {
self.client.execute(r).await
}
pub async fn execute_get<T: DeserializeOwned>(&mut self, url: &str) -> Result<T, ClientError> {
let url = self.base_url.join(url)?;
let mut req = Request::new(Method::GET, url);
self.add_credentials(req.headers_mut());
let response = self.client.execute(req).await?.json::<T>().await?;
Ok(response)
}
}
#[derive(Debug, Error)]
pub enum ClientError {
#[error(transparent)]
InvalidHeaderValue(#[from] reqwest::header::InvalidHeaderValue),
#[error(transparent)]
UrlError(#[from] url::ParseError),

View File

@ -1,34 +1,13 @@
use crate::{interface::types::Interface, Client};
use reqwest::{Method, Request};
use thiserror::Error;
use crate::{interface::types::Interface, Client, ClientError};
#[derive(Error, Debug)]
pub enum InterfaceError {
#[error(transparent)]
UrlError(#[from] url::ParseError),
pub async fn list(client: &mut Client) -> Result<Vec<Interface>, ClientError> {
let url = super::BASE;
#[error(transparent)]
ReqwestError(#[from] reqwest::Error),
client.execute_get::<Vec<Interface>>(&url).await
}
pub async fn list(client: &mut Client) -> Result<Vec<Interface>, InterfaceError> {
let url = client.base_url.clone();
let url = url.join(super::BASE)?;
pub async fn get(client: &mut Client, ifid: &str) -> Result<Interface, ClientError> {
let url = format!("{}/{}", super::BASE, ifid);
let req = Request::new(Method::GET, url);
let response = client.execute(req).await?.json::<Vec<Interface>>().await?;
Ok(response)
}
pub async fn get(client: &mut Client, ifid: &str) -> Result<Interface, InterfaceError> {
let url = client.base_url.clone();
let url = url.join(&format!("{}/{}", super::BASE, ifid))?;
let req = Request::new(Method::GET, url);
let response = client.execute(req).await?.json::<Interface>().await?;
Ok(response)
client.execute_get::<Interface>(&url).await
}

View File

@ -1,69 +1,40 @@
use crate::{
ip::types::{DhcpServer, Lease, Network},
Client,
Client, ClientError,
};
use reqwest::{Method, Request};
use thiserror::Error;
pub async fn list(client: &mut Client) -> Result<Vec<DhcpServer>, DhcpServerError> {
let url = client.base_url.clone();
let url = url.join(&format!("{}/dhcp-server", super::BASE))?;
let req = Request::new(Method::GET, url);
let response = client.execute(req).await?.json::<Vec<DhcpServer>>().await?;
pub async fn list(client: &mut Client) -> Result<Vec<DhcpServer>, ClientError> {
let url = format!("{}/dhcp-server", super::BASE);
Ok(response)
client.execute_get::<Vec<DhcpServer>>(&url).await
}
pub async fn get(client: &mut Client, dhcp_server_id: &str) -> Result<DhcpServer, DhcpServerError> {
let url = client.base_url.clone();
let url = url.join(&format!("{}/dhcp-server/{}", super::BASE, dhcp_server_id))?;
let req = Request::new(Method::GET, url);
let response = client.execute(req).await?.json::<DhcpServer>().await?;
pub async fn get(client: &mut Client, dhcp_server_id: &str) -> Result<DhcpServer, ClientError> {
let url = format!("{}/dhcp-server/{}", super::BASE, dhcp_server_id);
Ok(response)
client.execute_get::<DhcpServer>(&url).await
}
pub async fn list_network(client: &mut Client) -> Result<Vec<Network>, DhcpServerError> {
let url = client.base_url.clone();
let url = url.join(&format!("{}/dhcp-server/network", super::BASE))?;
let req = Request::new(Method::GET, url);
let response = client.execute(req).await?.json::<Vec<Network>>().await?;
pub async fn list_network(client: &mut Client) -> Result<Vec<Network>, ClientError> {
let url = format!("{}/dhcp-server/network", super::BASE);
Ok(response)
client.execute_get::<Vec<Network>>(&url).await
}
pub async fn get_network(client: &mut Client, nid: &str) -> Result<Network, DhcpServerError> {
let url = client.base_url.clone();
let url = url.join(&format!("{}/dhcp-server/network/{}", super::BASE, nid))?;
let req = Request::new(Method::GET, url);
let response = client.execute(req).await?.json::<Network>().await?;
pub async fn get_network(client: &mut Client, nid: &str) -> Result<Network, ClientError> {
let url = format!("{}/dhcp-server/network/{}", super::BASE, nid);
Ok(response)
client.execute_get::<Network>(&url).await
}
pub async fn list_leases(client: &mut Client) -> Result<Vec<Lease>, DhcpServerError> {
let url = client.base_url.clone();
let url = url.join(&format!("{}/dhcp-server/lease", super::BASE))?;
let req = Request::new(Method::GET, url);
let response = client.execute(req).await?.json::<Vec<Lease>>().await?;
pub async fn list_leases(client: &mut Client) -> Result<Vec<Lease>, ClientError> {
let url = format!("{}/dhcp-server/lease", super::BASE);
Ok(response)
client.execute_get::<Vec<Lease>>(&url).await
}
pub async fn get_lease(client: &mut Client, lease_id: &str) -> Result<Lease, DhcpServerError> {
let url = client.base_url.clone();
let url = url.join(&format!("{}/dhcp-server/lease/{}", super::BASE, lease_id))?;
let req = Request::new(Method::GET, url);
let response = client.execute(req).await?.json::<Lease>().await?;
pub async fn get_lease(client: &mut Client, lease_id: &str) -> Result<Lease, ClientError> {
let url = format!("{}/dhcp-server/lease/{}", super::BASE, lease_id);
Ok(response)
}
#[derive(Debug, Error)]
pub enum DhcpServerError {
#[error(transparent)]
UrlError(#[from] url::ParseError),
#[error(transparent)]
ReqwestError(#[from] reqwest::Error),
client.execute_get::<Lease>(&url).await
}

View File

@ -38,7 +38,7 @@ pub struct Lease {
pub disabled: String,
pub dynamic: String,
#[serde(rename = "host-name")]
pub host_name: String,
pub host_name: Option<String>,
#[serde(rename = "last-seen")]
pub last_seen: String,
#[serde(rename = "mac-address")]

View File

@ -1,23 +1,7 @@
use crate::{system::types::Health, Client};
use reqwest::{Method, Request};
use thiserror::Error;
use crate::{system::types::Health, Client, ClientError};
#[derive(Error, Debug)]
pub enum HealthError {
#[error(transparent)]
UrlError(#[from] url::ParseError),
pub async fn health(client: &mut Client) -> Result<Vec<Health>, ClientError> {
let url = format!("{}/health", super::BASE);
#[error(transparent)]
ReqwestError(#[from] reqwest::Error),
}
pub async fn health(client: &mut Client) -> Result<Vec<Health>, HealthError> {
let url = client.base_url.clone();
let url = url.join(&format!("{}/health", super::BASE))?;
let req = Request::new(Method::GET, url);
let response = client.execute(req).await?.json::<Vec<Health>>().await?;
Ok(response)
client.execute_get::<Vec<Health>>(&url).await
}

View File

@ -1,8 +1,8 @@
use mikrotik::{interface::InterfaceError, Client};
use mikrotik::{Client, ClientError};
use reqwest::Url;
#[tokio::test]
async fn list_interfaces() -> Result<(), InterfaceError> {
async fn list_interfaces() -> Result<(), ClientError> {
let base = Url::parse("https://10.0.10.1")?;
let mut client = Client::new(base, "admin".to_string(), "ifd783far".to_string(), true)
.expect("error in creating client");
@ -15,7 +15,7 @@ async fn list_interfaces() -> Result<(), InterfaceError> {
}
#[tokio::test]
async fn get_interface() -> Result<(), InterfaceError> {
async fn get_interface() -> Result<(), ClientError> {
let base = Url::parse("https://10.0.10.1")?;
let mut client = Client::new(base, "admin".to_string(), "ifd783far".to_string(), true)
.expect("error in creating client");

View File

@ -1,8 +1,8 @@
use mikrotik::{ip::dhcp_server::DhcpServerError, Client};
use mikrotik::{Client, ClientError};
use reqwest::Url;
#[tokio::test]
async fn list_dhcp_servers() -> Result<(), DhcpServerError> {
async fn list_dhcp_servers() -> Result<(), ClientError> {
let base = Url::parse("https://10.0.10.1")?;
let mut client = Client::new(base, "admin".to_string(), "ifd783far".to_string(), true)
.expect("error in creating client");
@ -15,7 +15,7 @@ async fn list_dhcp_servers() -> Result<(), DhcpServerError> {
}
#[tokio::test]
async fn get_dhcp_server() -> Result<(), DhcpServerError> {
async fn get_dhcp_server() -> Result<(), ClientError> {
let base = Url::parse("https://10.0.10.1")?;
let mut client = Client::new(base, "admin".to_string(), "ifd783far".to_string(), true)
.expect("error in creating client");
@ -28,7 +28,7 @@ async fn get_dhcp_server() -> Result<(), DhcpServerError> {
}
#[tokio::test]
async fn list_network() -> Result<(), DhcpServerError> {
async fn list_network() -> Result<(), ClientError> {
let base = Url::parse("https://10.0.10.1")?;
let mut client = Client::new(base, "admin".to_string(), "ifd783far".to_string(), true)
.expect("error in creating client");
@ -41,7 +41,7 @@ async fn list_network() -> Result<(), DhcpServerError> {
}
#[tokio::test]
async fn get_network() -> Result<(), DhcpServerError> {
async fn get_network() -> Result<(), ClientError> {
let base = Url::parse("https://10.0.10.1")?;
let mut client = Client::new(base, "admin".to_string(), "ifd783far".to_string(), true)
.expect("error in creating client");
@ -56,7 +56,7 @@ async fn get_network() -> Result<(), DhcpServerError> {
}
#[tokio::test]
async fn list_leases() -> Result<(), DhcpServerError> {
async fn list_leases() -> Result<(), ClientError> {
let base = Url::parse("https://10.0.10.1")?;
let mut client = Client::new(base, "admin".to_string(), "ifd783far".to_string(), true)
.expect("error in creating client");
@ -69,7 +69,7 @@ async fn list_leases() -> Result<(), DhcpServerError> {
}
#[tokio::test]
async fn get_lease() -> Result<(), DhcpServerError> {
async fn get_lease() -> Result<(), ClientError> {
let base = Url::parse("https://10.0.10.1")?;
let mut client = Client::new(base, "admin".to_string(), "ifd783far".to_string(), true)
.expect("error in creating client");

View File

@ -1,8 +1,8 @@
use mikrotik::{system::HealthError, Client};
use mikrotik::{Client, ClientError};
use reqwest::Url;
#[tokio::test]
async fn health() -> Result<(), HealthError> {
async fn health() -> Result<(), ClientError> {
let base = Url::parse("https://10.0.10.1")?;
let mut client = Client::new(base, "admin".to_string(), "ifd783far".to_string(), true)
.expect("error in creating client");