Auth order plz

This commit is contained in:
2025-02-07 23:17:12 -05:00
parent 3c4a9651b5
commit 0417106e88

21
app.py
View File

@ -49,17 +49,16 @@ class UnifiAPI:
self.base_url = config['unifi']['controller']
self.session = requests.Session()
self.session.verify = False
self.api_key = config['unifi']['api_key']
# Get CSRF token first
csrf_response = self.session.get(f"{self.base_url}/api/auth/csrf-token")
csrf_token = csrf_response.headers.get('X-CSRF-Token')
self.headers = {
'X-CSRF-Token': csrf_token,
'X-API-KEY': self.api_key,
'Accept': 'application/json'
'X-API-KEY': config['unifi']['api_key'],
'Accept': 'application/json',
'Content-Type': 'application/json'
}
# First authenticate
auth_url = f"{self.base_url}/api/auth/login"
auth_data = {'username': 'admin', 'apiKey': config['unifi']['api_key']}
self.session.post(auth_url, json=auth_data, headers=self.headers)
self.site_id = self.get_site_id()
def get_site_id(self):
@ -68,12 +67,12 @@ class UnifiAPI:
response.raise_for_status()
sites = response.json()['data']
return sites[0]['_id']
def get_all_devices(self):
url = f"{self.base_url}/proxy/network/api/s/{self.site_id}/stat/device"
response = self.session.get(url, headers=self.headers)
response.raise_for_status()
return response.json()['data']
return response.json()['data']
def get_device_details(self, device_id):
url = f"{self.base_url}/proxy/network/integration/v1/sites/{self.site_id}/devices/{device_id}"
response = self.session.get(url, headers=self.headers)