parent
ebe7e0b6e0
commit
25313c6d81
8 changed files with 87 additions and 18 deletions
@ -0,0 +1,25 @@ |
||||
from dspt_api.util.general.handle_redis import get_data_from_redis |
||||
import random |
||||
|
||||
|
||||
class ApiCinemaHallSeats: |
||||
def __init__(self, **kwargs): |
||||
self.member_type = kwargs.get('member_type') |
||||
self.api = kwargs.get('api') |
||||
self.ip = kwargs.get('ip') |
||||
|
||||
def get_suggestion(self): |
||||
# hall_id |
||||
request_api = {'name': '3.1.3 获取影厅列表', 'path': 'cinema/halls'} |
||||
redis_key_api = f'dspt_api_{self.ip}_{self.member_type}_{request_api["path"]}' |
||||
result, redis_data = get_data_from_redis(redis_key_api) |
||||
if result: |
||||
hall_data = random.choice(redis_data) |
||||
hall_id = hall_data['id'] |
||||
return [{'param': 'hall_id', 'value': hall_id, 'is_checked': True}] |
||||
else: |
||||
# 返回推荐参数应该包含参数名,参数值,和是否勾选的状态 |
||||
return [ |
||||
{'param': 'hall_id', 'value': redis_data + request_api["name"], 'is_checked': True}] |
||||
|
||||
|
@ -0,0 +1,31 @@ |
||||
from django_redis import get_redis_connection |
||||
import json |
||||
import xmltodict |
||||
|
||||
|
||||
# 封装方法处理从redis中获取接口数据 |
||||
def get_data_from_redis(redis_key): |
||||
# 初始化redis |
||||
redis_conn = get_redis_connection() |
||||
if redis_conn.exists(redis_key): |
||||
cinema_halls_data = json.loads(redis_conn.get(redis_key)) |
||||
resp_data = [] |
||||
resp_result = 0 |
||||
if cinema_halls_data['format'] == 'json': |
||||
resp = json.loads(cinema_halls_data['response_data']) |
||||
print('json-response', resp) |
||||
resp_result = resp['res']['status'] |
||||
if str(resp_result) == '1': |
||||
resp_data = resp['res']['data'] |
||||
if cinema_halls_data['format'] == 'xml': |
||||
resp = xmltodict.parse(cinema_halls_data['response_data']) |
||||
print('xml-response', resp) |
||||
resp_result = resp['root']['status'] |
||||
if str(resp_result) == '1': |
||||
resp_data = resp['root']['data']['item'] |
||||
if str(resp_result) != '1': |
||||
return False, '请检查接口返回值或手动输入参数:' |
||||
if len(resp_data) == 0: |
||||
return False, '接口返回数据为空,请手动输入参数:' |
||||
return True, resp_data |
||||
return False, '请手动输入参数,或先请求接口:' |
@ -0,0 +1,12 @@ |
||||
from dspt_api.util.api.cinema_hall_seats import ApiCinemaHallSeats |
||||
|
||||
|
||||
# 通过api来匹配不同的接口文件获取推荐 |
||||
# 返回推荐参数应该包含参数名,参数值,和是否勾选的状态 |
||||
def suggest_params(member_type, api, ip): |
||||
data = {'member_type': member_type, 'api': api, 'ip': ip} |
||||
params = [] |
||||
if api == 'cinema/hall-seats' and member_type == 'nonmember': |
||||
print('cinema/hall-seats') |
||||
params = ApiCinemaHallSeats(**data).get_suggestion() |
||||
return params |
Loading…
Reference in new issue