Skip to main content

Reqplus

A lib for strict Flask reqparse

This is an easy way to raise required parameters using reqparse from Flask Restful framework.

Installation

pip install reqplus

Required

Development

1- Create a normal reqparse object

from flask_restful import reqparse

parser = reqparse.RequestParser(bundle_errors=True)
parser.add_argument('id', type=int, required=True, location='args', help='Id is required')

2- Insteated using parse_args replace it for pre_parse_args, StrictParser is required for use pre_parse_args

from flask_restful import reqparse
from reqplus import StrictParser

parser = reqparse.RequestParser()
parser.add_argument('id', type=int, required=True, location='args', help='Id is required')

# Old one >>> args = parser.parse_args()
args = StrictParser.pre_parse_args(parser)

pre_parse_args will return a dictionary with the values from the request like a normal reqparse implementation, if one parameter is required and isn't in the request pre_parse_args will raise an exception with the help message you set.

Take care using required parameters, for required parameters you have to set the help parameter to display correctly the message.

In a normal reqparse implementation you will use the next structure:

# Taked from the official documentation
from flask_restful import reqparse

parser = reqparse.RequestParser(bundle_errors=True)
parser.add_argument('foo', type=int, required=True, help='foo error message')
parser.add_argument('bar', type=int, required=True, help='bar error message')

# Error response
{
    "message":  {
        "foo": "foo error message",
        "bar": "bar error message"
    }
}

With this implementation you will get an object named "message" inside another object named "data" with every error message with a key named equal to the parameter name, for this case you have to validate in the exception section looking for an easy way to return this values.

Normally an API returns static messages doing "try cath" blocks for raise all the incoming exceptions and returning messages like the following example:

"message" : "foo error message"

With reqparse you will have the following message:

"message" : "400 Bad Request: The browser (or proxy) sent a request that this server could not understand.".

To improve this error reqplus intercept the incoming request and comparate it with de reqparse arguments raising an exception if any argument don't meet the conditions.

pre_parse_args also accept the current parameters from reqparse and use the parameter bundle_errors from reqparse declaration to bundle all the error from de validation in one exception.

Example

# strict=True
# http_error_code=400
args = StrictParser.pre_parse_args(parser, strict=True, http_error_code=400)

Set bundle_errors in false to get the first posible error

# Old
"message":  {
    "foo" : "foo error message",
    "bar" : "bar error message"
}

# New 
# bundle_errors:false
"message" : "foo error message"
# bundle_errors:true
"message" : "foo error message, bar error message"

Comments

Any recomendation is acepted.

Licence

GNU - Yeah baby!

Release files for reqplus 0.0.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 reqplus 0.0.3
File Size Uploaded
reqplus-0.0.3.tar.gz 15.3 kB Details

Release files / reqplus-0.0.3.tar.gz

Download URL reqplus-0.0.3.tar.gz
Size 15.3 kB
Tags Source
SHA-256 checksum
How to use checksums
45b34b5156049be86b1d57c716f58d640e18d13ac666f67666d39be916713f93
BLAKE2b-256 checksum
How to use checksums
67e1477d0a5224c82e883af1a74fb2a86afc0b7b02efb36a07d1330ebfdbf8b3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.0 CPython/3.8.10

Release history Release notifications | RSS feed

This release

0.0.3 This release

1 release file

0.0.2

1 release file

0.0.1

1 release 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