-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from Eastwu5788/wudong
feature(): add filter examples
- Loading branch information
Showing
16 changed files
with
546 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# !/usr/local/python/bin/python | ||
# -*- coding: utf-8 -*- | ||
# (C) Wu Dong, 2020 | ||
# All rights reserved | ||
# @Author: 'Wu Dong <wudong@eastwu.cn>' | ||
# @Time: '2020-03-19 10:12' | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# !/usr/local/python/bin/python | ||
# -*- coding: utf-8 -*- | ||
# (C) Wu Dong, 2020 | ||
# All rights reserved | ||
# @Author: 'Wu Dong <wudong@eastwu.cn>' | ||
# @Time: '2020-03-19 11:15' | ||
""" 演示 pre-request 框架如何使用回调函数自定义处理函数 | ||
""" | ||
from flask import Flask | ||
from pre_request import filter_params, Rule | ||
|
||
|
||
app = Flask(__name__) | ||
app.config["TESTING"] = True | ||
client = app.test_client() | ||
|
||
|
||
def call_back_handler(value): | ||
return value + 100 | ||
|
||
|
||
# 指定callback自定义处理函数,注意:此函数的调用在系统处理完之后 | ||
callback_params = { | ||
"params": Rule(direct_type=int, callback=call_back_handler) | ||
} | ||
|
||
|
||
@app.route("/callback", methods=["GET", "POST"]) | ||
@filter_params(callback_params) | ||
def example_callback_handler(params): | ||
return str(params) | ||
|
||
|
||
def example_callback_filter(): | ||
""" 演示回调函数 | ||
""" | ||
resp = client.get("/callback", data={ | ||
"params": 10 | ||
}) | ||
print(resp.data) | ||
|
||
|
||
if __name__ == "__main__": | ||
example_callback_filter() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# !/usr/local/python/bin/python | ||
# -*- coding: utf-8 -*- | ||
# (C) Wu Dong, 2020 | ||
# All rights reserved | ||
# @Author: 'Wu Dong <wudong@eastwu.cn>' | ||
# @Time: '2020-03-19 10:14' | ||
""" 演示 pre-request 框架如何使用邮箱校验 | ||
""" | ||
from flask import Flask | ||
from pre_request import filter_params, Rule | ||
|
||
|
||
app = Flask(__name__) | ||
app.config["TESTING"] = True | ||
client = app.test_client() | ||
|
||
|
||
# 指定email=True,此时框架会自动判断用户入参是否符合email正则 | ||
email_params = { | ||
"params": Rule(email=True) | ||
} | ||
|
||
|
||
@app.route("/email", methods=["GET", "POST"]) | ||
@filter_params(email_params) | ||
def example_email_handler(params): | ||
return str(params) | ||
|
||
|
||
def example_email_filter(): | ||
""" 演示邮箱验证 | ||
""" | ||
resp = client.get("/email", data={ | ||
"params": "wudong@eastwu.cn" | ||
}) | ||
print(resp.data) | ||
|
||
|
||
if __name__ == "__main__": | ||
example_email_filter() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# !/usr/local/python/bin/python | ||
# -*- coding: utf-8 -*- | ||
# (C) Wu Dong, 2020 | ||
# All rights reserved | ||
# @Author: 'Wu Dong <wudong@eastwu.cn>' | ||
# @Time: '2020-03-19 10:23' | ||
""" 演示 pre-request 框架如何使用空值校验 | ||
""" | ||
from flask import Flask | ||
from pre_request import filter_params, Rule | ||
|
||
|
||
app = Flask(__name__) | ||
app.config["TESTING"] = True | ||
client = app.test_client() | ||
|
||
|
||
# 指定email=True,此时框架会自动判断用户入参是否符合email正则 | ||
empty_params = { | ||
"params": Rule(allow_empty=True, default="tmp"), | ||
"must": Rule(allow_empty=False) | ||
} | ||
|
||
|
||
@app.route("/empty", methods=["GET", "POST"]) | ||
@filter_params(empty_params) | ||
def example_empty_handler(params): | ||
return str(params) | ||
|
||
|
||
def example_empty_filter(): | ||
""" 演示邮箱验证 | ||
""" | ||
resp = client.get("/empty", data={ | ||
"params": "wudong@eastwu.cn", | ||
"must": "must" | ||
}) | ||
print(resp.data) | ||
|
||
# params参数不传时,会默认填充default值 | ||
resp = client.get("/empty", data={ | ||
"must": "must", | ||
}) | ||
print(resp.data) | ||
|
||
# 不允许为空的值如果不填写会报错 | ||
resp = client.get("/empty", data={ | ||
"params": "ss" | ||
}) | ||
print(resp.data) | ||
|
||
|
||
if __name__ == "__main__": | ||
example_empty_filter() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# !/usr/local/python/bin/python | ||
# -*- coding: utf-8 -*- | ||
# (C) Wu Dong, 2020 | ||
# All rights reserved | ||
# @Author: 'Wu Dong <wudong@eastwu.cn>' | ||
# @Time: '2020-03-19 10:28' | ||
""" 演示 pre-request 框架如何使用枚举校验 | ||
""" | ||
from flask import Flask | ||
from pre_request import filter_params, Rule | ||
|
||
|
||
app = Flask(__name__) | ||
app.config["TESTING"] = True | ||
client = app.test_client() | ||
|
||
|
||
# 指定enum=[10, 20]指定目标枚举值,注意:目前仅支持同数据类型枚举验证 | ||
enum_params = { | ||
"params": Rule(direct_type=int, enum=[10, 20, 30]) | ||
} | ||
|
||
|
||
@app.route("/enum", methods=["GET", "POST"]) | ||
@filter_params(enum_params) | ||
def example_enum_handler(params): | ||
return str(params) | ||
|
||
|
||
def example_enum_filter(): | ||
""" 演示枚举验证 | ||
""" | ||
resp = client.get("/enum", data={ | ||
"params": 10 | ||
}) | ||
print(resp.data) | ||
|
||
|
||
if __name__ == "__main__": | ||
example_enum_filter() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# !/usr/local/python/bin/python | ||
# -*- coding: utf-8 -*- | ||
# (C) Wu Dong, 2020 | ||
# All rights reserved | ||
# @Author: 'Wu Dong <wudong@eastwu.cn>' | ||
# @Time: '2020-03-19 10:33' | ||
""" 演示 pre-request 框架如何使用Json校验 | ||
""" | ||
import json | ||
from flask import Flask | ||
from pre_request import filter_params, Rule | ||
|
||
|
||
app = Flask(__name__) | ||
app.config["TESTING"] = True | ||
client = app.test_client() | ||
|
||
|
||
# json=True,此时框架会自动将params数据进行json解析 | ||
json_params = { | ||
"params": Rule(json=True) | ||
} | ||
|
||
|
||
@app.route("/json", methods=["GET", "POST"]) | ||
@filter_params(json_params) | ||
def example_json_handler(params): | ||
return str(params) | ||
|
||
|
||
def example_json_filter(): | ||
""" 演示邮箱验证 | ||
""" | ||
resp = client.post("/json", json={ | ||
"params": json.dumps(["hello", "work", "!"]) | ||
}) | ||
print(resp.data) | ||
|
||
|
||
if __name__ == "__main__": | ||
example_json_filter() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# !/usr/local/python/bin/python | ||
# -*- coding: utf-8 -*- | ||
# (C) Wu Dong, 2020 | ||
# All rights reserved | ||
# @Author: 'Wu Dong <wudong@eastwu.cn>' | ||
# @Time: '2020-03-19 11:14' | ||
""" 演示 pre-request 框架如何使用参数字段映射 | ||
""" | ||
from flask import Flask | ||
from pre_request import filter_params, Rule | ||
|
||
|
||
app = Flask(__name__) | ||
app.config["TESTING"] = True | ||
client = app.test_client() | ||
|
||
|
||
# 指定key_map,框架会将参数变更为 key_map 指定的key | ||
map_params = { | ||
"params": Rule(key_map="key") | ||
} | ||
|
||
|
||
@app.route("/map", methods=["GET", "POST"]) | ||
@filter_params(map_params) | ||
def example_map_handler(params): | ||
return str(params) | ||
|
||
|
||
def example_map_filter(): | ||
""" 演示邮箱验证 | ||
""" | ||
resp = client.get("/map", data={ | ||
"params": "wudong@eastwu.cn" | ||
}) | ||
print(resp.data) | ||
|
||
|
||
if __name__ == "__main__": | ||
example_map_filter() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# !/usr/local/python/bin/python | ||
# -*- coding: utf-8 -*- | ||
# (C) Wu Dong, 2020 | ||
# All rights reserved | ||
# @Author: 'Wu Dong <wudong@eastwu.cn>' | ||
# @Time: '2020-03-19 10:49' | ||
""" 演示 pre-request 框架如何使用长度校验,仅针对字符串有效 | ||
""" | ||
from flask import Flask | ||
from pre_request import filter_params, Rule, Length | ||
|
||
|
||
app = Flask(__name__) | ||
app.config["TESTING"] = True | ||
client = app.test_client() | ||
|
||
|
||
# 指定email=True,此时框架会自动判断用户入参是否符合email正则 | ||
length_params = { | ||
"params": Rule(length=Length(2, 4)) | ||
} | ||
|
||
|
||
@app.route("/length", methods=["GET", "POST"]) | ||
@filter_params(length_params) | ||
def example_length_handler(params): | ||
return str(params) | ||
|
||
|
||
def example_length_filter(): | ||
""" 演示字符串长度验证 | ||
""" | ||
resp = client.get("/length", data={ | ||
"params": "abc" | ||
}) | ||
print(resp.data) | ||
|
||
resp = client.get("/length", data={ | ||
"params": "a" | ||
}) | ||
print(resp.data) | ||
|
||
|
||
if __name__ == "__main__": | ||
example_length_filter() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# !/usr/local/python/bin/python | ||
# -*- coding: utf-8 -*- | ||
# (C) Wu Dong, 2020 | ||
# All rights reserved | ||
# @Author: 'Wu Dong <wudong@eastwu.cn>' | ||
# @Time: '2020-03-19 10:54' | ||
""" 演示 pre-request 框架如何使用手机号校验 | ||
目前使用的正则表达式为: ^0\d{2,3}\d{7,8}$|^1[3578]\d{9}$|^14[579]\d{8}$ | ||
""" | ||
from flask import Flask | ||
from pre_request import filter_params, Rule | ||
|
||
|
||
app = Flask(__name__) | ||
app.config["TESTING"] = True | ||
client = app.test_client() | ||
|
||
|
||
# 指定mobile=True,此时框架会自动判断用户入参是否符合mobile正则 | ||
mobile_params = { | ||
"params": Rule(mobile=True) | ||
} | ||
|
||
|
||
@app.route("/mobile", methods=["GET", "POST"]) | ||
@filter_params(mobile_params) | ||
def example_mobile_handler(params): | ||
return str(params) | ||
|
||
|
||
def example_mobile_filter(): | ||
""" 演示手机号验证 | ||
""" | ||
resp = client.get("/mobile", data={ | ||
"params": "13899991111" | ||
}) | ||
print(resp.data) | ||
|
||
|
||
if __name__ == "__main__": | ||
example_mobile_filter() |
Oops, something went wrong.