Change the implement of experimental agreegate API

This commit is contained in:
Young-Lord
2022-01-22 10:40:06 +08:00
parent e1fc1bbcd1
commit 23bf3c020f

View File

@@ -22,7 +22,8 @@ import os
import sys import sys
from multiprocessing import Process, Pipe from multiprocessing import Process, Pipe
if Config.USE_EXPERIMENTAL_AGGREGATE: if Config.USE_EXPERIMENTAL_AGGREGATE:
import requests from urllib.parse import parse_qs, urlparse
from werkzeug.datastructures import ImmutableMultiDict
app = Flask(__name__) app = Flask(__name__)
@@ -213,28 +214,29 @@ def present_info(user_id):
if Config.USE_EXPERIMENTAL_AGGREGATE: if Config.USE_EXPERIMENTAL_AGGREGATE:
@app.route(add_url_prefix('/compose/aggregate'), methods=['GET']) @app.route(add_url_prefix('/compose/aggregate'), methods=['GET'])
@server.auth.auth_required(request) def aggregate_experimental():
def aggregate_experimental(user_id): global request
# 只有()才会requests.request('http://localhost') # ()都不requests.request('http://localhost')
url_base:str='http'
if Config.SSL_CERT and Config.SSL_KEY:
url_base+='s'
url_base+='://'
url_base+='localhost:'+Config.PORT
finally_response={'success':True,'value':[]} finally_response={'success':True,'value':[]}
for i in json.loads(request.args.get('calls')): request_=request
resp_t=requests.request(request.method,\ for i in json.loads(request_.args.get('calls')):
url_base+add_url_prefix(i.get('endpoint')),\ url=add_url_prefix(i.get('endpoint'))
headers=request.headers request.args=ImmutableMultiDict({key:value[0] for key,value in parse_qs(urlparse(url).query).items()})
).content.decode() url=urlparse(url).path
urls = app.url_map.bind("example.com", "/")
resp_t=app.view_functions[urls.match(url,"GET")[0]]()
if hasattr(resp_t,"response"):
resp_t=resp_t.response[0].decode().rstrip('\n')
resp=json.loads(resp_t) resp=json.loads(resp_t)
if hasattr(resp,'get') and resp.get('success') is False: if hasattr(resp,'get') and resp.get('success') is False:
finally_response={'success':False,'error_code':7,'extra':{"id":i.get('id'),'error_code':resp.get('error_code')}} finally_response={'success':False,'error_code':7,'extra':{"id":i.get('id'),'error_code':resp.get('error_code')}}
if "extra" in resp: if "extra" in resp:
finally_response['extra']['extra']=resp['extra'] finally_response['extra']['extra']=resp['extra']
request=request_# 无实际影响
return jsonify(finally_response) return jsonify(finally_response)
# I don't sure if the behavior is correct # I don't sure if the behavior is correct
finally_response['value'].append({'id':i.get('id'),'value':resp.get('value') if hasattr(resp,'get') else resp}) finally_response['value'].append({'id':i.get('id'),'value':resp.get('value') if hasattr(resp,'get') else resp})
request=request_# 无实际影响
return jsonify(finally_response) return jsonify(finally_response)
else: else:
# 集成式请求,没想到什么好办法处理,就先这样写着 # 集成式请求,没想到什么好办法处理,就先这样写着