# Path: achilles/common.py
# class AchillesTransport(object):
#     """
#     Transport object, holds information context for some user requests
#     and response
#     """
#     def __init__(self, request):
#         """
#         Achilles Transport init for Django requests, it gets a request object
#         and fills its fields from it
#         """
#         # plugin's data
#         self._data = {}
# 
#         # common properties
#         self.encoding = request.encoding
# 
#         # backend specific properties
#         self.request = request
# 
#     def data(self, key=None, default=None):
#         """
#         Return achilles response dict. Achilles stores here data related
#         to the request, in order to prepare a reply.
# 
#         If key value is given, this function will return that key (if exists)
#         from achilles response dict.
# 
#         If default is given it will be asigned and returned if it doesn't exist
#         """
#         if key not in self._data and default is not None:
#             self._data[key] = default
# 
#         return self._data[key]
# 
# def achilles_renders():
#     """
#     Return a dict of enabled plugin' render functions
#     """
#     return {k: import_by_path('%s.render' % p)
#             for k, p in achilles_plugins().items()}
# 
# Path: achilles/actions.py
# def run_actions(transport, actions):
#     """
#     Run the given list of actions sent by the client
#     """
#     pre_actions_call.send(transport, transport=transport)
# 
#     data = transport.data('actions', {})
#     for a in actions:
#         name = a['name']
#         action = get(name)
# 
#         # run and save return value
#         try:
#             result = action(transport,
#                             *a.get('args', []),
#                             **a.get('kwargs', {}))
# 
#             data[a['id']] = {
#                 'value': result
#             }
#         except Exception as e:
#             # Mark as error
#             data[a['id']] = {
#                 'error': e.__class__.__name__,
#                 'message': str(e),
#             }
#             if settings.DEBUG:
#                 data[a['id']]['trace'] = traceback.format_exc()
# 
#             logger.exception("Error on %s action" % name)
# 
#     post_actions_call.send(transport, transport=transport)


from django.http import HttpResponseBadRequest, HttpResponse
from django.conf import settings
from django.test.client import CONTENT_TYPE_RE
from achilles.common import AchillesTransport, achilles_renders
from achilles.actions import run_actions
import json




def endpoint(request):
    if request.method != 'POST':
        return HttpResponseBadRequest()

    match = CONTENT_TYPE_RE.match(request.META['CONTENT_TYPE'])
    if match:
        charset = match.group(1)
    else:
        charset = settings.DEFAULT_CHARSET

    transport = AchillesTransport(request)
    data = json.loads(request.body.decode(charset))

# Path: achilles/common.py
# class AchillesTransport(object):
#     """
#     Transport object, holds information context for some user requests
#     and response
#     """
#     def __init__(self, request):
#         """
#         Achilles Transport init for Django requests, it gets a request object
#         and fills its fields from it
#         """
#         # plugin's data
#         self._data = {}
# 
#         # common properties
#         self.encoding = request.encoding
# 
#         # backend specific properties
#         self.request = request
# 
#     def data(self, key=None, default=None):
#         """
#         Return achilles response dict. Achilles stores here data related
#         to the request, in order to prepare a reply.
# 
#         If key value is given, this function will return that key (if exists)
#         from achilles response dict.
# 
#         If default is given it will be asigned and returned if it doesn't exist
#         """
#         if key not in self._data and default is not None:
#             self._data[key] = default
# 
#         return self._data[key]
# 
# def achilles_renders():
#     """
#     Return a dict of enabled plugin' render functions
#     """
#     return {k: import_by_path('%s.render' % p)
#             for k, p in achilles_plugins().items()}
# 
# Path: achilles/actions.py
# def run_actions(transport, actions):
#     """
#     Run the given list of actions sent by the client
#     """
#     pre_actions_call.send(transport, transport=transport)
# 
#     data = transport.data('actions', {})
#     for a in actions:
#         name = a['name']
#         action = get(name)
# 
#         # run and save return value
#         try:
#             result = action(transport,
#                             *a.get('args', []),
#                             **a.get('kwargs', {}))
# 
#             data[a['id']] = {
#                 'value': result
#             }
#         except Exception as e:
#             # Mark as error
#             data[a['id']] = {
#                 'error': e.__class__.__name__,
#                 'message': str(e),
#             }
#             if settings.DEBUG:
#                 data[a['id']]['trace'] = traceback.format_exc()
# 
#             logger.exception("Error on %s action" % name)
# 
#     post_actions_call.send(transport, transport=transport)


# Path: achilles/views.py
from django.http import HttpResponseBadRequest, HttpResponse
from django.conf import settings
from django.test.client import CONTENT_TYPE_RE
from achilles.common import AchillesTransport, achilles_renders
from achilles.actions import run_actions
import json





def endpoint(request):
    if request.method != 'POST':
        return HttpResponseBadRequest()

    match = CONTENT_TYPE_RE.match(request.META['CONTENT_TYPE'])
    if match:
        charset = match.group(1)
    else:
        charset = settings.DEFAULT_CHARSET

    transport = AchillesTransport(request)
    data = json.loads(request.body.decode(charset))

    run_actions(transport, data)