1. 支持xml的美化输出

2. xml数据格式化为json格式的逻辑支持
main
RogerWork 3 months ago
parent 0e14a86aed
commit 522c904fdb
  1. 2
      dspt_api/urls.py
  2. 8
      dspt_api/util/general/format_xml.py
  3. 4
      dspt_api/util/general/handle_redis.py
  4. 1002
      dspt_api/util/general/handle_xml_resp.py
  5. 69
      dspt_api/views.py

@ -13,4 +13,6 @@ urlpatterns = [
path('get_url', views.general_api_url), path('get_url', views.general_api_url),
path('send_request', views.send_request), path('send_request', views.send_request),
path('get_suggest_params', views.get_suggest_params_by_api), path('get_suggest_params', views.get_suggest_params_by_api),
path('set_user_data', views.set_user_select_data),
path('clear_user_data', views.clear_user_select_data),
] ]

@ -0,0 +1,8 @@
from xml.dom import minidom
import xml.etree.ElementTree as ET
def format_xml(_xml):
root = ET.fromstring(_xml)
dom = minidom.parseString(ET.tostring(root))
pretty_xml = dom.toprettyxml(indent="\t", newl="")
return pretty_xml

@ -13,13 +13,13 @@ def get_data_from_redis(redis_key):
resp_result = 0 resp_result = 0
if redis_data['format'] == 'json': if redis_data['format'] == 'json':
resp = json.loads(redis_data['response_data']) resp = json.loads(redis_data['response_data'])
print('json-response', resp) print('json-response', json.dumps(resp))
resp_result = resp['res']['status'] resp_result = resp['res']['status']
if str(resp_result) == '1': if str(resp_result) == '1':
resp_data = resp['res']['data'] resp_data = resp['res']['data']
if redis_data['format'] == 'xml': if redis_data['format'] == 'xml':
resp = xmltodict.parse(redis_data['response_data']) resp = xmltodict.parse(redis_data['response_data'])
print('xml-response', resp) print('xml-response', json.dumps(resp))
resp_result = resp['root']['status'] resp_result = resp['root']['status']
if str(resp_result) == '1': if str(resp_result) == '1':
resp_data = resp['root']['data'].get('item', resp['root']['data']) resp_data = resp['root']['data'].get('item', resp['root']['data'])

File diff suppressed because it is too large Load Diff

@ -1,5 +1,7 @@
import datetime import datetime
import json import json
import re
import requests import requests
import xml.etree.ElementTree import xml.etree.ElementTree
from urllib.parse import urljoin from urllib.parse import urljoin
@ -17,7 +19,9 @@ 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 dspt_api.util.suggest_params import suggest_params from dspt_api.util.suggest_params import suggest_params
from dspt_api.util.general.handle_xml_resp import HandleXmlResp
from dspt_api.util.general.format_xml import format_xml
import chardet
# Create your views here. # Create your views here.
class EcChannelViewSet(viewsets.ModelViewSet): class EcChannelViewSet(viewsets.ModelViewSet):
@ -214,25 +218,72 @@ def send_request(request):
} }
print(db_data) print(db_data)
EcRequestLog.objects.create(**db_data) EcRequestLog.objects.create(**db_data)
return JsonResponse({'format': resp_format, 'data': response.text}) # 处理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)})
# 对外接口,用于收集用户选择的数据,例如场次,卖品等,以便后续接口直接调用 # 对外接口,用于收集用户选择的数据,例如场次,卖品等,以便后续接口直接调用
@csrf_exempt @csrf_exempt
def user_select_data(request): def set_user_select_data(request):
"""
参数 包含
1. api地址
2. 接口类型 会员 非会员
3. 接口数据类型 json xml
4. 用户选择数据
"""
# 获取返回类型结果
req = json.loads(request.body)
api = req.get('api')
member_type = req.get('member_type')
resp_format = req.get('format')
user_data = req.get('user_data')
user_ip = request.META.get('REMOTE_ADDR')
# 初始化redis # 初始化redis
redis_conn = get_redis_connection() redis_conn = get_redis_connection()
user_ip = request.META.get('REMOTE_ADDR') redis_key_user_data = f'dspt_api_{user_ip}_{member_type}_{api}_user_data'
api = request.get('api')
redis_key_user_data = f'dspt_api_{user_ip}_{api}_data'
# 记录用户选择 # 记录用户选择
data = { data = {
'user_data': request.get('user_data') 'api': api,
'member_type': member_type,
'format': resp_format,
'user_data': user_data,
} }
if redis_conn.exists(redis_key_user_data): if redis_conn.exists(redis_key_user_data):
redis_conn.delete(redis_key_user_data) redis_conn.delete(redis_key_user_data)
result = redis_conn.set(redis_key_user_data, json.dumps(data), 10 * 60 * 60) if redis_conn.set(redis_key_user_data, json.dumps(data), 10 * 60 * 60):
return result return JsonResponse({'result': 'success'})
return JsonResponse({'result': 'fail'})
@csrf_exempt
def clear_user_select_data(request):
"""
参数 包含
1. api地址
2. 接口类型 会员 非会员
"""
# 获取返回类型结果
req = json.loads(request.body)
api = req.get('api')
member_type = req.get('member_type')
user_ip = request.META.get('REMOTE_ADDR')
# 初始化redis
redis_conn = get_redis_connection()
redis_key_user_data = f'dspt_api_{user_ip}_{member_type}_{api}_user_data'
if redis_conn.exists(redis_key_user_data):
if redis_conn.delete(redis_key_user_data):
return JsonResponse({'result': 'success'})
return JsonResponse({'result': 'fail'})
# 对外接口,用于给前端提供上次请求的数据,如果没有则返回数据库中的默认值 # 对外接口,用于给前端提供上次请求的数据,如果没有则返回数据库中的默认值

Loading…
Cancel
Save