Пример #1
0
    def callRemote(self, method, *args, **kwargs):
        """
        Remotely calls the method, with args. Given that we keep reference to
        the call via the Deferred, there's no need for id. It will coin some
        random anyway, just to satisfy the spec.

        According to the spec, we cannot use either args and kwargs at once.
        If there are kwargs, they get used and args are ignored.


        @type method: str
        @param method: Method name

        @type *args: list
        @param *args: List of agruments for the method. It gets ignored if
            kwargs is not empty.

        @type **kwargs: dict
        @param **kwargs: Dict of positional arguments for the method

        @rtype: t.i.d.Deferred
        @return: Deferred, that will fire with whatever the 'method' returned.
        @TODO support batch requests
        """

        if kwargs:
            json_request = jsonrpc.encodeRequest(method,
                                                 kwargs,
                                                 version=self.version)
        else:
            json_request = jsonrpc.encodeRequest(method,
                                                 args,
                                                 version=self.version)

        if self.verbose:
            log.msg('Sending: %s' % json_request)

        response_deferred = ResponseDeferred(verbose=self.verbose)
        factory = CallbackFactory(response_deferred.responseReceived)
        point = TCP4ClientEndpoint(reactor,
                                   self.hostname,
                                   self.port,
                                   timeout=self.timeout)
        d = point.connect(factory)
        d.addCallback(self.connectionMade, json_request)

        # response_deferred will be fired in responseReceived, after
        # we got response from the RPC server
        response_deferred.addCallback(jsonrpc.decodeResponse)
        return response_deferred
Пример #2
0
 def test_methodArgsVersion2(self):
     result = jsonrpc.encodeRequest('method', 'abcdef', version=2)
     self.assert_json_values(result,
                             jsonrpc='2.0',
                             params='abcdef',
                             method='method',
                             id=Regex(r'\d+'))
Пример #3
0
 def test_all(self):
     result = jsonrpc.encodeRequest('method',
                                    'abcdef',
                                    id_=123,
                                    version=2.0)
     expected = '{"jsonrpc": "2.0", "params": "abcdef", "method": '
     expected += '"method", "id": 123}'
     self.assertEquals(result, expected)
Пример #4
0
 def test_methodKwargs(self):
     result = jsonrpc.encodeRequest('method', {'first': 'a', 'second': 'b'})
     self.assert_json_values(result,
                             params={
                                 'first': 'a',
                                 'second': 'b'
                             },
                             method='method',
                             id=Regex(r'\d+'))
Пример #5
0
 def test_all(self):
     result = jsonrpc.encodeRequest('method',
                                    'abcdef',
                                    id_=123,
                                    version=2.0)
     self.assert_json_values(result,
                             jsonrpc='2.0',
                             params='abcdef',
                             method='method',
                             id=123)
Пример #6
0
    def callRemote(self, method, *args, **kwargs):
        """
        Remotely calls the method, with args. Given that we keep reference to
        the call via the Deferred, there's no need for id. It will coin some
        random anyway, just to satisfy the spec.

        @type method: str
        @param method: Method name

        @type *args: list
        @param *args: List of agruments for the method.

        @rtype: t.i.d.Deferred
        @return: Deferred, that will fire with whatever the 'method' returned.
        @TODO support batch requests
        """

        if kwargs:
            json_request = jsonrpc.encodeRequest(method,
                                                 kwargs,
                                                 version=self.version)
        else:
            json_request = jsonrpc.encodeRequest(method,
                                                 args,
                                                 version=self.version)

        body = StringProducer(json_request)

        headers_dict = {'Content-Type': ['application/json']}
        if not isinstance(self.credentials, Anonymous):
            headers_dict.update(self._getBasicHTTPAuthHeaders())
        headers = Headers(headers_dict)

        d = self.agent.request(b'POST', self.url.encode(), headers, body)
        d.addCallback(self.checkAuthError)
        d.addCallback(self.bodyFromResponse)
        d.addCallback(jsonrpc.decodeResponse)
        return d
Пример #7
0
 def test_methodIdInt(self):
     result = jsonrpc.encodeRequest('method', id_=123)
     expected = '{"method": "method", "id": 123}'
     self.assert_json(result, expected)
Пример #8
0
 def test_methodKwargs(self):
     result = jsonrpc.encodeRequest('method', {'first': 'a', 'second': 'b'})
     pattern = '\{"params": \{"second": "b", "first": "a"\}, '
     pattern += '"method": "method", "id": \d+\}'
     self.assertTrue(re.match(pattern, result))
Пример #9
0
 def test_onlyMethod(self):
     result = jsonrpc.encodeRequest('method')
     pattern = r'\{"method": "method", "id": \d+\}'
     self.assertTrue(re.match(pattern, result))
Пример #10
0
 def test_methodArgsId(self):
     result = jsonrpc.encodeRequest('method', 'abcdef', id_=123)
     self.assert_json_values(result,
                             params='abcdef',
                             method='method',
                             id=123)
