Exemple #1
0
    def _call_waitresponse(self, method, *args, **kwargs):
        """
        Send a request and wait response (blocking API)
        """
        request_id = self._call_sendtodb(method, *args, **kwargs)
        queue = amqp_queue_from_request_id(request_id)
        logging.info("wait response on %r" % queue)

        self._channel = self._conn_amqp.channel()
        self._channel.queue_declare(queue=queue)

        #print(" [*] Waiting for messages on queue '%s'. To exit press CTRL+C" % queue)

        #def callback(ch, method, properties, body):
        #    print(" [x] Received %r" % (body,))
        #    response = pyjsonrpc.parse_response_json(body)
        #    logging.info(response["result"])
        #    self._channel.close()

        #self._channel.basic_consume(callback,
        #              queue=queue,
        #              no_ack=True)
        
        #self._channel.consume(queue, no_ack=True)

        #self._channel.start_consuming()
        
        for method, properties, body in self._channel.consume(queue):
            response = pyjsonrpc.parse_response_json(body)
            break

        #self._channel.queue_delete(queue=queue)
        #self._channel.close()
            
        if response.error:
            logging.error("Error:", response.error.code, response.error.message)
            raise(MyException(response.error.message, response.error.code))
        else:
            result = response.result
            return(result)
    def _call_waitresponse(self, method, *args, **kwargs):
        """
        Send a request and wait response (blocking API)
        """
        request_id = self._call_sendtodb(method, *args, **kwargs)
        queue = amqp_queue_from_request_id(request_id)
        logging.info("wait response on %r" % queue)

        self._channel = self._conn_amqp.channel()
        self._channel.queue_declare(queue=queue)

        # print(" [*] Waiting for messages on queue '%s'. To exit press CTRL+C" % queue)

        # def callback(ch, method, properties, body):
        #    print(" [x] Received %r" % (body,))
        #    response = pyjsonrpc.parse_response_json(body)
        #    logging.info(response["result"])
        #    self._channel.close()

        # self._channel.basic_consume(callback,
        #              queue=queue,
        #              no_ack=True)

        # self._channel.consume(queue, no_ack=True)

        # self._channel.start_consuming()

        for method, properties, body in self._channel.consume(queue):
            response = pyjsonrpc.parse_response_json(body)
            break

        # self._channel.queue_delete(queue=queue)
        # self._channel.close()

        if response.error:
            logging.error("Error:", response.error.code, response.error.message)
            raise (MyException(response.error.message, response.error.code))
        else:
            result = response.result
            return result
Exemple #3
0
def mixed_params_example():

    # Initialize JSON-RPC-Class with JSON-RPC-Methods
    rpc = JsonRpc()

    # Create JSON-RPC-string with mixed params
    request_json = pyjsonrpc.create_request_json("add", 1, b = 2)
    # '{"params": {"b": 2, "__args": [1]}, "jsonrpc": "2.0", "method": "add", "id": "..."}'
    print "Request-JSON:", repr(request_json)

    # RPC-Call
    response_json = rpc.call(request_json)
    # '{"jsonrpc": "2.0", "id": "...", "result": 3}'
    print "Response-JSON:", repr(response_json)

    # Result
    response = pyjsonrpc.parse_response_json(response_json)
    if response.error:
        print "Error:", response.error.code, response.error.message
    else:
        # 3
        print "Result:", response.result
Exemple #4
0
def positional_params_example():

    # Initialize JSON-RPC-Class with JSON-RPC-Methods
    rpc = pyjsonrpc.JsonRpc(methods={"add": add})

    # Create JSON-RPC-string with positional params
    request_json = pyjsonrpc.create_request_json("add", 1, 2)
    # '{"params": [1, 2], "jsonrpc": "2.0", "method": "add", "id": "..."}'
    print "Request-JSON:", repr(request_json)

    # RPC-Call
    response_json = rpc.call(request_json)
    # '{"jsonrpc": "2.0", "id": "...", "result": 3}'
    print "Response-JSON:", repr(response_json)

    # Result
    response = pyjsonrpc.parse_response_json(response_json)
    if response.error:
        print "Error:", response.error.code, response.error.message
    else:
        # 3
        print "Result:", response.result
