# Path: code/api/enums.py
# JSON_TYPES = {
#     INIT: "init",
#     PROPOSE: "propose",
#     PREVOTE: "prevote",
#     VOTE: "vote",
# }
# 
# Path: code/api/utils.py
# def jsonify(data, allow_all_origin=False):
#     from flask import Response, jsonify as json_ify
#     res = json_ify(data)
#     assert isinstance(res, Response)
#     origin = request.environ.get('HTTP_ORIGIN')
#     if not origin:
#         origin = request.environ.get('ORIGIN')
#     # end if
#     if allow_all_origin:
#         res.headers["Access-Control-Allow-Origin"] = '*'
#     elif origin and origin in ORIGIN_LIST:
#         res.headers["Access-Control-Allow-Origin"] = origin
#     # end if
#     return res
# 
# Path: code/api/database.py
# VALUE_TYPE = float
# MSG_TYPE_TYPE = int
# NODE_TYPE = int
# SEQUENCE_TYPE = int
# MSG_TYPE_CLASS_MAP = {
#     INIT: DBInitMessage,
#     PROPOSE: DBProposeMessage,
#     PREVOTE: DBPrevoteMessage,
#     VOTE: DBVoteMessage,
#     # ...
#     ACKNOWLEDGE: DBAcknowledge,
# }
# class DBMessage(db.Entity):
# class DBInitMessage(DBMessage):
# class DBProposeMessage(DBMessage):
# class DBPrevoteMessage(DBMessage):
# class DBVoteMessage(DBMessage):
# class DBAcknowledge(DBMessage):
#     def from_db(self):
#     def to_db(cls, msg):
#     def from_db(self):
#     def to_db(cls, msg):
#     def from_db(self):
#     def to_db(cls, msg):
#     def to_db(cls, msg):
#     def from_db(self):
#     def to_db(cls, msg):
#     def from_db(self):
#     def to_db(cls, msg):
#     def from_db(self):
# def to_db(msg):


from datetime import datetime
from DictObject import DictObject
from flask import Flask, request
from luckydonaldUtils.logger import logging
from pony import orm
from .enums import JSON_TYPES
from .utils import jsonify
from .database import to_db, db, DBVoteMessage, DBMessage, DBInitMessage, DBPrevoteMessage, DBProposeMessage, DBAcknowledge, \
    MSG_TYPE_CLASS_MAP
from node.enums import INIT  # noqa # pylint: disable=unused-import
from node.messages import Message  # noqa # pylint: disable=unused-import
from werkzeug.debug import DebuggedApplication
# -*- coding: utf-8 -*-



__author__ = 'luckydonald'
logger = logging.getLogger(__name__)

VERSION = "0.0.1"
__version__ = VERSION
assert INIT == INIT  # to prevent the unused import warning. Is used in SQL statement.

app = Flask(__name__)
debug = DebuggedApplication(app, console_path="/console/")

API_V1 = ""
API_V2 = "/api/v2"


@app.route(API_V1+"/dump", methods=['POST', 'GET', 'PUT'])
@app.route(API_V1+"/dump/", methods=['POST', 'GET', 'PUT'])
@orm.db_session
def dump_to_db():
    try:
        logger.info("Incoming: {}".format(request.get_json(force=True)))

# Path: code/api/enums.py
# JSON_TYPES = {
#     INIT: "init",
#     PROPOSE: "propose",
#     PREVOTE: "prevote",
#     VOTE: "vote",
# }
# 
# Path: code/api/utils.py
# def jsonify(data, allow_all_origin=False):
#     from flask import Response, jsonify as json_ify
#     res = json_ify(data)
#     assert isinstance(res, Response)
#     origin = request.environ.get('HTTP_ORIGIN')
#     if not origin:
#         origin = request.environ.get('ORIGIN')
#     # end if
#     if allow_all_origin:
#         res.headers["Access-Control-Allow-Origin"] = '*'
#     elif origin and origin in ORIGIN_LIST:
#         res.headers["Access-Control-Allow-Origin"] = origin
#     # end if
#     return res
# 
# Path: code/api/database.py
# VALUE_TYPE = float
# MSG_TYPE_TYPE = int
# NODE_TYPE = int
# SEQUENCE_TYPE = int
# MSG_TYPE_CLASS_MAP = {
#     INIT: DBInitMessage,
#     PROPOSE: DBProposeMessage,
#     PREVOTE: DBPrevoteMessage,
#     VOTE: DBVoteMessage,
#     # ...
#     ACKNOWLEDGE: DBAcknowledge,
# }
# class DBMessage(db.Entity):
# class DBInitMessage(DBMessage):
# class DBProposeMessage(DBMessage):
# class DBPrevoteMessage(DBMessage):
# class DBVoteMessage(DBMessage):
# class DBAcknowledge(DBMessage):
#     def from_db(self):
#     def to_db(cls, msg):
#     def from_db(self):
#     def to_db(cls, msg):
#     def from_db(self):
#     def to_db(cls, msg):
#     def to_db(cls, msg):
#     def from_db(self):
#     def to_db(cls, msg):
#     def from_db(self):
#     def to_db(cls, msg):
#     def from_db(self):
# def to_db(msg):


# Path: code/api/main.py
from datetime import datetime
from DictObject import DictObject
from flask import Flask, request
from luckydonaldUtils.logger import logging
from pony import orm
from .enums import JSON_TYPES
from .utils import jsonify
from .database import to_db, db, DBVoteMessage, DBMessage, DBInitMessage, DBPrevoteMessage, DBProposeMessage, DBAcknowledge, \
    MSG_TYPE_CLASS_MAP
from node.enums import INIT  # noqa # pylint: disable=unused-import
from node.messages import Message  # noqa # pylint: disable=unused-import
from werkzeug.debug import DebuggedApplication

# -*- coding: utf-8 -*-



__author__ = 'luckydonald'
logger = logging.getLogger(__name__)

VERSION = "0.0.1"
__version__ = VERSION
assert INIT == INIT  # to prevent the unused import warning. Is used in SQL statement.

app = Flask(__name__)
debug = DebuggedApplication(app, console_path="/console/")

API_V1 = ""
API_V2 = "/api/v2"


@app.route(API_V1+"/dump", methods=['POST', 'GET', 'PUT'])
@app.route(API_V1+"/dump/", methods=['POST', 'GET', 'PUT'])
@orm.db_session
def dump_to_db():
    try:
        logger.info("Incoming: {}".format(request.get_json(force=True)))

        msg = to_db(request.get_json(force=True))