dingxin_toolbox
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
2.8 KiB

import json
import requests
from django.db.models import Q
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import filters
from rest_framework import viewsets
from django.views.decorators.csrf import csrf_exempt
from dspt_api.models import EcChannel, EcEnv, EcApi, EcApiParams, EcCinemaIds, EcRequestLog, EcApiGroup
from dspt_api.serializers import EcChannelSerializer, EcEnvSerializer, EcApiSerializer, EcApiParamsSerializer, \
EcCinemaIdsSerializer, EcRequestLogSerializer, EcApiGroupSerializer
from util.sign import Sign
# Create your views here.
class EcChannelViewSet(viewsets.ModelViewSet):
queryset = EcChannel.objects.all()
serializer_class = EcChannelSerializer
class EcEnvViewSet(viewsets.ModelViewSet):
queryset = EcEnv.objects.all()
serializer_class = EcEnvSerializer
class EcApiViewSet(viewsets.ModelViewSet):
queryset = EcApi.objects.all()
serializer_class = EcApiSerializer
class EcApiParamsViewSet(viewsets.ModelViewSet):
queryset = EcApiParams.objects.all()
serializer_class = EcApiParamsSerializer
filter_backends = (filters.SearchFilter, DjangoFilterBackend)
# http://172.16.1.114:8000/ec/get_api_params?api_id=1
filterset_fields = ('api_id',)
class EcCinemaIdsViewSet(viewsets.ModelViewSet):
queryset = EcCinemaIds
serializer_class = EcCinemaIdsSerializer
class EcRequestLogViewSet(viewsets.ModelViewSet):
queryset = EcRequestLog.objects.all()
serializer_class = EcRequestLogSerializer
class EcApiGroupViewSet(viewsets.ModelViewSet):
queryset = EcApiGroup.objects.all()
serializer_class = EcApiGroupSerializer
@csrf_exempt
def general_api_url(request):
req_obj = handle_request(request)
return req_obj.url
def handle_request(_request):
"""
POST请求参数
env dspt或zyds
member_type member或nonmember
api api地址
params 请求参数
"""
req = _request.POST
env = req.get('env')
member_type = req.get('member_type')
base_url = EcEnv.objects.filter(Q(code=env) & Q(type=member_type)).first().get('host')
api = req.get('api')
params = json.loads(req.get('params'))
pid = params.get('pid')
key = EcChannel.objects.filter(Q(pid=pid) & Q(env=env)).first().get('channel_key')
sign = Sign(key)
req_params = sign.add_sig(params)
req_obj = prepare_request(base_url, api, req_params)
return req_obj
def prepare_request(_url, _api, _params):
req = requests.PreparedRequest()
req.prepare_method('GET')
req.prepare_url(url=_url + '/' + _api, params=_params)
req.prepare_headers({'host': _url[7:]})
return req
@csrf_exempt
def send_request(request):
req = handle_request(request)
response = requests.Session().send(req)
# 插入数据
return response.json()