def dispatch(self, request, method=''): from django.http import HttpResponse from django.core.serializers.json import DjangoJSONEncoder 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)[0] for d in D] status = 200 else: response, status = self.response_dict(request, D) 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=DjangoJSONEncoder) except Error, e: response['error'] = e.json_rpc_format status = e.status json_rpc = dumps(response, cls=DjangoJSONEncoder)
def browse(request): if (request.GET.get('f', None) == 'mochikit.js'): return HttpResponse(mochikit.mochikit, content_type='application/x-javascript') if (request.GET.get('f', None) == 'interpreter.js'): return HttpResponse(mochikit.interpreter, content_type='application/x-javascript') desc = jsonrpc_site.service_desc() return render_to_response('browse.html', { 'methods': desc['procs'], 'method_names_str': dumps( [m['name'] for m in desc['procs']]) })
def __call__(self, *args, **kwargs): params = kwargs if len(kwargs) else args if Any.kind(params) == Object and self.__version != '2.0': raise Exception('Unsupport arg type for JSON-RPC 1.0 ' '(the default version for this client, ' 'pass version="2.0" to use keyword arguments)') r = urllib.urlopen(self.__service_url, dumps({ "jsonrpc": self.__version, "method": self.__service_name, 'params': params, 'id': str(uuid.uuid1())})).read() return loads(r)
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=DjangoJSONEncoder) except Error, e: response['error'] = e.json_rpc_format status = e.status json_rpc = dumps(response, cls=DjangoJSONEncoder) except Exception, e: # exception missed by others other_error = OtherError(e) response['result'] = None response['error'] = other_error.json_rpc_format status = other_error.status json_rpc = dumps(response,cls=DjangoJSONEncoder) 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': str(M.json_return_type)}} def service_desc(self): return {