From ee839e8f619d919815e7df22c1df12a2d493baeb Mon Sep 17 00:00:00 2001 From: RogerWork Date: Mon, 19 Aug 2024 15:30:52 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86xml=E5=A4=84=E7=90=86=E5=90=8E?= =?UTF-8?q?=E7=9A=84=E6=95=B0=E6=8D=AE=E4=B9=9F=E5=86=99=E5=85=A5redis?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dspt_api/views.py | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/dspt_api/views.py b/dspt_api/views.py index 320cbf1..261a754 100644 --- a/dspt_api/views.py +++ b/dspt_api/views.py @@ -195,6 +195,16 @@ def send_request(request): response = requests.Session().send(req) print('response', response.content) + # 处理xml数据 + handled_data = '' + response_data = '' + if resp_format == 'json': + response_data = response.text + handled_data = json.loads(response.text) + if resp_format == 'xml': + response_data = format_xml(re.sub(r'>\n?\s+?<', r'>\n<', response.text)) + handled_data = HandleXmlResp(response.text).format_xml() + # 记录Redis if redis_conn.exists(redis_key_api): redis_conn.delete(redis_key_api) @@ -203,7 +213,8 @@ def send_request(request): 'member_type': member_type, 'format': resp_format, 'params': params, - 'response_data': response.text + 'response_data': response.text, + 'handled_data': handled_data, } print(data) redis_conn.set(redis_key_api, json.dumps(data), 10 * 60 * 60) @@ -218,17 +229,8 @@ def send_request(request): } print(db_data) EcRequestLog.objects.create(**db_data) - # 处理xml数据 - handled_data = '' - _data = '' - if resp_format == 'json': - _data = response.text - handled_data = json.loads(response.text) - if resp_format == 'xml': - _data = format_xml(re.sub(r'>\n?\s+?<', r'>\n<', response.text)) - print('---format_xml-----', _data) - handled_data = HandleXmlResp(response.text).format_xml() - return JsonResponse({'format': resp_format, 'data': _data, 'handled': json.dumps(handled_data)}) + + return JsonResponse({'format': resp_format, 'data': response_data, 'handled': json.dumps(handled_data)}) # 对外接口,用于收集用户选择的数据,例如场次,卖品等,以便后续接口直接调用