Pre-request
This framework is designed to validate params for Restful api request.
We can validate complex struct and field, including Cross Field, Cross Struct
Install
you can simply install it by:
pip install pre-request
Document
Pre-request manual could be found at: https://pre-request.readthedocs.io/en/master/index.html
A Simple Example
This is very easy to use pre-request in your project
from flask import Flask
from pre_request import pre, Rule
app = Flask(__name__)
args = {
"userId": Rule(type=int, required=True)
}
@app.route("/")
@pre.catch(args)
def hello_world(params):
from flask import g
return str(params == g.params)
what happened in this code ?
Use pre-request library to import a global object pre
Define request params rule, userId must be type of int and required
Use @pre.catch(req_params) to filter input value
Use ~flask.g or def hello_world(params) to get formatted input value。
Complex Example
We use a very complex example to show the powerful of this framework
from flask import Flask
from pre_request import pre, Rule
args = {
"userFirst": {
"userId": Rule(type=int, required=False),
"socialInfo": {
"gender": Rule(type=int, enum=[1, 2], default=1),
"age": Rule(type=int, gte=18, lt=80),
"country": Rule(required=True, deep=False)
}
},
"userSecond": {
"userId": Rule(type=int, required=False, neq_key="userFirst.userId"),
"socialInfo": {
"gender": Rule(type=int, enum=[1, 2], default=1, neq_key="userFirst.socialInfo.gender"),
"age": Rule(type=int, gte=18, lt=80, required_with="userFirst.socialInfo.age"),
"country": Rule(required=True, deep=False)
}
}
}
app = Flask(__name__)
app.config["TESTING"] = True
client = app.test_client()
@app.route("/structure", methods=["GET", "POST"])
@pre.catch(args)
def structure_handler(params):
return str(params)
if __name__ == "__main__":
resp = app.test_client().post("/structure", json={
"userFirst": {
"userId": "13",
"socialInfo": {
"age": 20,
}
},
"userSecond": {
"userId": 14,
"socialInfo": {
"age": 21
}
},
"country": "CN",
"userFirst.socialInfo.gender": 1,
"userSecond.socialInfo.gender": 2,
})
print(resp.get_data(as_text=True))
Use parse
We can use function pre.parse instead of decorator @pre.catch()
args = {
"params": Rule(email=True)
}
@app.errorhandler(ParamsValueError)
def params_value_error(e):
return pre.fmt_resp(e)
@app.route("/index")
def example_handler():
rst = pre.parse(args)
return str(rst)
Contributing
How to make a contribution to Pre-request, see the contributing.
Coffee
Please give me a cup of coffee, thank you!
BTC: 1657DRJUyfMyz41pdJfpeoNpz23ghMLVM3
ETH: 0xb098600a9a4572a4894dce31471c46f1f290b087
Links
Documentaion: https://pre-request.readthedocs.io/en/master/index.html
Issue tracker: https://github.com/Eastwu5788/pre-request/issues
Test status: https://coveralls.io/github/Eastwu5788/pre-request
Metadata
Release files for pre-request 2.1.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| pre_request-2.1.5.tar.gz | 18.5 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pre_request-2.1.5-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 47.7 kB
Release files / pre_request-2.1.5.tar.gz
| Download URL | pre_request-2.1.5.tar.gz |
|---|---|
| Size | 18.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c0b5840042eab0b6784a74325a3d0a539d01b03291eb94225abdfe84a526632d
|
|
BLAKE2b-256 checksum How to use checksums |
7983386f922e92ac489361c89989f0c584f0adacd025b44c4eda1ddde8fd1b12
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.10
|
Release files / pre_request-2.1.5-py3-none-any.whl
| Download URL | pre_request-2.1.5-py3-none-any.whl |
|---|---|
| Size | 29.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f4b0bb09ef378101107db5ba4211325b8fc490edbc46a990919ba20eb3a91b0c
|
|
BLAKE2b-256 checksum How to use checksums |
556b9b7d088abb176bdc8181fad69c00099b74a76854bdecd984cea81100d6eb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.10
|
Security for pre-request 2.1.5
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.