141 lines
4.5 KiB
Python
141 lines
4.5 KiB
Python
from rest_framework.exceptions import ParseError, APIException
|
|
from apps.third.tapis import dhapis, xxapis
|
|
from apps.third.erros import TAPI_CODE_WRONG
|
|
from apps.utils.dahua import dhClient
|
|
from apps.utils.errors import XX_REQUEST_ERROR
|
|
from apps.utils.xunxi import xxClient
|
|
from rest_framework.response import Response
|
|
from rest_framework.views import APIView
|
|
from rest_framework.permissions import IsAuthenticated
|
|
from apps.utils.viewsets import CustomGenericViewSet
|
|
from rest_framework.mixins import CreateModelMixin
|
|
from rest_framework.decorators import action
|
|
|
|
from apps.third.serializers import RequestCommonSerializer
|
|
# Create your views here.
|
|
|
|
|
|
class DahuaTestView(APIView):
|
|
"""
|
|
大华测试接口
|
|
"""
|
|
permission_classes = [IsAuthenticated]
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
data = {
|
|
"data":{
|
|
"channelId": "1001339$1$0$0",
|
|
"streamType": "1",
|
|
"type": "hls"
|
|
}
|
|
}
|
|
# ok, res = dhClient.request(
|
|
# url='/evo-apigw/admin/API/video/stream/realtime', method='post', json=data)
|
|
ok, res = dhClient.request(url='/evo-apigw/evo-brm/1.2.0/department/tree',
|
|
method='get')
|
|
# data = {
|
|
# "pageNum":1,
|
|
# "pageSize":100,
|
|
# "isOnline":1,
|
|
# "showChildNodeData":1,
|
|
# "categorys":[8]
|
|
|
|
# }
|
|
# res = dhClient.request(
|
|
# url='/evo-apigw/evo-brm/1.0.0/device/subsystem/page', method='post', json=data)
|
|
# data = {
|
|
# "channelCodeList": ["1001382$7$0$0"]
|
|
# }
|
|
# res = dhClient.request(
|
|
# url='/evo-apigw/evo-accesscontrol/1.0.0/card/accessControl/channelControl/closeDoor', method='post', json=data)
|
|
if ok == 'success':
|
|
return Response(res)
|
|
elif ok == 'fail':
|
|
raise ParseError(**res)
|
|
else:
|
|
raise APIException(**res)
|
|
|
|
|
|
class XxTestView(APIView):
|
|
"""
|
|
寻息测试接口
|
|
"""
|
|
permission_classes = [IsAuthenticated]
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
ok, res = xxClient.request(
|
|
url='/api/application/build/buildListV2', json={})
|
|
if ok == 'success':
|
|
return Response(res)
|
|
elif ok == 'fail':
|
|
raise ParseError(**res)
|
|
else:
|
|
raise APIException(**res)
|
|
|
|
|
|
class XxCommonViewSet(CreateModelMixin, CustomGenericViewSet):
|
|
"""
|
|
寻息通用调用接口
|
|
"""
|
|
perms_map = {'post': '*'}
|
|
serializer_class = RequestCommonSerializer
|
|
|
|
def create(self, request, *args, **kwargs):
|
|
serializer = self.get_serializer(data=request.data)
|
|
serializer.is_valid(raise_exception=True)
|
|
vdata = serializer.validated_data
|
|
if vdata.get('code', ''):
|
|
try:
|
|
dhapi = dhapis[vdata['code']]
|
|
except:
|
|
raise ParseError(**TAPI_CODE_WRONG)
|
|
vdata['url'] = dhapi['url']
|
|
vdata['method'] = dhapi['method']
|
|
_, res = xxClient.request(
|
|
url=vdata['url'],
|
|
method=vdata.get('method', 'post'),
|
|
params=vdata.get('params', {}),
|
|
json=vdata.get('data', {}))
|
|
return Response(res)
|
|
|
|
@action(methods=['get'], detail=False, permission_classes=[IsAuthenticated])
|
|
def codes(self, request, pk=None):
|
|
"""获取请求短标识
|
|
|
|
获取请求短标识
|
|
"""
|
|
return Response(xxapis)
|
|
|
|
class DhCommonViewSet(CreateModelMixin, CustomGenericViewSet):
|
|
"""
|
|
大华通用调用接口
|
|
"""
|
|
perms_map = {'post': '*'}
|
|
serializer_class = RequestCommonSerializer
|
|
|
|
def create(self, request, *args, **kwargs):
|
|
serializer = self.get_serializer(data=request.data)
|
|
serializer.is_valid(raise_exception=True)
|
|
vdata = serializer.validated_data
|
|
if vdata.get('code', ''):
|
|
try:
|
|
xxapi = xxapis[vdata['code']]
|
|
except:
|
|
raise ParseError(**TAPI_CODE_WRONG)
|
|
vdata['url'] = xxapi['url']
|
|
vdata['method'] = xxapi['method']
|
|
_, res = dhClient.request(
|
|
url=vdata['url'],
|
|
method=vdata.get('method', 'post'),
|
|
params=vdata.get('params', {}),
|
|
json=vdata.get('data', {}))
|
|
return Response(res)
|
|
|
|
@action(methods=['get'], detail=False, permission_classes=[IsAuthenticated])
|
|
def codes(self, request, pk=None):
|
|
"""获取请求短标识
|
|
|
|
获取请求短标识
|
|
"""
|
|
return Response(dhapis)
|