def named_params_example():

    # Initialize JSON-RPC-Class with JSON-RPC-Methods
    rpc = pyjsonrpc.JsonRpc(methods={"add": add})

    # Create JSON-RPC-string with named params
    request_json = pyjsonrpc.create_request_json("add", a=1, b=2)
    # '{"params": {"a": 1, "b": 2}, "jsonrpc": "2.0", "method": "add", "id": "..."}'
    print("Request-JSON:", repr(request_json))

    # RPC-Call
    response_json = rpc.call(request_json)
    # '{"jsonrpc": "2.0", "id": "...", "result": 3}'
    print("Response-JSON:", repr(response_json))

    # Result
    response = pyjsonrpc.parse_response_json(response_json)
    if response.error:
        print("Error:", response.error.code, response.error.message)
    else:
        # 3
        print("Result:", response.result)
def positional_params_example():

    # Initialize JSON-RPC-Class with JSON-RPC-Methods
    rpc = pyjsonrpc.JsonRpc(methods = {"add": add})

    # Create JSON-RPC-string with positional params
    request_json = pyjsonrpc.create_request_json("add", 1, 2)
    # '{"params": [1, 2], "jsonrpc": "2.0", "method": "add", "id": "..."}'
    print "Request-JSON:", repr(request_json)

    # RPC-Call
    response_json = rpc.call(request_json)
    # '{"jsonrpc": "2.0", "id": "...", "result": 3}'
    print "Response-JSON:", repr(response_json)

    # Result
    response = pyjsonrpc.parse_response_json(response_json)
    if response.error:
        print "Error:", response.error.code, response.error.message
    else:
        # 3
        print "Result:", response.result
def test_named_params_example():

    # Initialize JSON-RPC-Class with JSON-RPC-Methods
    rpc = pyjsonrpc.JsonRpc(methods = {"add": add})

    # Create JSON-RPC-string with named params
    request_json = pyjsonrpc.create_request_json("add", a = 1, b = 2)
    # '{"params": {"a": 1, "b": 2}, "jsonrpc": "2.0", "method": "add", "id": "..."}'
    #print "Request-JSON:", repr(request_json)

    # RPC-Call
    response_json = rpc.call(request_json)
    # '{"jsonrpc": "2.0", "id": "...", "result": 3}'
    #print "Response-JSON:", repr(response_json)

    # Result
    response = pyjsonrpc.parse_response_json(response_json)
    if response.error:
        print "Error:", response.error.code, response.error.message
    else:
        # 3
        #print "Result:", response.result
        assert response.result == 3

import pyjsonrpc


def add(a, b):
    """Test function"""
    return a + b


# 1. Initialize JSON-RPC class with JSON-RPC method(s)
rpc = pyjsonrpc.JsonRpc(methods = {"add": add})

# 2. Create JSON-RPC string with parameters (= request string)
request_json = pyjsonrpc.create_request_json("add", 1, 2)
# request_json = '{"method": "add", "params": [1, 2], "id": "...", "jsonrpc": "2.0"}'

# 3. Call the JSON-RPC function and get back the JSON-RPC result (= response string)
response_json = rpc.call(request_json)
# response_json = '{"result": 3, "id": "...", "jsonrpc": "2.0"}'

# 4. Convert JSON-RPC string to Python objects
response = pyjsonrpc.parse_response_json(response_json)

# 5. Print result or error
if response.error:
    print "Error:", response.error.code, response.error.message
else:
    print "Result:", response.result

sys.path.insert(0, APPDIR)
# END --- required only for testing, remove in real world code --- END

import pyjsonrpc


def add(a, b):
    """Test function"""
    return a + b


# 1. Initialize JSON-RPC class with JSON-RPC method(s)
rpc = pyjsonrpc.JsonRpc(methods={"add": add})

# 2. Create JSON-RPC string with parameters (= request string)
request_json = pyjsonrpc.create_request_json("add", 1, 2)
# request_json = '{"method": "add", "params": [1, 2], "id": "...", "jsonrpc": "2.0"}'

# 3. Call the JSON-RPC function and get back the JSON-RPC result (= response string)
response_json = rpc.call(request_json)
# response_json = '{"result": 3, "id": "...", "jsonrpc": "2.0"}'

# 4. Convert JSON-RPC string to Python objects
response = pyjsonrpc.parse_response_json(response_json)

# 5. Print result or error
if response.error:
    print "Error:", response.error.code, response.error.message
else:
    print "Result:", response.result