Skip to main content

A lib for strict Flask reqparse

Project description

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!

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

reqplus-0.0.3.tar.gz (15.3 kB view details)

Uploaded Source

File details

Details for the file reqplus-0.0.3.tar.gz.

File metadata

  • Download URL: reqplus-0.0.3.tar.gz
  • Upload date:
  • Size: 15.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.8.10

File hashes

Hashes for reqplus-0.0.3.tar.gz
Algorithm Hash digest
SHA256 45b34b5156049be86b1d57c716f58d640e18d13ac666f67666d39be916713f93
MD5 d450dae5b23adbca5edc660049130789
BLAKE2b-256 67e1477d0a5224c82e883af1a74fb2a86afc0b7b02efb36a07d1330ebfdbf8b3

See more details on using hashes here.

Supported by

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