- Import the library.
const NetworkMapsLib = require('./networkmapslib');
- Create a NetworkMapsLib object.
let nml = new NetworkMapsLib(USE_SSL,
SERVER,
PORT,
{
verify_cert: VERIFY_CERT,
session_id: SESSION_ID
},
CALLBACK,
CLOSE_CALLBACK
);
- USE_SSL: boolean defining if the connection will use http or https.
- SERVER: IP address or hostname of the server.
- PORT: port where the server is listening.
- VERIFY_CERT: (optional) boolean defining if we will verify validity of the servers certificate.
- SESSION_ID: (optional) if we already have an established session, we can provide it here. This way, if this session is already authenticated, we won't have to.
- CALLBACK: function that will be called when the connection to the user endpoint is established. This function will not have any parameter.
- CLOSE_CALLBACK: function that will be called when the connection to the user endpoint is broken. This might happen if for example, the server breaks or is restarted.
- Verify if the connection is authenticated. If not, authenticate us.
if(!nml.authenticated) {
// The session is not authenticated. let's authenticate ourselves
console.log("Session not authenticated. Authenticating...");
nml.login(USERNAME, PASSWORD, (error) => {
if(error) {
console.log("Authentication error: " + error);
}
else {
console.log("Session authenticated: " + nml.conn.session_id);
do_something(nml);
}
})
}
else {
// Session is authenticated.
do_something(nml);
}
- At this point, we have a working connection to the user endpoint. From here, we can list available diagrams, create new ones, delete them, or manage their permissions.
- Let's create a diagram.
console.log("Creating diagram...");
nml.add_diagram("Diagram Name", (error, uuid) => {
if(error) {
console.log("Error creating diagram: " + error);
exit();
}
// Diagram has been created, do something with it
do_something_with_diagram(nml, uuid);
})
- Next, to edit the diagram, we will connect to the diagram endpoint (the connection will use the same session id we used or created on the connection to the user endpoint). When connected, we will receive a Diagram object on which we can do different calls to edit it.
nml.setup_diagram_ws(uuid, (error, diagram) => {
if(error) {
console.log("Error connecting to new diagram.")
exit();
}
console.log("Connected to diagram.");
edit_diagram(diagram);
})