Пример #11
0
 def test_methodArgsVersion2(self):
     result = jsonrpc.encodeRequest('method', 'abcdef', version=2)
     pattern = '\{"jsonrpc": "2.0", "params": "abcdef", "method": '
     pattern += '"method", "id": \d+\}'
     self.assertTrue(re.match(pattern, result))
Пример #12
0
 def test_methodArgs(self):
     result = jsonrpc.encodeRequest('method', ['abc', 'def'])
     pattern = '\{"params": \["abc", "def"\], "method": "method", '
     pattern += '"id": \d+\}'
     self.assertTrue(re.match(pattern, result))
Пример #13
0
 def test_methodVersion3(self):
     result = jsonrpc.encodeRequest('method', version=3)
     self.assert_json_values(result, method='method', id=Regex(r'\d+'))
Пример #14
0
 def test_methodKwargs(self):
     result = jsonrpc.encodeRequest('method', {'first': 'a', 'second': 'b'})
     pattern = '\{"params": \{"second": "b", "first": "a"\}, '
     pattern += '"method": "method", "id": \d+\}'
     self.assertTrue(re.match(pattern, result))
Пример #15
0
 def test_methodVersion3(self):
     result = jsonrpc.encodeRequest('method', version=3)
     pattern = '\{"method": "method", "id": \d+\}'
     self.assertTrue(re.match(pattern, result))
Пример #16
0
 def test_methodIdStr(self):
     result = jsonrpc.encodeRequest('method', id_='abc')
     expected = '{"method": "method", "id": "abc"}'
     self.assertEquals(result, expected)
Пример #17
0
 def test_methodArgs(self):
     result = jsonrpc.encodeRequest('method', ['abc', 'def'])
     pattern = '\{"params": \["abc", "def"\], "method": "method", '
     pattern += '"id": \d+\}'
     self.assertTrue(re.match(pattern, result))
Пример #18
0
 def test_methodIdInt(self):
     result = jsonrpc.encodeRequest('method', id_=123)
     expected = '{"method": "method", "id": 123}'
     self.assertEquals(result, expected)
Пример #19
0
 def test_onlyMethod(self):
     result = jsonrpc.encodeRequest('method')
     pattern = '\{"method": "method", "id": \d+\}'
     self.assertTrue(re.match(pattern, result))
Пример #20
0
 def test_methodIdVersion(self):
     result = jsonrpc.encodeRequest('method', version=2.0, id_=123)
     expected = '{"jsonrpc": "2.0", "method": "method", "id": 123}'
     self.assertEquals(result, expected)
Пример #21
0
 def test_methodIdStr(self):
     result = jsonrpc.encodeRequest('method', id_='abc')
     expected = '{"method": "method", "id": "abc"}'
     self.assert_json(result, expected)
Пример #22
0
 def test_methodVersion3(self):
     result = jsonrpc.encodeRequest('method', version=3)
     pattern = '\{"method": "method", "id": \d+\}'
     self.assertTrue(re.match(pattern, result))
Пример #23
0
 def test_methodArgs(self):
     result = jsonrpc.encodeRequest('method', ['abc', 'def'])
     self.assert_json_values(result,
                             params=['abc', 'def'],
                             method='method',
                             id=Regex(r'\d+'))
Пример #24
0
 def test_methodArgsId(self):
     result = jsonrpc.encodeRequest('method', 'abcdef', id_=123)
     expected = '{"params": "abcdef", "method": "method", "id": 123}'
     self.assertEquals(result, expected)
Пример #25
0
 def test_methodVersion2int(self):
     result = jsonrpc.encodeRequest('method', version=2)
     self.assert_json_values(result,
                             jsonrpc='2.0',
                             method='method',
                             id=Regex(r'\d+'))
Пример #26
0
 def test_methodArgsVersion2(self):
     result = jsonrpc.encodeRequest('method', 'abcdef', version=2)
     pattern = '\{"jsonrpc": "2.0", "params": "abcdef", "method": '
     pattern += '"method", "id": \d+\}'
     self.assertTrue(re.match(pattern, result))
Пример #27
0
 def test_methodIdVersion(self):
     result = jsonrpc.encodeRequest('method', version=2.0, id_=123)
     self.assert_json_values(result, jsonrpc='2.0', method='method', id=123)
Пример #28
0
 def test_all(self):
     result = jsonrpc.encodeRequest('method', 'abcdef', id_=123,
                                    version=2.0)
     expected = '{"jsonrpc": "2.0", "params": "abcdef", "method": '
     expected += '"method", "id": 123}'
     self.assertEquals(result, expected)
Пример #29
0
 def test_methodIdVersion(self):
     result = jsonrpc.encodeRequest('method', version=2.0, id_=123)
     expected = '{"jsonrpc": "2.0", "method": "method", "id": 123}'
     self.assertEquals(result, expected)
Пример #30
0
 def test_methodArgsId(self):
     result = jsonrpc.encodeRequest('method', 'abcdef', id_=123)
     expected = '{"params": "abcdef", "method": "method", "id": 123}'
     self.assertEquals(result, expected)