mirror of
https://github.com/i701/sarlink-portal-api.git
synced 2025-06-28 05:26:07 +00:00
Refactor device blocking logic in DeviceBlockAPIView to utilize Omada BlockDeviceResponse for blocking in omada first
Some checks failed
Build and Push Docker Images / Build and Push Docker Images (push) Failing after 3m42s
Some checks failed
Build and Push Docker Images / Build and Push Docker Images (push) Failing after 3m42s
This commit is contained in:
30
api/omada.py
30
api/omada.py
@ -1,5 +1,12 @@
|
||||
import requests
|
||||
from apibase.env import env
|
||||
from dataclasses import dataclass
|
||||
|
||||
|
||||
@dataclass
|
||||
class BlockDeviceResponse:
|
||||
errorCode: int
|
||||
msg: str
|
||||
|
||||
|
||||
class Omada:
|
||||
@ -92,7 +99,9 @@ class Omada:
|
||||
except requests.RequestException as e:
|
||||
print(f"Error adding devices to Omada: {e}")
|
||||
|
||||
def block_device(self, mac_address: str, operation: str = "block"):
|
||||
def block_device(
|
||||
self, mac_address: str, operation: str = "block"
|
||||
) -> BlockDeviceResponse:
|
||||
"""
|
||||
Block a device in Omada.
|
||||
:param mac_address: The MAC address of the device to block.
|
||||
@ -103,10 +112,23 @@ class Omada:
|
||||
url,
|
||||
headers={"X-API-Key": str(self.api_key)},
|
||||
)
|
||||
if response.status_code == 200:
|
||||
data = response.json()
|
||||
return data
|
||||
data = response.json()
|
||||
print("Response Data: ", data)
|
||||
if data["errorCode"] == 0:
|
||||
return BlockDeviceResponse(
|
||||
errorCode=data["errorCode"],
|
||||
msg=data["msg"]
|
||||
if "msg" in data
|
||||
else "Device blocked successfully.",
|
||||
)
|
||||
else:
|
||||
print(f"Failed to block device: {response.text}")
|
||||
return BlockDeviceResponse(
|
||||
errorCode=data["errorCode"],
|
||||
msg=data["msg"] if "msg" in data else "Failed to block device.",
|
||||
)
|
||||
except requests.RequestException as e:
|
||||
print(f"Error blocking device in Omada: {e}")
|
||||
return BlockDeviceResponse(
|
||||
errorCode=-1, msg=f"Error blocking device in Omada: {e}"
|
||||
)
|
||||
|
Reference in New Issue
Block a user