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.mixins import MyLoggingMixin 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(MyLoggingMixin, APIView): """ 大华测试接口 """ permission_classes = [IsAuthenticated] def get(self, request, *args, **kwargs): # file_path_rela = '/media/default/avatar.png' # _, res = dhClient.request(**dhapis['person_img_upload'], file_path_rela=file_path_rela) _,res = dhClient.request( url='/evo-apigw/evo-brm/1.0.0/person/subsystem/{}'.format(2059335), method='get') return Response(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('json', {})) 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('json', {})) return Response(res) @action(methods=['get'], detail=False, permission_classes=[IsAuthenticated]) def codes(self, request, pk=None): """获取请求短标识 获取请求短标识 """ return Response(dhapis)