Skip to main content

Installation

$ pip install bottle-api

Basic Usage

json_endpoint decorator will make a function to JSON WebAPI endpoint. decorated function will return bottle.HTTPResponse.

see this sample web app:

#!python
from bottleapi import WebApiError
from bottleapi.jsonapi import json_endpoint
from bottle import Bottle, request

app = Bottle()

@json_endpoint
def devide():
    a = int(request.params['a'])
    b = int(request.params['b'])
    if b == 0:
        raise WebApiError('b cannot be zero', status=400)

    result = a / b
    return dict(value=result)

app.route('/devide', ['GET'], devide)

if you access /device?a=1&b=1, it will return 200 OK response with body:

{"status": "ok", "result": {"value": 1}}

with Content-Type application/json

but when you access device?a=1&b=0, you will get 400 BAD REQUEST response with body:

{"status": "error", "message": "b cannot be zero", "result": null}

If you want to use JSONP, you can specify callback function name with parameter(j by default) So accessing /device?a=4&b=2&j=my_callback will result:

my_callback({"status": "ok", "result": {"value": 2}});

If you dont like parameter or result data format, You can define your own formatters(success/error). See jsonapi.py for customized formatter example.

Download files

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

Source Distribution

bottle-api-0.0.2.tar.gz (3.5 kB view details)

Uploaded Source

File details

Details for the file bottle-api-0.0.2.tar.gz.

File metadata

  • Download URL: bottle-api-0.0.2.tar.gz
  • Upload date:
  • Size: 3.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for bottle-api-0.0.2.tar.gz
Algorithm Hash digest
SHA256 1e8c5a6921d639b376ade65d5d60a173353f8587f5e38a6653b951a413caedf8
MD5 6b064de05dd43c0d2373ea1c4e1355f4
BLAKE2b-256 53ff394da57eb0d13add8e472535675be9e75b097a14b73044489654cce5cee0

See more details on using hashes here.

Release history Release notifications | RSS feed

0.0.4

1 file

0.0.3

1 file

This release

0.0.2 This release

1 file

0.0.1

1 file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page