1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//! # Library and minimal client for RouterOS' proprietary API
//!
//! Mikrotik RouterOS exposes on port TCP/8278 a semi-textual API composed of words and sentences.
//! This API allow power-users to request data about the router state, set configuration, listen to some events, ... using a syntax similar to the one used in the CLI.
//!
//! For a more in-depth description of the API, please see the [official Mikrotik wiki](https://wiki.mikrotik.com/wiki/Manual:API).
//!
//! ## The library
//! Based on tokio and fully asynchronous, the library allows to deal with all the API's uses cases :
//! - simple requests with one-off answers, like `/system/identity/print`,
//! - simple requests with array-like answers, like `/interfaces/print`,
//! - simple requests to listen to events, like `/user/active/listen`,
//! - cancel streaming commands with `/cancel`
//!
//! ### Usage
//!
//! The library exposes only one function: `connect`, that makes a TCP connection to the provided address.
//! If successful, a `MikrotikAPI<Disconnected>` object is returned.
//! It is then necessary to `authenticate` to get a `MikrotikAPI<Authenticated>` object.
//! Eight functions are then available:
//! - `system_resources` will make a call to `/system/resource/print`
//! - `interfaces` will retrieve a list of interfaces with all their properties (``/interfaces/print``)
//! - `active_users` returns a `Stream` of events regarding user activity (login & logout)
//! - `interface_changes` returns a `Stream` of events regarding changes to interfaces (up, down, ...)
//! - `cancel` cancels a streaming command given its tag
//! - `generic_oneshot_call` allows to call any endpoint providing a one-off answer. Thanks to type inference, answer is returned in the user's object of choice. Example:
//! ```rust
//! #[derive(Debug, Deserialize)]
//! struct Identity {
//! pub name: String,
//! }
//! let identity = api
//! .generic_oneshot_call::<Identity>("/system/identity/print", None)
//! .await
//! .unwrap();
//! println!("Name: '{}'", identity.name);
//! ```
//! - `generic_array_call` will do the same job but for endpoints providing multiples (but finite) answers
//! - `generic_streaming_call` will provide a `Stream` of `Response` for any endpoint supporting the `listen` command. Example:
//! ```rust
//! #[derive(Debug, Deserialize)]
//! struct Interface {
//! pub name: String,
//!
//! #[serde(default)]
//! pub running: bool
//!}
//!
//! let mut tag: u16 = 0;
//!
//! let changes = api
//! .generic_streaming_call::<Interface>("/interface/listen", None, &mut tag); //`tag` allows us to cancel the stream later on.
//!
//! tokio::spawn(changes.for_each(|item| async move {
//!
//! if let Reponse::Reply(iface) = item {
//!
//! let up_down = if iface.running { "up" } else { "down" };
//!
//! println!("Interface {} is {}", iface.name, up_down);
//! }
//!
//! })).await;
//! ```
use io;
use ;
pub use ;
pub use ;
/// Given an address, opens a connection to the remote API service
/// the returned object is in a Disconnected state
pub async