# standard imports import json import logging import os import sys import uuid from os import path from urllib.parse import parse_qsl, urlparse import redis from cic_eth.api.api_task import Api from cic_eth.server.UWSGIOpenAPIRequest import UWSGIOpenAPIRequest from openapi_core import create_spec 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) logging.basicConfig(level=logging.WARNING) logg = logging.getLogger() # uwsgi application def application(env, start_response): print(spec) request = Request(env) oAPIRequest = UWSGIOpenAPIRequest(request) validator = RequestValidator(spec) result = validator.validate(oAPIRequest) # raise errors if request invalid # result.raise_for_errors() if result.errors: # get list of errors json_data = json.dumps(list(map(lambda e: str(e), result.errors))) content = json_data.encode('utf-8') headers = [] headers.append(('Content-Length', str(len(content))),) headers.append(('Access-Control-Allow-Origin', '*',)) headers.append(('Content-Type', 'application/json',)) start_response('400 Invalid Request', headers) return [content] parsed_url = urlparse(env.get('REQUEST_URI')) # /api path = parsed_url.path parsed_url.params params = dict(parse_qsl(parsed_url.query)) if path == '/list': address = params.get('address') print('address', address) # address, limit=10, external_task=None, external_queue=None data = call('list', address) elif path == '/balance': data = call('balance', **params) elif path == '/create_account': data = call('create_account', **params) elif path == '/ping': data = call('ping', **params) elif path == '/transfer': data = call('transfer', **params) elif path == '/transfer_from': data = call('transfer_from', **params) elif path == '/token': data = call('token', **params) elif path == '/tokens': data = call('tokens', **params) elif path == '/default_token': data = call('default_token', ) json_data = json.dumps(data) content = json_data.encode('utf-8') headers = [] headers.append(('Content-Length', str(len(content))),) headers.append(('Access-Control-Allow-Origin', '*',)) headers.append(('Content-Type', 'application/json',)) start_response('200 OK', headers) return [content]