Skip to main content

LegacyClient

Struct LegacyClient 

Source
pub struct LegacyClient { /* private fields */ }
Expand description

Raw HTTP client for the UniFi controller’s legacy API.

Handles the { data: [], meta: { rc, msg } } envelope, site-scoped URL construction, and platform-aware path prefixing. All methods return unwrapped data payloads – the envelope is stripped before the caller sees it.

Implementations§

Source§

impl LegacyClient

Source

pub async fn login( &self, username: &str, password: &SecretString, ) -> Result<(), Error>

Authenticate with the controller using username/password.

On success the session cookie is stored in the client’s cookie jar and used for all subsequent requests. The login endpoint differs by platform:

  • UniFi OS: POST /api/auth/login
  • Standalone: POST /api/login
Source

pub async fn logout(&self) -> Result<(), Error>

End the current session.

Platform-specific logout endpoint:

  • UniFi OS: POST /api/auth/logout
  • Standalone: POST /api/logout
Source

pub async fn detect_platform( base_url: &Url, ) -> Result<ControllerPlatform, Error>

Auto-detect the controller platform by probing login endpoints.

Tries the UniFi OS endpoint first (/api/auth/login). If it responds (even with an error), we’re on UniFi OS. If the connection fails or returns 404, falls back to standalone detection.

Source§

impl LegacyClient

Source

pub fn new( base_url: Url, site: String, platform: ControllerPlatform, transport: &TransportConfig, ) -> Result<Self, Error>

Create a new legacy client from a TransportConfig.

