def star_api(user_ID, api_id): """ :param user_ID: :type user_ID: :param api_id: :type api_id: :return: :rtype: """ user = user_business.get_by_user_ID(user_ID=user_ID) api = api_business.get_by_api_id(api_id=api_id) # user_result, api_result = None, None # 1. 在user下存star_apis if api not in user.star_apis: user.star_apis.append(api) user_result = user.save() else: user.star_apis.remove(api) user_result = user.save() # 2. 在api下存star_users if user not in api.star_users: api.star_users.append(user) api_result = api.save() else: api.star_users.remove(user) api_result = api.save() if user_result and api_result: return {"user": user_result.to_mongo(), "api": api_result.to_mongo()}
def add_used_api(user_ID, api_id): """ 为用户增加 使用过的api :param user_ID: :type user_ID: :param api_id: :type api_id: :return: :rtype: """ user = user_business.get_by_user_ID(user_ID=user_ID) api = api_business.get_by_api_id(api_id=api_id) user_result = None if api not in user.used_apis: user.used_apis.append(api) user_result = user.save() if user_result: return { "user": user_result.to_mongo(), }
def run_api(): data = request.get_json() user_ID = data.pop("user_ID") api_id = data["api"]["api_id"] input = data["api"]["input"] api = api_business.get_by_api_id(api_id=api_id) # filled_api_detail = data["filled_api_detail"] run_result = True # 成功调用后后 # 增加api调用次数 # 在用户下增加 used_api if run_result: user = user_service.add_used_api(user_ID=user_ID, api_id=api_id) api_result = api_business.increment_usage_count(api_id) if api.status == 0: return jsonify({'response': api.fake_response}), 200 elif api.status == 1: # result = return jsonify({'response': "真实api结果"}), 200
def get_api(api_id): api = api_business.get_by_api_id(api_id) api = json_utility.convert_to_json(api.to_mongo()) return jsonify({"response": api}), 200