This commit is contained in:
William Luke 2021-11-18 18:03:01 +03:00
parent a379065891
commit b42f076d85
2 changed files with 11 additions and 57 deletions

View File

@ -7,7 +7,6 @@ import uuid
from os import path
from urllib.parse import parse_qsl, urlparse
import cic_eth.cli
import redis
from cic_eth.api.api_task import Api
from cic_eth.server.UWSGIOpenAPIRequest import UWSGIOpenAPIRequest
@ -16,7 +15,7 @@ from openapi_core.validation.request.datatypes import OpenAPIRequest
from openapi_core.validation.request.validators import RequestValidator
from openapi_spec_validator.schemas import read_yaml_file
from werkzeug.wrappers import Request
from cic_eth.server.celery_helper import call
spec_dict = read_yaml_file(path.join(path.dirname(
__file__), '../../server/openapi/server.yaml'))
spec = create_spec(spec_dict)
@ -25,15 +24,7 @@ spec = create_spec(spec_dict)
logging.basicConfig(level=logging.WARNING)
logg = logging.getLogger()
arg_flags = cic_eth.cli.argflag_std_base
local_arg_flags = cic_eth.cli.argflag_local_taskcallback
argparser = cic_eth.cli.ArgumentParser(arg_flags)
argparser.process_local_flags(local_arg_flags)
args = argparser.parse_args()
config = cic_eth.cli.Config.from_args(args, arg_flags, local_arg_flags)
celery_app = cic_eth.cli.CeleryApp.from_config(config)
# uwsgi application
@ -62,67 +53,29 @@ def application(env, start_response):
path = parsed_url.path
parsed_url.params
params = dict(parse_qsl(parsed_url.query))
request_method = env.get('REQUEST_METHOD')
chain_spec = config.get('CHAIN_SPEC')
redis_host = config.get('REDIS_HOST')
redis_port = config.get('REDIS_PORT')
redis_db = config.get('REDIS_DB')
celery_queue = config.get('CELERY_QUEUE')
redis_channel = str(uuid.uuid4())
r = redis.Redis(redis_host, redis_port, redis_db)
ps = r.pubsub()
ps.subscribe(redis_channel)
ps.get_message() # Subscription Object
api = Api(
chain_spec,
queue=celery_queue,
callback_param='{}:{}:{}:{}'.format(
redis_host, redis_port, redis_db, redis_channel),
callback_task='cic_eth.callbacks.redis.redis',
callback_queue=celery_queue,
)
if path == '/list':
address = params.get('address')
print('address', address)
# address, limit=10, external_task=None, external_queue=None
api.list(address)
data = call('list', address)
elif path == '/balance':
api.balance(**params)
data = call('balance', **params)
elif path == '/create_account':
api.create_account(**params)
data = call('create_account', **params)
elif path == '/ping':
api.ping(**params)
data = call('ping', **params)
elif path == '/transfer':
api.transfer(**params)
data = call('transfer', **params)
elif path == '/transfer_from':
api.transfer_from(**params)
data = call('transfer_from', **params)
elif path == '/token':
api.token(**params)
data = call('token', **params)
elif path == '/tokens':
api.tokens(**params)
data = call('tokens', **params)
elif path == '/default_token':
api.default_token()
data = call('default_token', )
ps.get_message() # returns None !?
try:
o = ps.get_message(timeout=config.get('REDIS_TIMEOUT'))
except TimeoutError as e:
sys.stderr.write(
'got no new address from cic-eth before timeout: {}\n'.format(e))
sys.exit(1)
ps.unsubscribe()
m = json.loads(o['data'])
print(m['result'])
data = {
"path": path,
"request_method": request_method,
"result": m
}
json_data = json.dumps(data)
content = json_data.encode('utf-8')
headers = []

View File

@ -59,3 +59,4 @@ def call(method, *args):
sys.stderr.write(
f'Unable to parse Data:\n{o}\n Error:\n{e}')
sys.exit(1)