Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import json
- import logging
- from django.http import HttpResponse
- logger = logging.getLogger(__name__)
- def _return_response(response_dict, status=200):
- response = json.dumps(response_dict)
- return HttpResponse(response, status=status, content_type="application/javascript")
- def _return_error_response(error):
- response_dict = {'error': error}
- return _return_response(response_dict, status=400)
- def api_request_decorator(request_type):
- assert request_type in ('GET', 'POST'), "Invalid request_type specified"
- def inner_decorator(func):
- def inner(request, *args, **kwargs):
- try:
- if request.method != request_type:
- raise Exception('request type %s required for this method' % request_type)
- return _return_response(func(request, *args, **kwargs))
- except Exception, e:
- logger.error("Exception caught in API request: %s" % e)
- return _return_error_response(e.message)
- return inner
- return inner_decorator
Add Comment
Please, Sign In to add comment