import web
from utils.EndpointDecorators import acceptJSON, returnJSON, withAuth
from api.endpoints.BaseEndpoint import BaseEndpoint
from api.processing.CodeExecutor import execCode, Timeout

class Execute(BaseEndpoint):
    @acceptJSON('data')
    @returnJSON
    @withAuth(checkAuth=False)
    def POST(self, data):
        code = data['code']
        mode = data['mode']
        try:
            return execCode(code, mode)
        except Timeout as te:
            return { 
                'executionResults': {
                    'err': {
                        'type': 'timeout',
                        'content': str(te)
                    }
                }
            }
        except Exception as e:
            # TODO: log the error before re-raising

            # server 500s here, we don't know what happened
            # unexpected error
            raise e

urls = (
    '', 'Execute'
)

subapp = web.application(urls, locals())
