|
|
@ -1,3 +1,4 @@ |
|
|
|
|
|
|
|
import datetime |
|
|
|
import json |
|
|
|
import json |
|
|
|
import requests |
|
|
|
import requests |
|
|
|
from urllib.parse import urljoin |
|
|
|
from urllib.parse import urljoin |
|
|
@ -11,6 +12,7 @@ from dspt_api.models import EcChannel, EcEnv, EcApi, EcApiParams, EcCinemaIds, E |
|
|
|
from dspt_api.serializers import EcChannelSerializer, EcEnvSerializer, EcApiSerializer, EcApiParamsSerializer, \ |
|
|
|
from dspt_api.serializers import EcChannelSerializer, EcEnvSerializer, EcApiSerializer, EcApiParamsSerializer, \ |
|
|
|
EcCinemaIdsSerializer, EcRequestLogSerializer, EcApiGroupSerializer |
|
|
|
EcCinemaIdsSerializer, EcRequestLogSerializer, EcApiGroupSerializer |
|
|
|
from django.http.response import JsonResponse |
|
|
|
from django.http.response import JsonResponse |
|
|
|
|
|
|
|
from django.utils import timezone |
|
|
|
from dspt_api.util.sign import Sign |
|
|
|
from dspt_api.util.sign import Sign |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -79,7 +81,7 @@ def handle_request(_request): |
|
|
|
sign = Sign(key) |
|
|
|
sign = Sign(key) |
|
|
|
req_params = sign.add_sig(params) |
|
|
|
req_params = sign.add_sig(params) |
|
|
|
req_obj = prepare_request(base_url, api, req_params) |
|
|
|
req_obj = prepare_request(base_url, api, req_params) |
|
|
|
return req_obj |
|
|
|
return req_obj, req_params['_sig'] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 内部方法,用于拼接请求 |
|
|
|
# 内部方法,用于拼接请求 |
|
|
@ -125,8 +127,8 @@ def general_api_url(request): |
|
|
|
api api地址 |
|
|
|
api api地址 |
|
|
|
params 请求参数 |
|
|
|
params 请求参数 |
|
|
|
""" |
|
|
|
""" |
|
|
|
req_obj = handle_request(request) |
|
|
|
req_obj, sig = handle_request(request) |
|
|
|
return JsonResponse({'url': req_obj.url}) |
|
|
|
return JsonResponse({'url': req_obj.url, 'sig': sig}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 对外接口,用于收集用户提交内容后发送请求 |
|
|
|
# 对外接口,用于收集用户提交内容后发送请求 |
|
|
@ -142,31 +144,36 @@ def send_request(request): |
|
|
|
# 初始化redis |
|
|
|
# 初始化redis |
|
|
|
redis_conn = get_redis_connection() |
|
|
|
redis_conn = get_redis_connection() |
|
|
|
user_ip = request.META.get('REMOTE_ADDR') |
|
|
|
user_ip = request.META.get('REMOTE_ADDR') |
|
|
|
api = request.get('api') |
|
|
|
web_req = json.loads(request.body) |
|
|
|
|
|
|
|
api = web_req.get('api') |
|
|
|
redis_key_api = f'dspt_api_{user_ip}_{api}' |
|
|
|
redis_key_api = f'dspt_api_{user_ip}_{api}' |
|
|
|
redis_key_user_data = f'dspt_api_{user_ip}_{api}_data' |
|
|
|
redis_key_user_data = f'dspt_api_{user_ip}_{api}_data' |
|
|
|
# 发送请求 |
|
|
|
# 发送请求 |
|
|
|
req = handle_request(request) |
|
|
|
req, sig = handle_request(request) |
|
|
|
response = requests.Session().send(req) |
|
|
|
response = requests.Session().send(req) |
|
|
|
|
|
|
|
print('response', response) |
|
|
|
# 记录Redis |
|
|
|
# 记录Redis |
|
|
|
if redis_conn.exists(redis_key_api): |
|
|
|
if redis_conn.exists(redis_key_api): |
|
|
|
redis_conn.delete(redis_key_api) |
|
|
|
redis_conn.delete(redis_key_api) |
|
|
|
else: |
|
|
|
else: |
|
|
|
data = { |
|
|
|
data = { |
|
|
|
'request_url': request.get('request_url'), |
|
|
|
'request_url': req.url, |
|
|
|
'response_data': response.json() |
|
|
|
'response_data': response.json() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
print(data) |
|
|
|
redis_conn.set(redis_key_api, json.dumps(data), 10 * 60 * 60) |
|
|
|
redis_conn.set(redis_key_api, json.dumps(data), 10 * 60 * 60) |
|
|
|
redis_conn.delete(redis_key_user_data) |
|
|
|
redis_conn.delete(redis_key_user_data) |
|
|
|
# 插入数据 |
|
|
|
# 插入数据 |
|
|
|
db_data = { |
|
|
|
db_data = { |
|
|
|
'ip': user_ip, |
|
|
|
'ip': user_ip, |
|
|
|
'env': request.get('env'), |
|
|
|
'env': web_req.get('env'), |
|
|
|
'request': request.get('request_url'), |
|
|
|
'request': req.url, |
|
|
|
'response': response.json() |
|
|
|
'response': response.json(), |
|
|
|
|
|
|
|
'request_datetime': timezone.now(), |
|
|
|
} |
|
|
|
} |
|
|
|
EcRequestLog.objects.create(db_data) |
|
|
|
print(db_data) |
|
|
|
return response.json() |
|
|
|
EcRequestLog.objects.create(**db_data) |
|
|
|
|
|
|
|
return JsonResponse(response.json()) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 对外接口,用于收集用户选择的数据,例如场次,卖品等,以便后续接口直接调用 |
|
|
|
# 对外接口,用于收集用户选择的数据,例如场次,卖品等,以便后续接口直接调用 |
|
|
|