If the config doesn’t already include a cookie jar, one is created automatically (legacy auth requires cookies). The base_url should be the controller root (e.g. https://192.168.1.1 for UniFi OS or https://controller:8443 for standalone).

Source

pub fn with_client( http: Client, base_url: Url, site: String, platform: ControllerPlatform, ) -> Self

Create a legacy client with a pre-built reqwest::Client.

Use this when you already have a client with a session cookie in its jar (e.g. after authenticating via a shared client).

Source

pub fn site(&self) -> &str

The current site identifier.

Source

pub fn http(&self) -> &Client

The underlying HTTP client (for auth flows that need direct access).

Source

pub fn base_url(&self) -> &Url

The controller base URL.

Source

pub fn platform(&self) -> ControllerPlatform

The detected controller platform.

Source

pub fn cookie_header(&self) -> Option<String>

Extract the session cookie header value for WebSocket auth.

Returns the Cookie header string (e.g. "TOKEN=abc123") if a cookie jar is available and contains cookies for the controller URL.

Source§

impl LegacyClient

Source

pub async fn list_clients(&self) -> Result<Vec<LegacyClientEntry>, Error>

List all currently connected clients (stations).

GET /api/s/{site}/stat/sta

Source

pub async fn block_client(&self, mac: &str) -> Result<(), Error>

Block a client by MAC address.

POST /api/s/{site}/cmd/stamgr with {"cmd": "block-sta", "mac": "..."}

Source

pub async fn unblock_client(&self, mac: &str) -> Result<(), Error>

Unblock a client by MAC address.

POST /api/s/{site}/cmd/stamgr with {"cmd": "unblock-sta", "mac": "..."}

Source

pub async fn kick_client(&self, mac: &str) -> Result<(), Error>

Disconnect (kick) a client.

POST /api/s/{site}/cmd/stamgr with {"cmd": "kick-sta", "mac": "..."}

Source

pub async fn forget_client(&self, mac: &str) -> Result<(), Error>

Forget (permanently remove) a client by MAC address.

POST /api/s/{site}/cmd/stamgr with {"cmd": "forget-sta", "macs": [...]}

Source

pub async fn authorize_guest( &self, mac: &str, minutes: u32, up_kbps: Option<u32>, down_kbps: Option<u32>, quota_mb: Option<u32>, ) -> Result<(), Error>

Authorize a guest client on the hotspot portal.

POST /api/s/{site}/cmd/stamgr with guest authorization parameters.

  • mac: Client MAC address
  • minutes: Authorization duration in minutes
  • up_kbps: Optional upload bandwidth limit (Kbps)
  • down_kbps: Optional download bandwidth limit (Kbps)
  • quota_mb: Optional data transfer quota (MB)
Source

pub async fn unauthorize_guest(&self, mac: &str) -> Result<(), Error>

Revoke guest authorization for a client.

POST /api/s/{site}/cmd/stamgr with {"cmd": "unauthorize-guest", "mac": "..."}

Source§

impl LegacyClient

Source

pub async fn list_devices(&self) -> Result<Vec<LegacyDevice>, Error>

List all devices with full statistics.

GET /api/s/{site}/stat/device

Source

pub async fn get_device(&self, mac: &str) -> Result<Option<LegacyDevice>, Error>

Get a single device by MAC address.

Filters the device list by MAC. Returns None if no device matches.

Source

pub async fn adopt_device(&self, mac: &str) -> Result<(), Error>

Adopt a pending device.

POST /api/s/{site}/cmd/devmgr with {"cmd": "adopt", "mac": "..."}

Source

pub async fn restart_device(&self, mac: &str) -> Result<(), Error>

Restart a device.

POST /api/s/{site}/cmd/devmgr with {"cmd": "restart", "mac": "..."}

Source

pub async fn upgrade_device( &self, mac: &str, firmware_url: Option<&str>, ) -> Result<(), Error>

Upgrade device firmware.

If url is Some, upgrades from that URL (cmd: "upgrade-external"). Otherwise upgrades from Ubiquiti’s cloud (cmd: "upgrade").

Source

pub async fn provision_device(&self, mac: &str) -> Result<(), Error>

Force re-provision a device configuration.

POST /api/s/{site}/cmd/devmgr with {"cmd": "force-provision", "mac": "..."}

Source

pub async fn speedtest(&self) -> Result<(), Error>

Trigger a site speed test (gateway).

POST /api/s/{site}/cmd/devmgr with {"cmd": "speedtest"}

Source

pub async fn locate_device(&self, mac: &str, enable: bool) -> Result<(), Error>

Toggle the LED locator on a device.

enable: true sends set-locate, false sends unset-locate.

Source§

impl LegacyClient

Source

pub async fn list_events( &self, count: Option<u32>, ) -> Result<Vec<LegacyEvent>, Error>

List recent events.

GET /api/s/{site}/stat/event

If count is provided, limits the number of events returned by appending ?_limit={count} to the request.

Source

pub async fn list_alarms(&self) -> Result<Vec<LegacyAlarm>, Error>

List active alarms.

GET /api/s/{site}/stat/alarm

Source

pub async fn archive_alarm(&self, id: &str) -> Result<(), Error>

Archive (acknowledge) a specific alarm by its ID.

POST /api/s/{site}/cmd/evtmgr with {"cmd": "archive-alarm", "_id": "..."}

Source

pub async fn archive_all_alarms(&self) -> Result<(), Error>

Archive all alarms for the active site.

POST /api/s/{site}/cmd/evtmgr with {"cmd": "archive-all-alarms"}

Source§

impl LegacyClient

Source

pub async fn list_sites(&self) -> Result<Vec<LegacySite>, Error>

List all sites visible to the authenticated user.

GET /api/self/sites (controller-level, not site-scoped)

Source

pub async fn create_site( &self, name: &str, description: &str, ) -> Result<(), Error>

Create a new site.

POST /api/s/{site}/cmd/sitemgr with {"cmd": "add-site", "name": "...", "desc": "..."}

Source

pub async fn delete_site(&self, name: &str) -> Result<(), Error>

Delete a site.

POST /api/s/{site}/cmd/sitemgr with {"cmd": "delete-site", "name": "..."}

Source

pub async fn invite_admin( &self, name: &str, email: &str, role: &str, ) -> Result<(), Error>

Invite a site administrator.

POST /api/s/{site}/cmd/sitemgr with {"cmd": "invite-admin", ...}

Source

pub async fn revoke_admin(&self, admin_id: &str) -> Result<(), Error>

Revoke a site administrator.

POST /api/s/{site}/cmd/sitemgr with {"cmd": "revoke-admin", "admin": "..."}

Source

pub async fn update_admin( &self, admin_id: &str, role: Option<&str>, ) -> Result<(), Error>

Update site administrator role.

POST /api/s/{site}/cmd/sitemgr with {"cmd": "update-admin", ...}

Source§

impl LegacyClient

Source

pub async fn get_site_stats( &self, interval: &str, start: Option<i64>, end: Option<i64>, attrs: Option<&[String]>, ) -> Result<Vec<Value>, Error>

Fetch site-level historical statistics.

POST /api/s/{site}/stat/report/{interval}.site

The interval parameter should be one of: "5minutes", "hourly", "daily". Returns loosely-typed JSON because the field set varies by report type.

Source

pub async fn get_device_stats( &self, interval: &str, macs: Option<&[String]>, attrs: Option<&[String]>, ) -> Result<Vec<Value>, Error>

Fetch per-device historical statistics.

POST /api/s/{site}/stat/report/{interval}.device

If macs is provided, results are filtered to those devices.

Source

pub async fn get_client_stats( &self, interval: &str, macs: Option<&[String]>, attrs: Option<&[String]>, ) -> Result<Vec<Value>, Error>

Fetch per-client historical statistics.

POST /api/s/{site}/stat/report/{interval}.user

If macs is provided, results are filtered to those clients.

Source

pub async fn get_gateway_stats( &self, interval: &str, start: Option<i64>, end: Option<i64>, attrs: Option<&[String]>, ) -> Result<Vec<Value>, Error>

Fetch gateway historical statistics.

POST /api/s/{site}/stat/report/{interval}.gw

Source

pub async fn get_dpi_stats( &self, group_by: &str, macs: Option<&[String]>, ) -> Result<Vec<Value>, Error>

Fetch site-wide DPI (Deep Packet Inspection) statistics.

POST /api/s/{site}/stat/sitedpi with {"type": "by_app"} body.

The group_by parameter selects the DPI grouping: "by_app" or "by_cat". Returns empty data if DPI tracking is not enabled on the controller.

Source§

impl LegacyClient

Source

pub async fn get_sysinfo(&self) -> Result<Value, Error>

Get controller system information.

GET /api/s/{site}/stat/sysinfo

Returns loosely-typed JSON because the field set varies by platform and firmware version.

Source

pub async fn get_health(&self) -> Result<Vec<Value>, Error>

Get site health dashboard metrics.

GET /api/s/{site}/stat/health

Returns subsystem health entries (wan, lan, wlan, vpn, etc.).

Source

pub async fn list_backups(&self) -> Result<Vec<Value>, Error>

List available controller backups.

POST /api/s/{site}/cmd/backup with {"cmd": "list-backups"}

Source

pub async fn delete_backup(&self, filename: &str) -> Result<(), Error>

Delete a backup file from the controller.

POST /api/s/{site}/cmd/backup with {"cmd": "delete-backup", "filename": "..."}

Source

pub async fn download_backup(&self, filename: &str) -> Result<Vec<u8>, Error>

Download a backup file from the controller.

GET /dl/autobackup/{filename}

Source

pub async fn list_admins(&self) -> Result<Vec<Value>, Error>

List controller admins.

GET /api/stat/admin — controller-level (not site-scoped).

Source

pub async fn create_backup(&self) -> Result<(), Error>

Create a new controller backup.

POST /api/s/{site}/cmd/backup with {"cmd": "backup"}

Source

pub async fn reboot_controller(&self) -> Result<(), Error>

Reboot the controller.

POST /api/system/reboot

Source

pub async fn poweroff_controller(&self) -> Result<(), Error>

Power off the controller.

POST /api/system/poweroff

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more