Coder Social home page Coder Social logo

simplejsonrpc's Introduction

simplejsonrpc

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!

Install

Just install it though pip:

pip install simplejsonrpc

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 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.

simplejsonrpc's People

Contributors

moritz-wundke avatar

Watchers

 avatar  avatar

Forkers

joecare zcattacz

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.