Exemplo n.º 1
0
  def dispatch(self, request, method='', json_encoder=None):
    from django.http import HttpResponse
    json_encoder = json_encoder or self.json_encoder

    try:
      # in case we do something json doesn't like, we always get back valid json-rpc response
      response = self.empty_response()
      if request.method.lower() == 'get':
        valid, D = self.validate_get(request, method)
        if not valid:
          raise InvalidRequestError('The method you are trying to access is '
                                    'not availble by GET requests')
      elif not request.method.lower() == 'post':
        raise RequestPostError
      else:
        try:
          D = loads(request.raw_post_data)
        except:
          raise InvalidRequestError
      
      if type(D) is list:
        response = [self.response_dict(request, d, is_batch=True, json_encoder=json_encoder)[0] for d in D]
        status = 200
      else:
        response, status = self.response_dict(request, D, json_encoder=json_encoder)
        if response is None and (not u'id' in D or D[u'id'] is None): # a notification
          return HttpResponse('', status=status)
      
      json_rpc = dumps(response, cls=json_encoder)
    except Error, e:
      signals.got_request_exception.send(sender=self.__class__, request=request)
      response['error'] = e.json_rpc_format
      status = e.status
      json_rpc = dumps(response, cls=json_encoder)
Exemplo n.º 2
0
 def send_payload(self, params):
     """Performs the actual sending action and returns the result"""
     return urllib.urlopen(self.service_url,
                   dumps({
                     "jsonrpc": self.version,
                     "method": self.service_name,
                     'params': params,
                     'id': str(uuid.uuid1())})).read()
Exemplo n.º 3
0
 def test_20_notify(self):
   req = {
     u'jsonrpc': u'2.0', 
     u'method': u'jsonrpc.notify', 
     u'params': [u'this is a string'], 
     u'id': None
   }
   resp = urllib.urlopen(self.host, dumps(req)).read()
   self.assertEquals(resp, '')
Exemplo n.º 4
0
 def send_payload(self, params):
     dump = dumps({"jsonrpc" : self.version,
                    "method" : self.service_name,
                    "params" : params,
                    "id" : str(uuid.uuid1())
                    })
     dump_payload = FakePayload(dump)
     response = self.client.post(self.service_url,
                       **{"wsgi.input" : dump_payload,
                       'CONTENT_LENGTH' : len(dump)})
     return response.content
Exemplo n.º 5
0
 def test_20_batch(self):
   req = [{
     u'jsonrpc': u'2.0',
     u'method': u'jsonrpc.test',
     u'params': [u'this is a string'],
     u'id': u'id-'+unicode(i)
   } for i in range(5)]
   resp = loads(urllib.urlopen(self.host, dumps(req)).read())
   self.assertEquals(len(resp), len(req))
   for i, D in enumerate(resp):
     self.assertEquals(D[u'result'], req[i][u'params'][0])
     self.assertEquals(D[u'id'], req[i][u'id'])
Exemplo n.º 6
0
 def test_20_batch_with_errors(self):
   req = [{
     u'jsonrpc': u'2.0',
     u'method': u'jsonrpc.test' if not i % 2 else u'jsonrpc.fails',
     u'params': [u'this is a string'],
     u'id': u'id-'+unicode(i)
   } for i in range(10)]
   resp = loads(urllib.urlopen(self.host, dumps(req)).read())
   self.assertEquals(len(resp), len(req))
   for i, D in enumerate(resp):
     if not i % 2:
       self.assertEquals(D[u'result'], req[i][u'params'][0])
       self.assertEquals(D[u'id'], req[i][u'id'])
       self.assert_(u'error' not in D)
     else:
       self.assert_(u'result' not in D)
       self.assert_(u'error' in D)
       self.assertEquals(D[u'error'][u'code'], 500)
Exemplo n.º 7
0
     
     json_rpc = dumps(response, cls=json_encoder)
   except Error, e:
     signals.got_request_exception.send(sender=self.__class__, request=request)
     response['error'] = e.json_rpc_format
     status = e.status
     json_rpc = dumps(response, cls=json_encoder)
   except Exception, e:
     # exception missed by others
     signals.got_request_exception.send(sender=self.__class__, request=request)
     other_error = OtherError(e)
     response['result'] = None
     response['error'] = other_error.json_rpc_format
     status = other_error.status    
     
     json_rpc = dumps(response,cls=json_encoder)
   
   return HttpResponse(json_rpc, status=status, content_type='application/json-rpc')
 
 def procedure_desc(self, key):
   M = self.urls[key]
   return {
     'name': M.json_method,
     'summary': M.__doc__,
     'idempotent': M.json_safe,
     'params': [{'type': str(Any.kind(t)), 'name': k} 
       for k, t in M.json_arg_types.iteritems()],
     'return': {'type': M.json_return_type}}
 
 def service_desc(self):
   return {
Exemplo n.º 8
0
def _call(host, req):
  return loads(urllib.urlopen(host, dumps(req)).read())