Skip to main content

A Flask and Flask-RESTful argument parser.

Project description

membrane.py 🦠

A Flask arg-parser & splitter to ensure wanted data gets through, unwanted data stays out, and required data shows up.

Compatible with Flask-RESTful. 👍

Basic Usage 🐍

from flask import Flask
app = Flask(__name__)


@app.route('/')
def route_handler():
    return "No args~!"

Above we see a basic Flask application with a root API handler. No query options are yet passed to our root.

Let's say we wanted to handle the query parameter ?limit=0 in the request handler. We can use membrane to do this.

from flask import Flask, jsonify
from membrane import membrane
app = Flask(__name__)


@app.route('/')
@membrane({'limit': int})
def route_handler(microbe):
	'''Here microbe should be either an empty dict: {} or dict: {"limit": ?}'''

    return jsonify(microbe)

Defining Membranes 🔬

The membrane, a wrapper function, can take multiple layers of type dict which will act as a filter for the: 1) query parameters; 2) body; and 3) headers passed to the request handler. The route_handler will take these filtered dicts as *args.

The basic structure is as follows:

simple_membrane = {"limit": int}
complex_membrane = {
	"other_parameter": {
		"type": lambda x: int(x),
		"options": (1, 2, 3,),
		"default": 1,
		"required": False,
		"location": "params"
	}
}

In the simple_membrane, we want to take limit from either the params, body, or headers and convert it to an int (or verify that it is of type int). We're also allowed to use custom functions to handle conversions here.

The complex_membrane is different in that it's a dict with additional options.

  • type: any callable function or class.
  • options: a tuple of accepted values.
  • default: if not provided, use this as a default. If you need this value to be generated at the time of parsing, provide a callable function that returns the proper value.
  • required: should the server throw an error if this value is not provided?
  • location: where in the request should we search for this value: ("params", "body", "headers", "files").

To use multiple layers for a route handler, simply list them out.

@app.route('/')
@membrane(simple_membrane, complex_membrane)
def route_handler(simple_microbe, complex_microbe):
    return jsonify([simple_microbe, complex_microbe])

Error Handling ⚠️

Exported from membrane are exceptions: RequiredFieldError, NotAnOptionError, TypeConversionError.

You can handle these errors yourself or use our default error handler.

from membrane import error_handler

app = Flask(__name__)
error_handler(app)

Otherwise you can handle these errors however you like.

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

membrane-0.0.4.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

membrane-0.0.4-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file membrane-0.0.4.tar.gz.

File metadata

  • Download URL: membrane-0.0.4.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.4.2 requests/2.21.0 setuptools/41.1.0 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.7.5rc1

File hashes

Hashes for membrane-0.0.4.tar.gz
Algorithm Hash digest
SHA256 00a19ec79f012edc5cd0dee496a8ad6f09c06ad58de749199aaa7e5de10fb1b4
MD5 c31767a6f2ee7f081a5cae5a9513bd2d
BLAKE2b-256 716c9dcb579fa60a23551a56f065a4ecfc058a9ad50e3caef08fab202c010e92

See more details on using hashes here.

File details

Details for the file membrane-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: membrane-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.4.2 requests/2.21.0 setuptools/41.1.0 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.7.5rc1

File hashes

Hashes for membrane-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 3b99db52232c1daf57f84bec7fbfbe92d91568857f503448dd5d57a3593a9d1b
MD5 a1b5f4f0d766208dc9c35e3518c28e4b
BLAKE2b-256 33fe19857336f39d3a9ee738934cdbf5908d441cc3146d85f3254361089a6ed1

See more details on using hashes here.

Supported by

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