修复请求接口的方法

main
RogerWork 4 months ago
parent 456a4af8d5
commit 2df3558c16
  1. 23
      dspt_api/migrations/0011_alter_ecrequestlog_request_and_more.py
  2. 4
      dspt_api/models.py
  3. 29
      dspt_api/views.py

@ -0,0 +1,23 @@
# Generated by Django 4.2.7 on 2024-07-29 05:56
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('dspt_api', '0010_rename_desc_ecapiparams_description'),
]
operations = [
migrations.AlterField(
model_name='ecrequestlog',
name='request',
field=models.CharField(max_length=5000, verbose_name='请求链接'),
),
migrations.AlterField(
model_name='ecrequestlog',
name='response',
field=models.TextField(verbose_name='返回值'),
),
]

@ -87,8 +87,8 @@ class EcRequestLog(models.Model):
id = models.IntegerField(primary_key=True) id = models.IntegerField(primary_key=True)
ip = models.CharField(verbose_name='请求ip', max_length=20, null=False) # 接口测试人的ip ip = models.CharField(verbose_name='请求ip', max_length=20, null=False) # 接口测试人的ip
env = models.CharField(verbose_name='环境', max_length=10, null=False) # 电商平台 dspt 自营电商 zyds env = models.CharField(verbose_name='环境', max_length=10, null=False) # 电商平台 dspt 自营电商 zyds
request = models.CharField(verbose_name='请求链接', max_length=500, null=False) request = models.CharField(verbose_name='请求链接', max_length=5000, null=False)
response = models.CharField(verbose_name='返回值', max_length=10000, null=False) response = models.TextField(verbose_name='返回值', null=False)
request_datetime = models.DateTimeField(verbose_name='请求发送时间', null=False) request_datetime = models.DateTimeField(verbose_name='请求发送时间', null=False)
def __str__(self): def __str__(self):

@ -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())
# 对外接口,用于收集用户选择的数据,例如场次,卖品等,以便后续接口直接调用 # 对外接口,用于收集用户选择的数据,例如场次,卖品等,以便后续接口直接调用

Loading…
Cancel
Save