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 setup.py and cfg. 6 years ago
.gitignore 0.0.2: update setup.py and gitignore 6 years ago
README.md 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 setup.py and cfg. 6 years ago
setup.py 0.0.2: Big code overhaul -- much less shitty 5 years ago

README.md

rest-api-template

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

Includes:

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

Usage

The main API stub is easy to set up:

  • Import the API modules:
lang=python
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:
lang=python
    manager = RESTAPIManager()
    manager.load_logging()
    log.LoggingDriver.from_config(manager.config.get_section("logging"), name = "myapp")
    log = log.LoggingDriver.get_logger(name = "myapp.__main__")
    manager.load_bottle()
    routing.load_routing_modules(manager) # See `Routing Modules`
    manager.load_dsn()
  • Run Bottle:
lang=python
    try:
        manager.run_bottle()
    except:
        manager.dsn.client.captureException()

Routing Modules

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

lang=python
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('__init__.py') 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:
lang=python
import json
from rest_api.routing import base
from rest_api.routing.base import api_route
... other imports ...
  • Create your routing module:
lang=python
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:

lang=python
from rest_api import routing
...
routing.load_routing_modules(managerInstance, package = "path.to.routing.package")