Code Monkey home page Code Monkey logo

jsonrpcclient's Introduction

jsonrpcclient

PyPI Code Quality Coverage Status Downloads

Create JSON-RPC requests and parse responses in Python.

pip install jsonrpcclient

Generate a request:

from jsonrpcclient import request, parse
req = request("ping")
# => {'jsonrpc': '2.0', 'method': 'ping', 'id': 1}

Parse a response:

parsed = parse({"jsonrpc": "2.0", "result": "pong", "id": 1})
# => Ok(result='pong', id=1)

For strings, use request_json and parse_json.

Watch a video on how to use it

Full documentation is at jsonrpcclient.com.

See also: jsonrpcserver

jsonrpcclient's People

Contributors

aqw avatar bcb avatar crazyumka avatar elihunter173 avatar embray avatar fastfailure avatar ferrouswheel avatar fmcorz avatar ivanryavkin avatar littlelamplight avatar oxullo avatar pgold avatar saaj avatar sbrandtb avatar tekulvw avatar villainy avatar yuuki0xff avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

jsonrpcclient's Issues

Log more attributes of the requests & responses

Currently when logging the requests and responses, only certain attributes can be logged (such as the http headers). Give the ability to log any part, by passing the whole request/response object to the log method.

Add AsyncClient class

Subclass Client with an AsyncClient class. It overrides send (and request and notify), with a coroutine version. It can then call _send_message as a coroutine.

Saves having to mess around with futures as seen in the TornadoClient.

Update TornadoClient to subclass AsyncClient

Update TornadoClient to use the new AsyncClient class. Should look something like:

async def _send_message(self, request, **kwargs):
    response = await self.http_client.fetch(
        self.endpoint, method='POST', body=request, headers=headers, **kwargs)
    return self.process_response(response)

This is a breaking change because the current TornadoClient works with python 3.2, whereas using AsyncClient will limit to 3.5+.

Re #16

Refactor Client.send

For more flexible subclassing of the Client class, refactor the send method.

Reason: Currently when a subclass overrides _send_message, it's required to log both the request and response. The parent class should do this work, with the subclass optionally adding extra details to include in the log entry.

Previously send was:

  • send_message (subclass was required to log the request and response)
  • process_response

New procedure:

  • prepare_request (here the subclass can optionally add extra details to include in the request log entry)
  • log_request
  • send_message (here the subclass can optionally add extra details to include in the response log entry, after sending)
  • process_response (includes logging the response)

Add a new syntax for one-time requests

Something like:

>>> from jsonrpcclient.http_client import request
>>> request('http://pets.com/', 'cat', name='Mittens')

It would instantiate a client for making a request, then throw it away.

Rename classes to *Client

Proposing to rename the "server" modules and classes, to "client";

  • HTTPServer becomes HTTPClient
  • ZMQServer becomes ZMQClient
  • TornadoServer becomes TornadoClient

Example code:

>>> from jsonrpcclient.http_client import HTTPClient
>>> HTTPClient('http://cats.com/').request('speak')

The plan would be to deprecate the old names by subclassing.

More reliable logging for Clients

Rather than hoping the client implementations log requests and responses, allow them to override logging methods, optionally. The requests and responses are logged regardless.

Proposal: Add a Response class

Currently the request-like methods return a primitive, normally the "result" part of the JSON-RPC response, such as "pong".

Instead, return an object of type Response. Some reasons to do this:

  • Can hold more information about the response
  • It's easier and more familiar for client classes to return Response(), rather than return self.process_response().

Down side would be, the user would have to use an attribute of the response (response.result or something) rather than just the raw return value. Edit: unless we override __new__ to set the object's value. Otherwise, Breaking change

Can't send a batch request as a list of JSON strings

Sending a batch as one big json string works:

>>> client.send('[{"method": "ping", "id": 5, "jsonrpc": "2.0"}, {"method": "ping", "id": 6, "jsonrpc": "2.0"}]')
--> [{"method": "ping", "id": 5, "jsonrpc": "2.0"}, {"method": "ping", "id": 6, "jsonrpc": "2.0"}]
<-- [{"result": "pong", "id": 5, "jsonrpc": "2.0"}, {"result": "pong", "id": 6, "jsonrpc": "2.0"}] (200 OK)
[{'result': 'pong', 'id': 5, 'jsonrpc': '2.0'}, {'result': 'pong', 'id': 6, 'jsonrpc': '2.0'}]

But not as a list of json strings:

>>> client.send(['{"method": "ping", "id": 5, "jsonrpc": "2.0"}', '{"method": "ping", "id": 6, "jsonrpc": "2.0"}'])
--> ["{\"method\": \"ping\", \"id\": 5, \"jsonrpc\": \"2.0\"}", "{\"method\": \"ping\", \"id\": 6, \"jsonrpc\": \"2.0\"}"]
<-- [{"id": null, "jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}}, {"id": null, "jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid Request"}}] (200 OK)
[{'id': None, 'jsonrpc': '2.0', 'error': {'code': -32600, 'message': 'Invalid Request'}}, {'id': None, 'jsonrpc': '2.0', 'error': {'code': -32600, 'message': 'Invalid Request'}}]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.