BottlePy+Malibu Template for creating REST APIs and Applications
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Sean Johnson ac161352a1
Add finally case for release tagger
5 years ago
src/restify Add finally case for release tagger 5 years ago
tests 0.2: Added Python and cfg. 6 years ago
.gitignore 0.0.2: update and gitignore 6 years ago 0.2: Doc updates, routing module loader fixes 6 years ago
UNLICENSE 0.1: Initial commit 6 years ago
api.example.ini 0.2: add setting for quiet mode 6 years ago
requirements.txt Update requirements.txt 5 years ago
setup.cfg 0.2: Added Python and cfg. 6 years ago 0.0.2: Big code overhaul -- much less shitty 5 years ago


This is a template for creating a REST-like API with Bottle and some other Python frameworks.


  • DSN / Debugging Plugin
  • Malibu Logger integration
  • Autoloaded DSN modules
  • Autoloaded routing modules


The main API stub is easy to set up:

  • Import the API modules:
from rest_api import routing, manager, util
from rest_api.manager import RESTAPIManager
from malibu.util import log
  • Create the manager and load the modules:
    manager = RESTAPIManager()
    log.LoggingDriver.from_config(manager.config.get_section("logging"), name = "myapp")
    log = log.LoggingDriver.get_logger(name = "myapp.__main__")
    routing.load_routing_modules(manager) # See `Routing Modules`
  • Run Bottle:

Routing Modules

It is recommended that you put your routing modules in their own package. Inside your package, drop a with the following content:

import glob, os

modules = glob.glob(os.path.dirname(__file__) + "/*.py")
__all__ = [os.path.basename(f)[:-3] for f in modules if not os.path.basename(f).startswith('_') and not f.endswith('') and os.path.isfile(f)]

This essentially indexes the package and stores a list of modules inside the __all__ variable for the module loader to read.

Creating routing modules is a relatively straightforward process:

  • Import the module API and JSON module:
import json
from rest_api.routing import base
from rest_api.routing.base import api_route
... other imports ...
  • Create your routing module:
class ExampleAPIRouter(base.APIRouter):

    def __init__(self, manager):

        base.APIRouter.__init__(self, manager)

    @api_route(path = "/example", actions = ["GET"])
    def do_example():
        """ GET /example

            Returns an example.

        response = base.generate_bare_response()
        response.update({"example": true})

        yield json.dumps(response) + "\n"

register_route_providers = [ExampleAPIRouter]
  • Test it!

If you've directly cloned rest_api_template and refactored the code, place your router in rest_api.routing and it will be loaded automatically.

NOTE: If you're using rest_api_template as a submodule, make sure to load your routing modules in your loader stub:

from rest_api import routing
routing.load_routing_modules(managerInstance, package = "")