[][src]Struct cp_api::Client

pub struct Client { /* fields omitted */ }

A Client to communicate with the API.

There are various configuration values to set, but the defaults are the most commonly used.

The Client should be created and reused for multiple API calls.

use cp_api::Client;
use serde_json::json;

let mut client = Client::new("192.168.1.10", 443);
client.certificate("/home/admin/cert.cer");
client.login("user", "pass")?;
client.call("show-host", json!({"name": "host1"}))?;
client.call("show-package", json!({"name": "Standard"}))?;
client.logout()?;

Methods

impl Client[src]

pub fn new(server: &str, port: u16) -> Self[src]

Create a new Client to make API calls.

let mut client = Client::new("192.168.1.10", 443);

pub fn login(&mut self, user: &str, pass: &str) -> Result<Response>[src]

Login to the API.

If the login is successful, the uid and api-server-version are stored in the Client. If the session is not read only, the sid will be stored as well.

let mut client = Client::new("192.168.1.10", 443);
client.certificate("/home/admin/cert.cer");
let login = client.login("user", "pass")?;
assert!(login.is_success());

pub fn logout(&mut self) -> Result<Response>[src]

Logout of the API.

If the logout was successful, the sid, uid, and api-server-version are cleared from the Client.

let logout = client.logout()?;
assert!(logout.is_success());

pub fn call(&mut self, command: &str, payload: Value) -> Result<Response>[src]

Perform an API call.

let host_payload = json!({
    "name": "host1",
    "ip-address": "172.25.1.50"
});

let host = client.call("add-host", host_payload)?;
assert!(host.is_success());

let rule_payload = json!({
    "name": "allow host1",
    "layer": "Network",
    "position": "top",
    "source": "host1",
    "action": "accept"
});

let rule = client.call("add-access-rule", rule_payload)?;
assert!(rule.is_success());

let publish = client.call("publish", json!({}))?;
assert!(publish.is_success());

pub fn query(&mut self, command: &str, details_level: &str) -> Result<Response>[src]

Perform an API query.

All commands that return a list of objects can take a details-level parameter. The possible options are "standard", "full", and "uid".

A vector of all the objects will be stored in the Response objects field.

let hosts = client.query("show-hosts", "standard")?;
assert!(hosts.is_success());

for host in &hosts.objects {
    println!("{} - {}", host["name"], host["ipv4-address"]);
}

pub fn certificate(&mut self, s: &str)[src]

Set a binary DER encoded certificate.

If a certificate is set, accept_invalid_certs will be ignored.

client.certificate("/home/admin/mycert.cer");

pub fn accept_invalid_certs(&mut self, b: bool)[src]

Set the certificate validation.

The default is false to not accept invalid certificates.

client.accept_invalid_certs(true);

pub fn proxy(&mut self, s: &str)[src]

Set the proxy to use.

This will proxy all HTTPS traffic to the specified URL.

client.proxy("https://10.1.1.100:8080");

pub fn connect_timeout(&mut self, t: u64)[src]

Set the connection timeout in seconds to the Management server. Default is 30 seconds.

client.connect_timeout(10);

pub fn session_timeout(&mut self, t: u64)[src]

Set the login session-timeout in seconds. Default is 600 seconds.

client.session_timeout(1200);

pub fn domain(&mut self, s: &str)[src]

Set the Domain to login to.

client.domain("System Data");

pub fn read_only(&mut self, b: bool)[src]

Set to true to login with read only permissions. Default is false.

client.read_only(true);

pub fn continue_last_session(&mut self, b: bool)[src]

Set to true to continue the last session. Default is false.

client.continue_last_session(true);

pub fn sid(&self) -> &str[src]

Get the sid after logging in.

client.login("user", "pass")?;
println!("{}", client.sid());

pub fn uid(&self) -> &str[src]

Get the uid after logging in. This will be empty if read_only is true.

client.login("user", "pass")?;
println!("{}", client.uid());

pub fn api_server_version(&self) -> &str[src]

Get the api-server-version after logging in.

client.login("user", "pass")?;
println!("{}", client.api_server_version());

pub fn wait_for_task(&mut self, b: bool)[src]

Wait for an API call to complete.

Some API commands return a task-id while they continue to run. The default is to wait for the task to finish.

Set this to false to not wait for the task to complete.

client.wait_for_task(false);

let payload = json!({
    "policy-package": "Standard",
    "access": true,
    "targets": "Gateway1"
});

let response = client.call("install-policy", payload)?;
println!("task-id = {}", response.data["task-id"]);

pub fn log_file(&mut self, s: &str)[src]

Set the log file name that will contain the API calls.

The path to the file can be absolute or relative. Separate the path with / on Linux and either / or \\ on Windows.

client.log_file("C:\\Users\\admin\\Desktop\\log.txt");

client.log_file("/home/admin/log.txt");

pub fn save_log(&mut self) -> Result<()>[src]

Save the API calls to a file.

API calls made before log_file was set will not be saved.

After the API calls are saved, the API calls and log_file will be cleared.

client.log_file("/home/admin/log.txt");
client.call("show-host", json!({"name": "host1"}))?;
client.save_log()?;

pub fn show_password(&mut self, b: bool)[src]

Show the login password as clear text in the log file.

This must be set before logging in or the password will be obfuscated.

client.log_file("/home/admin/log.txt");
client.show_password(true);
client.login("user", "pass")?;
client.save_log()?;

Trait Implementations

impl Drop for Client[src]

impl Debug for Client[src]

impl Serialize for Client[src]

Auto Trait Implementations

impl Send for Client

impl Sync for Client

Blanket Implementations

impl<T> From for T[src]

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T> BorrowMut for T where
    T: ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Erased for T