95 lines
3.3 KiB
Python
95 lines
3.3 KiB
Python
import requests
|
|
import json
|
|
|
|
from . import API_URL
|
|
from .auth import AuthAPI
|
|
|
|
class DeviceAPI():
|
|
@staticmethod
|
|
def create(name, serial_number, mac_address):
|
|
post_form_data = {
|
|
"name": str(name),
|
|
"serial_number": str(serial_number),
|
|
"mac_address": str(mac_address),
|
|
}
|
|
|
|
try:
|
|
ret = requests.post(API_URL + 'api/device/create', post_form_data, headers= AuthAPI.get_key())
|
|
if ret.status_code == 200:
|
|
return True
|
|
except requests.exceptions.ConnectionError as e:
|
|
print('create device fail', e)
|
|
return
|
|
|
|
@staticmethod
|
|
def getByMac(mac_address):
|
|
try:
|
|
ret = requests.get(API_URL + 'api/device/get_by_mac/' + mac_address, headers= AuthAPI.get_key())
|
|
if ret.status_code == 200:
|
|
return ret.json()
|
|
except (requests.exceptions.ConnectionError, json.decoder.JSONDecodeError) as e:
|
|
print('get device by mac fail', e)
|
|
return
|
|
|
|
@staticmethod
|
|
def updateByAttrs(attrs:str, values:str, datas:dict):
|
|
try:
|
|
ret = requests.post(API_URL + 'api/device/update_by_attr/' + str(attrs) + '/' + str(values), datas, headers= AuthAPI.get_key())
|
|
if ret.status_code == 200:
|
|
return ret.json()
|
|
except requests.exceptions.ConnectionError as e:
|
|
print('update device by attrs fail', e)
|
|
return
|
|
|
|
@staticmethod
|
|
def updateByID(id:int, attrs:dict):
|
|
try:
|
|
ret = requests.post(API_URL + 'api/device/update_by_id/' + str(id), attrs, headers= AuthAPI.get_key())
|
|
if ret.status_code == 200:
|
|
return True
|
|
except requests.exceptions.ConnectionError as e:
|
|
print('update device by id fail', e)
|
|
return
|
|
|
|
@staticmethod
|
|
def updateByMac(mac_address:str, attrs:dict):
|
|
print('mac_address', mac_address)
|
|
try:
|
|
ret = requests.post(API_URL + 'api/device/update_by_mac/' + str(mac_address), attrs, headers= AuthAPI.get_key())
|
|
if ret.status_code == 200:
|
|
return True
|
|
except requests.exceptions.ConnectionError as e:
|
|
print('update device by id fail', e)
|
|
return
|
|
|
|
@staticmethod
|
|
def updateAll(attrs:dict):
|
|
try:
|
|
ret = requests.post(API_URL + 'api/device/update/', attrs, headers= AuthAPI.get_key())
|
|
if ret.status_code == 200:
|
|
return True
|
|
except requests.exceptions.ConnectionError as e:
|
|
print('update all devices fail', e)
|
|
return
|
|
|
|
@staticmethod
|
|
def getByAttrs(attrs:dict, values:dict):
|
|
try:
|
|
ret = requests.get(API_URL + 'api/device/get_by_attr/' + str(attrs) + '/' + str(values), headers= AuthAPI.get_key())
|
|
if ret.status_code == 200:
|
|
return ret.json()
|
|
except (requests.exceptions.ConnectionError, json.decoder.JSONDecodeError) as e:
|
|
print('get device by attrs fail', e)
|
|
return
|
|
|
|
@staticmethod
|
|
def getAll():
|
|
try:
|
|
ret = requests.get(API_URL + 'api/device/get/all', headers= AuthAPI.get_key())
|
|
if ret.status_code == 200:
|
|
return ret.json()
|
|
except (requests.exceptions.ConnectionError, json.decoder.JSONDecodeError) as e:
|
|
print('get all devices fail', e)
|
|
return
|
|
|
|
|