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
impl LegacyClient
Sourcepub async fn login(
&self,
username: &str,
password: &SecretString,
) -> Result<(), Error>
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
Sourcepub async fn logout(&self) -> Result<(), Error>
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
Sourcepub async fn detect_platform(
base_url: &Url,
) -> Result<ControllerPlatform, Error>
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
impl LegacyClient
Sourcepub fn new(
base_url: Url,
site: String,
platform: ControllerPlatform,
transport: &TransportConfig,
) -> Result<Self, Error>
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).
Sourcepub fn with_client(
http: Client,
base_url: Url,
site: String,
platform: ControllerPlatform,
) -> Self
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).
Sourcepub fn http(&self) -> &Client
pub fn http(&self) -> &Client
The underlying HTTP client (for auth flows that need direct access).
Sourcepub fn platform(&self) -> ControllerPlatform
pub fn platform(&self) -> ControllerPlatform
The detected controller platform.
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
impl LegacyClient
Sourcepub async fn list_clients(&self) -> Result<Vec<LegacyClientEntry>, Error>
pub async fn list_clients(&self) -> Result<Vec<LegacyClientEntry>, Error>
List all currently connected clients (stations).
GET /api/s/{site}/stat/sta
Sourcepub async fn block_client(&self, mac: &str) -> Result<(), Error>
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": "..."}
Sourcepub async fn unblock_client(&self, mac: &str) -> Result<(), Error>
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": "..."}
Sourcepub async fn kick_client(&self, mac: &str) -> Result<(), Error>
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": "..."}
Sourcepub async fn forget_client(&self, mac: &str) -> Result<(), Error>
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": [...]}
Authorize a guest client on the hotspot portal.
POST /api/s/{site}/cmd/stamgr with guest authorization parameters.
mac: Client MAC addressminutes: Authorization duration in minutesup_kbps: Optional upload bandwidth limit (Kbps)down_kbps: Optional download bandwidth limit (Kbps)quota_mb: Optional data transfer quota (MB)
Revoke guest authorization for a client.
POST /api/s/{site}/cmd/stamgr with {"cmd": "unauthorize-guest", "mac": "..."}
Source§impl LegacyClient
impl LegacyClient
Sourcepub async fn list_devices(&self) -> Result<Vec<LegacyDevice>, Error>
pub async fn list_devices(&self) -> Result<Vec<LegacyDevice>, Error>
List all devices with full statistics.
GET /api/s/{site}/stat/device
Sourcepub async fn get_device(&self, mac: &str) -> Result<Option<LegacyDevice>, Error>
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.
Sourcepub async fn adopt_device(&self, mac: &str) -> Result<(), Error>
pub async fn adopt_device(&self, mac: &str) -> Result<(), Error>
Adopt a pending device.
POST /api/s/{site}/cmd/devmgr with {"cmd": "adopt", "mac": "..."}
Sourcepub async fn restart_device(&self, mac: &str) -> Result<(), Error>
pub async fn restart_device(&self, mac: &str) -> Result<(), Error>
Restart a device.
POST /api/s/{site}/cmd/devmgr with {"cmd": "restart", "mac": "..."}
Sourcepub async fn upgrade_device(
&self,
mac: &str,
firmware_url: Option<&str>,
) -> Result<(), Error>
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").
Sourcepub async fn provision_device(&self, mac: &str) -> Result<(), Error>
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§impl LegacyClient
impl LegacyClient
Sourcepub async fn list_events(
&self,
count: Option<u32>,
) -> Result<Vec<LegacyEvent>, Error>
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.
Sourcepub async fn list_alarms(&self) -> Result<Vec<LegacyAlarm>, Error>
pub async fn list_alarms(&self) -> Result<Vec<LegacyAlarm>, Error>
List active alarms.
GET /api/s/{site}/stat/alarm
Sourcepub async fn archive_alarm(&self, id: &str) -> Result<(), Error>
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": "..."}
Sourcepub async fn archive_all_alarms(&self) -> Result<(), Error>
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
impl LegacyClient
Sourcepub async fn list_sites(&self) -> Result<Vec<LegacySite>, Error>
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)
Sourcepub async fn create_site(
&self,
name: &str,
description: &str,
) -> Result<(), Error>
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": "..."}
Sourcepub async fn delete_site(&self, name: &str) -> Result<(), Error>
pub async fn delete_site(&self, name: &str) -> Result<(), Error>
Delete a site.
POST /api/s/{site}/cmd/sitemgr with {"cmd": "delete-site", "name": "..."}
Sourcepub async fn invite_admin(
&self,
name: &str,
email: &str,
role: &str,
) -> Result<(), Error>
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§impl LegacyClient
impl LegacyClient
Sourcepub async fn get_site_stats(
&self,
interval: &str,
start: Option<i64>,
end: Option<i64>,
attrs: Option<&[String]>,
) -> Result<Vec<Value>, Error>
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.
Sourcepub async fn get_device_stats(
&self,
interval: &str,
macs: Option<&[String]>,
attrs: Option<&[String]>,
) -> Result<Vec<Value>, Error>
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.
Sourcepub async fn get_client_stats(
&self,
interval: &str,
macs: Option<&[String]>,
attrs: Option<&[String]>,
) -> Result<Vec<Value>, Error>
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.
Sourcepub async fn get_gateway_stats(
&self,
interval: &str,
start: Option<i64>,
end: Option<i64>,
attrs: Option<&[String]>,
) -> Result<Vec<Value>, Error>
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
Sourcepub async fn get_dpi_stats(
&self,
group_by: &str,
macs: Option<&[String]>,
) -> Result<Vec<Value>, Error>
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
impl LegacyClient
Sourcepub async fn get_sysinfo(&self) -> Result<Value, Error>
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.
Sourcepub async fn get_health(&self) -> Result<Vec<Value>, Error>
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.).
Sourcepub async fn list_backups(&self) -> Result<Vec<Value>, Error>
pub async fn list_backups(&self) -> Result<Vec<Value>, Error>
List available controller backups.
POST /api/s/{site}/cmd/backup with {"cmd": "list-backups"}
Sourcepub async fn delete_backup(&self, filename: &str) -> Result<(), Error>
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": "..."}
Sourcepub async fn download_backup(&self, filename: &str) -> Result<Vec<u8>, Error>
pub async fn download_backup(&self, filename: &str) -> Result<Vec<u8>, Error>
Download a backup file from the controller.
GET /dl/autobackup/{filename}
Sourcepub async fn list_admins(&self) -> Result<Vec<Value>, Error>
pub async fn list_admins(&self) -> Result<Vec<Value>, Error>
List controller admins.
GET /api/stat/admin — controller-level (not site-scoped).
Sourcepub async fn create_backup(&self) -> Result<(), Error>
pub async fn create_backup(&self) -> Result<(), Error>
Create a new controller backup.
POST /api/s/{site}/cmd/backup with {"cmd": "backup"}
Sourcepub async fn reboot_controller(&self) -> Result<(), Error>
pub async fn reboot_controller(&self) -> Result<(), Error>
Reboot the controller.
POST /api/system/reboot
Sourcepub async fn poweroff_controller(&self) -> Result<(), Error>
pub async fn poweroff_controller(&self) -> Result<(), Error>
Power off the controller.
POST /api/system/poweroff