[Core] UniFi Client: aiounifi wrapper with UniFi OS detection #2

Closed
opened 2026-01-01 20:06:41 -06:00 by b3nw · 0 comments
Owner

Overview

Create unifi_client.py - a clean abstraction layer wrapping the aiounifi library.

Tasks

  • Create UnifiClient class with async context manager support
  • Implement authentication via aiounifi.Controller
  • Implement UniFi OS detection (probe both path structures)
  • Implement raw_request(method, endpoint, payload) for pass-through
  • Implement write protection check (respect UNIFI_ALLOW_WRITES)
  • Add proper error handling and logging
  • Manage aiohttp.ClientSession lifecycle

UniFi OS Detection Logic

Probe both paths during init:

  • Direct: /api/s/{site}/...
  • Proxy (UniFi OS): /proxy/network/api/s/{site}/...

Write Protection

WRITE_METHODS = {'POST', 'PUT', 'DELETE', 'PATCH'}

def raw_request(self, method, endpoint, payload=None):
    if method.upper() in WRITE_METHODS and not self.allow_writes:
        raise PermissionError("Write operations disabled. Set UNIFI_ALLOW_WRITES=true")

Acceptance Criteria

  • Successfully authenticates to UniFi controller
  • Auto-detects UniFi OS vs standalone controller
  • Blocks write operations unless explicitly enabled
  • Proper async cleanup on shutdown
## Overview Create `unifi_client.py` - a clean abstraction layer wrapping the `aiounifi` library. ## Tasks - [ ] Create `UnifiClient` class with async context manager support - [ ] Implement authentication via `aiounifi.Controller` - [ ] Implement UniFi OS detection (probe both path structures) - [ ] Implement `raw_request(method, endpoint, payload)` for pass-through - [ ] Implement write protection check (respect `UNIFI_ALLOW_WRITES`) - [ ] Add proper error handling and logging - [ ] Manage `aiohttp.ClientSession` lifecycle ## UniFi OS Detection Logic Probe both paths during init: - Direct: `/api/s/{site}/...` - Proxy (UniFi OS): `/proxy/network/api/s/{site}/...` ## Write Protection ```python WRITE_METHODS = {'POST', 'PUT', 'DELETE', 'PATCH'} def raw_request(self, method, endpoint, payload=None): if method.upper() in WRITE_METHODS and not self.allow_writes: raise PermissionError("Write operations disabled. Set UNIFI_ALLOW_WRITES=true") ``` ## Acceptance Criteria - Successfully authenticates to UniFi controller - Auto-detects UniFi OS vs standalone controller - Blocks write operations unless explicitly enabled - Proper async cleanup on shutdown
b3nw closed this issue 2026-01-01 20:21:24 -06:00
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: b3nw/unifi-mcp-light#2