2021-10-25 16:51:45 +02:00
|
|
|
# standard imports
|
|
|
|
import json
|
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import uuid
|
2021-11-18 13:38:20 +01:00
|
|
|
from os import path
|
2021-10-25 16:51:45 +02:00
|
|
|
from urllib.parse import parse_qsl, urlparse
|
|
|
|
|
|
|
|
import redis
|
2021-11-18 13:38:20 +01:00
|
|
|
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
|
2021-11-18 16:03:01 +01:00
|
|
|
from cic_eth.server.celery_helper import call
|
2021-11-18 13:38:20 +01:00
|
|
|
spec_dict = read_yaml_file(path.join(path.dirname(
|
|
|
|
__file__), '../../server/openapi/server.yaml'))
|
|
|
|
spec = create_spec(spec_dict)
|
|
|
|
|
2021-10-25 16:51:45 +02:00
|
|
|
|
|
|
|
logging.basicConfig(level=logging.WARNING)
|
|
|
|
logg = logging.getLogger()
|
|
|
|
|
|
|
|
|
2021-11-18 13:38:20 +01:00
|
|
|
|
2021-10-25 16:51:45 +02:00
|
|
|
|
|
|
|
# uwsgi application
|
|
|
|
|
|
|
|
|
|
|
|
def application(env, start_response):
|
2021-11-18 13:38:20 +01:00
|
|
|
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
|
2021-10-25 16:51:45 +02:00
|
|
|
path = parsed_url.path
|
2021-11-18 13:38:20 +01:00
|
|
|
parsed_url.params
|
2021-10-25 16:51:45 +02:00
|
|
|
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
|
2021-11-18 16:03:01 +01:00
|
|
|
data = call('list', address)
|
2021-10-25 16:51:45 +02:00
|
|
|
elif path == '/balance':
|
2021-11-18 16:03:01 +01:00
|
|
|
data = call('balance', **params)
|
2021-10-25 16:51:45 +02:00
|
|
|
elif path == '/create_account':
|
2021-11-18 16:03:01 +01:00
|
|
|
data = call('create_account', **params)
|
2021-10-25 16:51:45 +02:00
|
|
|
elif path == '/ping':
|
2021-11-18 16:03:01 +01:00
|
|
|
data = call('ping', **params)
|
2021-10-25 16:51:45 +02:00
|
|
|
elif path == '/transfer':
|
2021-11-18 16:03:01 +01:00
|
|
|
data = call('transfer', **params)
|
2021-10-25 16:51:45 +02:00
|
|
|
elif path == '/transfer_from':
|
2021-11-18 16:03:01 +01:00
|
|
|
data = call('transfer_from', **params)
|
2021-10-25 16:51:45 +02:00
|
|
|
elif path == '/token':
|
2021-11-18 16:03:01 +01:00
|
|
|
data = call('token', **params)
|
2021-10-25 16:51:45 +02:00
|
|
|
elif path == '/tokens':
|
2021-11-18 16:03:01 +01:00
|
|
|
data = call('tokens', **params)
|
2021-10-25 16:51:45 +02:00
|
|
|
elif path == '/default_token':
|
2021-11-18 16:03:01 +01:00
|
|
|
data = call('default_token', )
|
2021-10-25 16:51:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
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]
|