Git Product home page Git Product logo

jsonrpcserver's Introduction

jsonrpcserver

PyPI Code Quality Coverage Status Downloads

Process incoming JSON-RPC requests in Python.

pip install jsonrpcserver
from jsonrpcserver import method, serve, Success

@method
def ping():
    return Success("pong")

if __name__ == "__main__":
    serve()

Or use dispatch instead of serve:

response = dispatch('{"jsonrpc": "2.0", "method": "ping", "id": 1}')
# => '{"jsonrpc": "2.0", "result": "pong", "id": 1}'

Watch a video on how to use it.

Full documentation is at jsonrpcserver.com.

See also: jsonrpcclient

jsonrpcserver's People

Contributors

bcb avatar brahmlower avatar deptyped avatar embray avatar ferrouswheel avatar gamepad64 avatar gonvaled avatar marcelotrevisani avatar mikepii avatar nicola-lunghi avatar pawel-slowik avatar roadscape avatar rwckr avatar s4mw1s3 avatar st31ny avatar voice-of-texnoforge avatar ysangkok 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  avatar  avatar

jsonrpcserver's Issues

Call notifications in batch requests

Just noticed this in batch requests:

# Call each request
response = [r.call(methods) for r in requests
            if not r.is_notification]

It looks like it's not calling notifications. Instead it should be removing the notification responses.

Add an async module

Which provides the asynchronous versions of methods, Methods, and dispatch.

User can then import the same names either from jsonrpcserver or jsonrpcserver.async.

Methods should not subclass dict

Continuing on from #10, Methods shouldn't subclass dict, because adding a function can trample on dict's attributes such as get.

Subclass object or list instead.

Include a basic http server

Give the option of a basic built-in server.

from jsonrpcserver import Methods
methods = Methods()
methods.add(lambda: 'meow', 'cat')
methods.serve_forever()

Add Django example

Just include the views.py. Use something like:

def post(request):
    response = dispatch(methods, request.body.decode("utf-8"))
    return HttpResponse(str(response), status=response.http_status)

Add an asynchronous server

Subclass Methods with an AsyncMethods class, which starts it's own asynchronous server when you call serve_forever. (Requires #21 first)

Change ZeroMQ documentation

Really enjoy using your library. I think you might need to update your ZeroMQ example though. In the example, you don't reply with a message if Response.is_notifcation == True. If you don't return a response, I believe this will never return a zmq.REP type socket to a readable state.

Allow request_id int 0

The jsonrpc spec for id in requests states An identifier established by the Client that MUST contain a String, Number, or NULL value if included. If it is not included it is assumed to be a notification.

Currently, jsonrpcserver throws when encountering "id":0. Possibly "id":"" should be allowed too. Handling the null value is less trivial.

https://github.com/bcb/jsonrpcserver/blob/master/jsonrpcserver/response.py#L80

        # Ensure we're not responding to a notification with data
        if not request_id:
            raise ValueError(
                'Requests must have an id, use NotificationResponse instead')

Validate batch request only once

Currently with batch requests, it loops through each request validating each one against the schema. Validate the entire request in one go.

Improve batch processing performance

Currently looping through the batch twice, once to build a list of Requests, and another time to call them. Do the Request part in the same time as calling. Basically remove the loop from Requests.__init__

Improve configuration

Configuration currently requires modifying class attributes of Request and ErrorResponse. It should be easier, something like:

from jsonrpcserver import config
config.debug = True
config.schema_validation = False

Allow asynchronous processing

There's no way to asynchronously process a request, which is a major inadequacy of the library. Should be able to await dispatch. Need a dispatch coroutine, or at least return a future from dispatch.

Will need some heavy refactoring to support both synchronous and asynchronous processing.

Tornado server example can not support batch

In the example :https://jsonrpcserver.readthedocs.io/en/latest/examples.html#tornado

when client post a list json params, the server will be hang and do nothing.

☁  itiger  curl -X 'POST' -H 'Content-Type: application/json' -d '{"jsonrpc": "2.0", "method": "ping", "id": 1}' http://127.0.0.1:5000/
{"jsonrpc": "2.0", "result": "pong", "id": 1}%            
☁  itiger  curl -X 'POST' -H 'Content-Type: application/json' -d '[{"jsonrpc": "2.0", "method": "ping", "id": 1}, {"jsonrpc": "2.0", "method": "ping", "id": 2}]' http://127.0.0.1:5000/

the await methods.dispatch(request) will be hang , methods.dispatch return nothing.

Add Methods.dispatch

Would allow this usage:

methods.dispatch(request)

Saves having to import dispatch.

Remove the need for instantiating Methods

Rather than

methods = Methods()
@methods.add

It could just have an internal method registry, like:

from jsonrpcserver import methods

@methods.add
def ping():
    return 'pong'

if __name__ == '__main__':
    methods.serve_forever()

Add serialisation options

To minimise the payload, allow expanding serialised json before processing.

Such as avro, protobuf, hpack, gzip

Reverse dispatch arguments

In order to have simpler usage (#25), should reverse the order of the dispatch arguments, to make the "methods" argument optional. Without methods specified, it uses the internal methods registry.

dispatch(request)

A breaking change..........

Access framework HTTP Request from method

This is a question as much as an issue. I have several jsonrpc methods that require db, redis, and other state/config access that would normally be passed using the the 'app' or 'request' object. What is the best method to access the parent http request and/or web app object from a method?

It seems like its necessary to modify/create/subclass:

I will be making these changes for use in our projects (jussi,yo,sbds,hivemind).

I'm thinking that changing the signature of the dispatch method (and all the other methods that lead to the jsonrpc method) to also accept *args, **kwargs is the best, most modular way:

async def handle(aio_http_request):
    statsd = aio_http_request.app.config['statsd_client']
    jsonrpc_request = await aio_http_request.text()
    response = await methods.dispatch(jsonrpc_request, aio_http_request, statsd=statsd)
    return web.json_response(response)

I presume the use case for db/cache/metrics isn't rare, so I thought I would solicit your expertise and opinion about how these changes are implemented, especially if you're interested in adding my changes upstream.

Thanks for you excellent work!

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.