I have started with pyjamas that use JSON RPC and would like to use
"JSON RPC views" but found only external libraries solution. I have
written following snippets and wonder if they could be added to
django.utils:

class JsonResponse(HttpResponse):
  """
  Http response which has JSON content.
  """
  mimetype = 'application/json; charset=utf8'

  def __init__(self, data):
    content = simplejson.dumps(data, indent=2, ensure_ascii=False)
    super(JsonResponse, self).__init__(content=content,
                                       mimetype=self.mimetype)

class JsonRPCService(object):
  """
  Class that implement a JSON RPC service. The RPC methods are
  members methods of a successor which must implement them.
  Usage:
  - create instance of the successor implementing desired methods
  - add its dispatch method among your urls
  """

  version = '2.0'
  @csrf_exempt
  def dispatch(self, request):
    try:
      data = simplejson.loads(request.raw_post_data)
      ver = data['jsonrpc']
      if ver != self.version:
        raise Exception('incorrect JSON RPC version')
      methodName = data['method']
      messId = data['id']
      params = data['params']
      method = getattr(self, methodName)
      response = method(*params)
      response = {"result": response, "error": None, "id": messId}
    except Exception, e:
      response = {"result": None, "error": e.message, "id": messId}
    return JsonResponse(response)

class SampleJSONService(JsonRPCService):
  def echo(self, data):
    return data

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-developers@googlegroups.com.
To unsubscribe from this group, send email to 
django-developers+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en.

Reply via email to