Skip to main content

flask-reqparser-py

Overview

The Flask web framework provides access to request parameters via flask.request.args for GET requests and via flask.request.form for PUT and POST requests. Requiring certain parameters be present for an endpoint, or parsing parameters as custom types, necessitates extra logic to process values from the aforementioned dictionaries and is error-prone. However, API endpoints can act much like command line interfaces, and Python has solved that problem fairly will with the argparse library. flask-reqparser-py exposes a RequestParser class that allows you to define a parser for request arguments, and access them like regular command line arguments.

Installation

Install from PyPi (preferred method)

pip install lc-flask-reqparser

Install from GitHub with Pip

pip install git+https://github.com/libcommon/flask-reqparser-py.git@vx.x.x#egg=lc_flask_reqparser

where x.x.x is the version you want to download.

Install by Manual Download

To download the source distribution and/or wheel files, navigate to https://github.com/libcommon/flask-reqparser-py/tree/releases/vx.x.x/dist, where x.x.x is the version you want to install, and download either via the UI or with a tool like wget. Then to install run:

pip install <downloaded file>

Do not change the name of the file after downloading, as Pip requires a specific naming convention for installation files.

Dependencies

flask-reqparser-py depends on, and is designed to work with, the Flask framework. Only Python versions >= 3.6 are officially supported.

Getting Started

The syntax for defining a RequestParser is almost identical to an ArgumentParser, except that all arguments should be defined as positional, and it supports the builder pattern for adding arguments. RequestParser will take the positional definitions and transform them to act like optional arguments (optional arguments in argparse can still have required=True).

from typing import List

from flask import Flask

from lc_flask_reqparser import RequestParser


app = Flask(__name__)


def CommaSeparatedList(arg: str) -> List[str]:
    """Convert string representation of CSV to list of str."""
    return arg.split(",")


@app.route("/api/describe/person")
def describe_person():
    """API endpoint to describe information about a person."""
    parser = (RequestParser()
              .add_argument("name", required=True)
              .add_argument("nicknames", type=CommaSeparatedList))
    args, _ = parser.parse_args()
    // use args.name or args.nicknames list to lookup user in database
    // and return information in JSON form.

Note that RequestParser.parse_args returns a tuple containing the Namespace with defined arguments, as well as a list of the remaining (undefined) arguments. If an API endpoint only cares about defined arguments, it can safely ignore the second element of the tuple like the example above.

Contributing/Suggestions

Contributions and suggestions are welcome! To make a feature request, report a bug, or otherwise comment on existing functionality, please file an issue. For contributions please submit a PR, but make sure to lint, type-check, and test your code before doing so. Thanks in advance!

Metadata

Release files for lc-flask-reqparser 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for lc-flask-reqparser 0.1.3
File Size Uploaded Provenance Actions
lc_flask_reqparser-0.1.3.tar.gz 4.7 kB Without provenance Details for lc_flask_reqparser-0.1.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for lc-flask-reqparser 0.1.3
File Interpreter ABI Platform Actions
lc_flask_reqparser-0.1.3-py3-none-any.whl Python 3 none any Details for lc_flask_reqparser-0.1.3-py3-none-any.whl

Total release size: 11.1 kB

Release files / lc_flask_reqparser-0.1.3.tar.gz

Download URL lc_flask_reqparser-0.1.3.tar.gz
Size 4.7 kB
Tags Source
SHA-256 checksum
How to use checksums
611742afa421711db58db77bc309bd2a679da3c3cda73bed7639b5518294b1ca
BLAKE2b-256 checksum
How to use checksums
86e559885dbbb6e80414dce69de7c8de30ce7d8622469f9ab7dcf34d353a30e2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.7

Release files / lc_flask_reqparser-0.1.3-py3-none-any.whl

Download URL lc_flask_reqparser-0.1.3-py3-none-any.whl
Size 6.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b2dc3f46a5c9d8b74a99f66934b67d9cea303212f6796633ef41d4692ba5db62
BLAKE2b-256 checksum
How to use checksums
cba26104e05ab2b1448bd81f603566f18941417e8bca53d82ee9e3196bd91057
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.7

Security for lc-flask-reqparser 0.1.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.3 This release

2 release files No provenance

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