Skip to main content

Simple JSON-RPC 2.0 compilant middleware for python using decorators. With just a few lines of code you get a JSONRPC 2.0 compilant web API running and ready for your needs!

Project description

Build Status

Simple JSON-RPC 2.0 compilant middleware for python using decorators. With just a few lines of code you get a JSONRPC 2.0 compilant web API running and ready for your needs!

Usage

Just create a service class to store your API calls and decorate your methods:

from simplejsonrpc import *

loginservice = SimpleJSONRPCService(api_version=1)

@jsonremote(loginservice, name='login', doc='Method used to log a user in')
def login(request, user_name, user_pass):
    (...)

More complex example interating it into [Web.py!]

import web

urls = (
    '/', 'index'
    , '/api/', 'json_handler'
    , '/api', 'json_handler'
)
app = web.application(urls, globals())

api_service = SimpleJSONRPCService(api_version=1)

class index:
    def GET(self, name):
        if not name:
            name = 'World'
        return 'Hello, ' + name + '!'

class json_handler:
    def POST(self):
        return api_service(web.webapi.data())

@jsonremote(api_service, doc='print api documentation')
def api(request):
    return api_service.api()

@jsonremote(api_service, doc='ping server')
def ping(request):
    return "pong"

if __name__ == "__main__":
    app.run()

Adding API versioning to the previous example

import web

urls = (
    '/', 'index'
    , '/api/1/', 'json_handler_v1'
    , '/api/1', 'json_handler_v1'
    , '/api/2/', 'json_handler_v2'
    , '/api/2', 'json_handler_v2'
)
app = web.application(urls, globals())

api_service_v1 = SimpleJSONRPCService(api_version=1)
api_service_v2 = SimpleJSONRPCService(api_version=2)

class index:
    def GET(self, name):
        if not name:
            name = 'World'
        return 'Hello, ' + name + '!'

class json_handler_v1:
    def POST(self):
        return api_service_v1(web.webapi.data())

class json_handler_v1:
    def POST(self):
        return api_service_v2(web.webapi.data())

@jsonremote(api_service_v1, doc='print api documentation')
def api(request):
    return api_service_v1.api()

@jsonremote(api_service_v1, doc='ping server')
def ping(request):
    return "pong"

@jsonremote(api_service_v2, name='api', doc='print api documentation')
def api_v2(request):
    return api_service_v2.api()

@jsonremote(api_service_v2, name='ping', doc='ping server')
def ping_v2(request):
    return "pong"

if __name__ == "__main__":
    app.run()

License

The MIT License (MIT)

Copyright (c) 2014-2016 Moritz Wundke

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

simplejsonrpc-1.2.1.zip (2.6 kB view details)

Uploaded Source

File details

Details for the file simplejsonrpc-1.2.1.zip.

File metadata

  • Download URL: simplejsonrpc-1.2.1.zip
  • Upload date:
  • Size: 2.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for simplejsonrpc-1.2.1.zip
Algorithm Hash digest
SHA256 c24949e1093e9f3ce742bf8697175936e07a502048dcf04372779d04f6f44c7c
MD5 bbe043e4c6b4ba10eec1c421faa15bb7
BLAKE2b-256 d9abe53bf7be7afa8823a80224afa43a9ff7690037f2b6b716257addae85e135

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page