From e7298c28c7c59033f50a81a87a36dd8071b43f8c Mon Sep 17 00:00:00 2001 From: OmarThinks Date: Sat, 30 Jan 2021 08:52:00 +0200 Subject: [PATCH 01/17] made it simple to run app, app.run() --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index d580862..9bbb5ff 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,8 @@ def post(): name=request.body_params.name, nickname=request.body_params.nickname, ) + +app.run(debug=True) ``` - `age` query parameter is a required `int` From 5cf736baa3bdcb6111d701900866b211845dff1c Mon Sep 17 00:00:00 2001 From: OmarThinks Date: Sat, 30 Jan 2021 09:00:49 +0200 Subject: [PATCH 02/17] README.md:mod:first endpoint --- README.md | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 9bbb5ff..8093338 100644 --- a/README.md +++ b/README.md @@ -51,36 +51,36 @@ from pydantic import BaseModel app = Flask("flask_pydantic_app") - -class QueryModel(BaseModel): - age: int - - +# Defening your expectations of the rquest body, as a pydantic model class BodyModel(BaseModel): name: str nickname: Optional[str] - +# Defening your expectations of the API response, as a pydantic model class ResponseModel(BaseModel): id: int age: int name: str nickname: Optional[str] - +# First Endpoint, Receive request body @app.route("/", methods=["POST"]) -@validate(body=BodyModel, query=QueryModel) -def post(): +@validate() +def post(body:BodyModel): # save model to DB - id_ = ... + id_ = 1 # we should have got it from db + age_ = 1 # we should have got it from db return ResponseModel( - id=id_, - age=request.query_params.age, - name=request.body_params.name, - nickname=request.body_params.nickname, + id=id_, + age=age_, + name=body.name, + nickname=body.nickname, ) +class QueryModel(BaseModel): + age: int + app.run(debug=True) ``` From a805e0e451a16fd724606f306da4ffa5257a4b07 Mon Sep 17 00:00:00 2001 From: OmarThinks Date: Sat, 30 Jan 2021 09:07:03 +0200 Subject: [PATCH 03/17] README.md:mod:first endpoint now ready --- README.md | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 8093338..7aaeb4c 100644 --- a/README.md +++ b/README.md @@ -67,17 +67,21 @@ class ResponseModel(BaseModel): @app.route("/", methods=["POST"]) @validate() def post(body:BodyModel): - # save model to DB - id_ = 1 # we should have got it from db - age_ = 1 # we should have got it from db - - return ResponseModel( - id=id_, - age=age_, - name=body.name, - nickname=body.nickname, - ) - + name = body.name + nickname = body.nickname + # Now 'name' and 'nickname' are + # received and sanitized from the request body + + # save model to DB + id = 1 # we should have got it from db + age = 1 # we should have got it from db + + return ResponseModel( + id=id, + age=age, + name=name, + nickname=nickname, + ) class QueryModel(BaseModel): age: int From 39aa0aef40069a4dde7303a2bd3a9868731ef69c Mon Sep 17 00:00:00 2001 From: OmarThinks Date: Sat, 30 Jan 2021 09:16:09 +0200 Subject: [PATCH 04/17] README.md:mod:example more clarification --- README.md | 52 +++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 7aaeb4c..91dba75 100644 --- a/README.md +++ b/README.md @@ -44,29 +44,53 @@ Simply use `validate` decorator on route function. ```python from typing import Optional - from flask import Flask, request -from flask_pydantic import validate from pydantic import BaseModel +from flask_pydantic import validate + app = Flask("flask_pydantic_app") -# Defening your expectations of the rquest body, as a pydantic model +# Request body expectation as a pydantic model class BodyModel(BaseModel): - name: str - nickname: Optional[str] + name: str + nickname: Optional[str] -# Defening your expectations of the API response, as a pydantic model +# API response expectation as a pydantic model class ResponseModel(BaseModel): - id: int - age: int - name: str - nickname: Optional[str] - -# First Endpoint, Receive request body + id: int + age: int + name: str + nickname: Optional[str] + +""" +Example 1: +receive inputs from request body +""" @app.route("/", methods=["POST"]) +@validate() # Welcome to flask_pydantic +def post(body:BodyModel): # expected request body + name = body.name + nickname = body.nickname + # Now 'name' and 'nickname' are + # received and, validated and sanitized from the request body + + # save model to DB + id = 1 # we should have got it from db + age = 1 # we should have got it from db + + return ResponseModel( + id=id, age=age, name=name, + nickname=nickname, + ) + +class QueryModel(BaseModel): + age: int + +# First example Endpoint, Receive request body +@app.route("/", methods=["GET"]) @validate() -def post(body:BodyModel): +def get(body:BodyModel): name = body.name nickname = body.nickname # Now 'name' and 'nickname' are @@ -82,8 +106,6 @@ def post(body:BodyModel): name=name, nickname=nickname, ) -class QueryModel(BaseModel): - age: int app.run(debug=True) ``` From d2c3b3eb82577f354586c6092465cf680f9be8cf Mon Sep 17 00:00:00 2001 From: OmarThinks Date: Sat, 30 Jan 2021 09:38:34 +0200 Subject: [PATCH 05/17] README.me:mod:example1:clear --- README.md | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 91dba75..1615b9b 100644 --- a/README.md +++ b/README.md @@ -51,12 +51,12 @@ from flask_pydantic import validate app = Flask("flask_pydantic_app") -# Request body expectation as a pydantic model -class BodyModel(BaseModel): +# Request body expectations as a pydantic model +class RequestBodyModel(BaseModel): name: str nickname: Optional[str] -# API response expectation as a pydantic model +# API response expectations as a pydantic model class ResponseModel(BaseModel): id: int age: int @@ -68,21 +68,19 @@ Example 1: receive inputs from request body """ @app.route("/", methods=["POST"]) -@validate() # Welcome to flask_pydantic -def post(body:BodyModel): # expected request body - name = body.name - nickname = body.nickname +@validate() # To apply flask_pydantic to this endpoint +def post(body:RequestBodyModel): # expected request body + name_ = body.name + nickname_ = body.nickname # Now 'name' and 'nickname' are - # received and, validated and sanitized from the request body + # received, validated and sanitized from the request body # save model to DB - id = 1 # we should have got it from db - age = 1 # we should have got it from db + id_ = 1; age_ = 1 # we should have got them from db return ResponseModel( - id=id, age=age, name=name, - nickname=nickname, - ) + id=id_, age=age_, name=name_, nickname=nickname_ + ) class QueryModel(BaseModel): age: int @@ -90,7 +88,7 @@ class QueryModel(BaseModel): # First example Endpoint, Receive request body @app.route("/", methods=["GET"]) @validate() -def get(body:BodyModel): +def get(body:RequestBodyModel): name = body.name nickname = body.nickname # Now 'name' and 'nickname' are From ff24834f009305a1fedaa62c2a69222ee0fc7b46 Mon Sep 17 00:00:00 2001 From: OmarThinks Date: Sat, 30 Jan 2021 09:46:14 +0200 Subject: [PATCH 06/17] README.md:example 2:ready --- README.md | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 1615b9b..3c67d08 100644 --- a/README.md +++ b/README.md @@ -70,39 +70,41 @@ receive inputs from request body @app.route("/", methods=["POST"]) @validate() # To apply flask_pydantic to this endpoint def post(body:RequestBodyModel): # expected request body - name_ = body.name - nickname_ = body.nickname + name = body.name + nickname = body.nickname # Now 'name' and 'nickname' are # received, validated and sanitized from the request body # save model to DB - id_ = 1; age_ = 1 # we should have got them from db + id = 1 + age = 2 # we should have got them from db return ResponseModel( - id=id_, age=age_, name=name_, nickname=nickname_ + id=id, age=age, name=name, nickname=nickname ) + +# query_paramaters expectations as a pydantic model class QueryModel(BaseModel): age: int -# First example Endpoint, Receive request body +""" +Example 2: +receive inputs from query paramaters +""" @app.route("/", methods=["GET"]) @validate() -def get(body:RequestBodyModel): - name = body.name - nickname = body.nickname - # Now 'name' and 'nickname' are - # received and sanitized from the request body +def get(query:QueryModel): + age = query.age + # Now 'age' is + # received and sanitized from the query paramters # save model to DB id = 1 # we should have got it from db - age = 1 # we should have got it from db - + name = "abc" + nickname = "123" return ResponseModel( - id=id, - age=age, - name=name, - nickname=nickname, + id=id, age=age, name=name, nickname=nickname, ) app.run(debug=True) From b270923c7b86b1760978c936e37f59883111ca49 Mon Sep 17 00:00:00 2001 From: OmarThinks Date: Sat, 30 Jan 2021 09:55:16 +0200 Subject: [PATCH 07/17] README.md:add:example 3:ready --- README.md | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 3c67d08..48a4b38 100644 --- a/README.md +++ b/README.md @@ -76,8 +76,8 @@ def post(body:RequestBodyModel): # expected request body # received, validated and sanitized from the request body # save model to DB - id = 1 - age = 2 # we should have got them from db + id = 0 + age = 1000 # we should have got them from db return ResponseModel( id=id, age=age, name=name, nickname=nickname @@ -100,9 +100,33 @@ def get(query:QueryModel): # received and sanitized from the query paramters # save model to DB - id = 1 # we should have got it from db + id = 0 # we should have got it from db name = "abc" nickname = "123" + return ResponseModel( + id=id, age=age, name=name, nickname=nickname + ) + +""" +Example 3: +receive inputs from BOTH: + 1) request_body + 2) query paramaters +In the same request +""" +@app.route("/both", methods=["POST"]) +@validate() +def get_and_post(body:RequestBodyModel,query:QueryModel): + # From request body + name = body.name + nickname = body.nickname + + # from query parameters + age = query.age + + # save model to DB + id = 0 # we should have got it from db + return ResponseModel( id=id, age=age, name=name, nickname=nickname, ) From f79037636fc957e3f071a8fdacd4a9175cc489cf Mon Sep 17 00:00:00 2001 From: OmarThinks Date: Sat, 30 Jan 2021 10:07:22 +0200 Subject: [PATCH 08/17] full example now ready --- README.md | 39 +++++++++++++-------------------------- 1 file changed, 13 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 48a4b38..46fcb2d 100644 --- a/README.md +++ b/README.md @@ -74,16 +74,12 @@ def post(body:RequestBodyModel): # expected request body nickname = body.nickname # Now 'name' and 'nickname' are # received, validated and sanitized from the request body - - # save model to DB - id = 0 - age = 1000 # we should have got them from db - + # save model to DB, and get 'id' and 'age' return ResponseModel( - id=id, age=age, name=name, nickname=nickname + name=name, nickname=nickname, + id=0, age=1000 ) - # query_paramaters expectations as a pydantic model class QueryModel(BaseModel): age: int @@ -96,15 +92,11 @@ receive inputs from query paramaters @validate() def get(query:QueryModel): age = query.age - # Now 'age' is - # received and sanitized from the query paramters - - # save model to DB - id = 0 # we should have got it from db - name = "abc" - nickname = "123" + # Now 'age' is received and sanitized from the query paramters + # save model to DB, and get 'id','name' and 'nickname' return ResponseModel( - id=id, age=age, name=name, nickname=nickname + age=age, + id=0, name="abc", nickname="123" ) """ @@ -117,18 +109,13 @@ In the same request @app.route("/both", methods=["POST"]) @validate() def get_and_post(body:RequestBodyModel,query:QueryModel): - # From request body - name = body.name - nickname = body.nickname - - # from query parameters - age = query.age - - # save model to DB - id = 0 # we should have got it from db - + name = body.name # From request body + nickname = body.nickname # From request body + age = query.age # from query parameters + # save model to db, and get 'id' return ResponseModel( - id=id, age=age, name=name, nickname=nickname, + age=age, name=name, nickname=nickname, + id=0 ) app.run(debug=True) From 697131edb336a8e1688702b17fbabac29a44e298 Mon Sep 17 00:00:00 2001 From: OmarThinks Date: Sat, 30 Jan 2021 10:19:00 +0200 Subject: [PATCH 09/17] removed comments --- README.md | 31 +++---------------------------- 1 file changed, 3 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 46fcb2d..a63ed50 100644 --- a/README.md +++ b/README.md @@ -51,68 +51,43 @@ from flask_pydantic import validate app = Flask("flask_pydantic_app") -# Request body expectations as a pydantic model class RequestBodyModel(BaseModel): name: str nickname: Optional[str] -# API response expectations as a pydantic model class ResponseModel(BaseModel): id: int age: int name: str nickname: Optional[str] -""" -Example 1: -receive inputs from request body -""" @app.route("/", methods=["POST"]) -@validate() # To apply flask_pydantic to this endpoint -def post(body:RequestBodyModel): # expected request body +@validate() +def post(body:RequestBodyModel): name = body.name nickname = body.nickname - # Now 'name' and 'nickname' are - # received, validated and sanitized from the request body - # save model to DB, and get 'id' and 'age' return ResponseModel( - name=name, nickname=nickname, - id=0, age=1000 + name=name, nickname=nickname,id=0, age=1000 ) -# query_paramaters expectations as a pydantic model class QueryModel(BaseModel): age: int -""" -Example 2: -receive inputs from query paramaters -""" @app.route("/", methods=["GET"]) @validate() def get(query:QueryModel): age = query.age - # Now 'age' is received and sanitized from the query paramters - # save model to DB, and get 'id','name' and 'nickname' return ResponseModel( age=age, id=0, name="abc", nickname="123" ) -""" -Example 3: -receive inputs from BOTH: - 1) request_body - 2) query paramaters -In the same request -""" @app.route("/both", methods=["POST"]) @validate() def get_and_post(body:RequestBodyModel,query:QueryModel): name = body.name # From request body nickname = body.nickname # From request body age = query.age # from query parameters - # save model to db, and get 'id' return ResponseModel( age=age, name=name, nickname=nickname, id=0 From cf6bdc0388e3754d46f858c4309577f596b88a7e Mon Sep 17 00:00:00 2001 From: OmarThinks Date: Sat, 30 Jan 2021 10:23:19 +0200 Subject: [PATCH 10/17] README.md:add:curl --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a63ed50..600bf7a 100644 --- a/README.md +++ b/README.md @@ -97,6 +97,7 @@ app.run(debug=True) ``` - `age` query parameter is a required `int` + - `curl --location --request GET 'http://127.0.0.1:5000/'` - if none is provided the response contains: ```json { @@ -112,6 +113,7 @@ app.run(debug=True) } ``` - for incompatible type (e. g. string `/?age=not_a_number`) + - `curl --location --request GET 'http://127.0.0.1:5000/?age=abc'` ```json { "validation_error": { @@ -127,9 +129,9 @@ app.run(debug=True) ``` - likewise for body parameters - example call with valid parameters: - `curl -XPOST http://localhost:5000/?age=20 --data '{"name": "John Doe"}' -H 'Content-Type: application/json'` + `curl --location --request GET 'http://127.0.0.1:5000/?age=20'` --> `{"id": 2, "age": 20, "name": "John Doe", "nickname": null}` +-> `{"id": 0, "age": 20, "name": "abc", "nickname": "123"}` ### Modify response status code From 5afece7bd9afb6d961df8e99c24a4db15691f8f9 Mon Sep 17 00:00:00 2001 From: OmarThinks Date: Sat, 30 Jan 2021 10:31:13 +0200 Subject: [PATCH 11/17] README.md:add:rearranged examples --- README.md | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 600bf7a..4114788 100644 --- a/README.md +++ b/README.md @@ -55,12 +55,26 @@ class RequestBodyModel(BaseModel): name: str nickname: Optional[str] +class QueryModel(BaseModel): + age: int + +# Example 1: query parameters only +@app.route("/", methods=["GET"]) +@validate() +def get(query:QueryModel): + age = query.age + return ResponseModel( + age=age, + id=0, name="abc", nickname="123" + ) + class ResponseModel(BaseModel): id: int age: int name: str nickname: Optional[str] +# Example2: request body only @app.route("/", methods=["POST"]) @validate() def post(body:RequestBodyModel): @@ -70,18 +84,7 @@ def post(body:RequestBodyModel): name=name, nickname=nickname,id=0, age=1000 ) -class QueryModel(BaseModel): - age: int - -@app.route("/", methods=["GET"]) -@validate() -def get(query:QueryModel): - age = query.age - return ResponseModel( - age=age, - id=0, name="abc", nickname="123" - ) - +# Example 3: both query paramters and request body @app.route("/both", methods=["POST"]) @validate() def get_and_post(body:RequestBodyModel,query:QueryModel): @@ -129,7 +132,7 @@ app.run(debug=True) ``` - likewise for body parameters - example call with valid parameters: - `curl --location --request GET 'http://127.0.0.1:5000/?age=20'` + `curl --location --request GET 'http://127.0.0.1:5000/?age=20'` -> `{"id": 0, "age": 20, "name": "abc", "nickname": "123"}` From 77ea712529567828530f615878dd8db6fcc407e6 Mon Sep 17 00:00:00 2001 From: OmarThinks Date: Sat, 30 Jan 2021 10:34:38 +0200 Subject: [PATCH 12/17] README.md:add:example 2 and 3 --- README.md | 73 ++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 45 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 4114788..1f8f585 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ For more details see in-code docstring or example app. ## Usage -### Basic example +### Example 1: Query parameters only Simply use `validate` decorator on route function. @@ -68,33 +68,7 @@ def get(query:QueryModel): id=0, name="abc", nickname="123" ) -class ResponseModel(BaseModel): - id: int - age: int - name: str - nickname: Optional[str] - -# Example2: request body only -@app.route("/", methods=["POST"]) -@validate() -def post(body:RequestBodyModel): - name = body.name - nickname = body.nickname - return ResponseModel( - name=name, nickname=nickname,id=0, age=1000 - ) - -# Example 3: both query paramters and request body -@app.route("/both", methods=["POST"]) -@validate() -def get_and_post(body:RequestBodyModel,query:QueryModel): - name = body.name # From request body - nickname = body.nickname # From request body - age = query.age # from query parameters - return ResponseModel( - age=age, name=name, nickname=nickname, - id=0 - ) +# Example 2 and 3 should be here app.run(debug=True) ``` @@ -136,6 +110,49 @@ app.run(debug=True) -> `{"id": 0, "age": 20, "name": "abc", "nickname": "123"}` + + +### Example 2: Request body only + +```python +class ResponseModel(BaseModel): + id: int + age: int + name: str + nickname: Optional[str] + +# Example2: request body only +@app.route("/", methods=["POST"]) +@validate() +def post(body:RequestBodyModel): + name = body.name + nickname = body.nickname + return ResponseModel( + name=name, nickname=nickname,id=0, age=1000 + ) +``` + +### Example 3: BOTH query paramaters and request body + +```python +# Example 3: both query paramters and request body +@app.route("/both", methods=["POST"]) +@validate() +def get_and_post(body:RequestBodyModel,query:QueryModel): + name = body.name # From request body + nickname = body.nickname # From request body + age = query.age # from query parameters + return ResponseModel( + age=age, name=name, nickname=nickname, + id=0 + ) +``` + + + + + + ### Modify response status code The default success status code is `200`. It can be modified in two ways From 0c67f05835ba1825b9cb386b9801bd4d12634f60 Mon Sep 17 00:00:00 2001 From: OmarThinks Date: Sat, 30 Jan 2021 10:42:56 +0200 Subject: [PATCH 13/17] README.md:add:rearranged examples --- README.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 1f8f585..ec9381f 100644 --- a/README.md +++ b/README.md @@ -51,13 +51,15 @@ from flask_pydantic import validate app = Flask("flask_pydantic_app") -class RequestBodyModel(BaseModel): - name: str - nickname: Optional[str] - class QueryModel(BaseModel): age: int +class ResponseModel(BaseModel): + id: int + age: int + name: str + nickname: Optional[str] + # Example 1: query parameters only @app.route("/", methods=["GET"]) @validate() @@ -115,9 +117,7 @@ app.run(debug=True) ### Example 2: Request body only ```python -class ResponseModel(BaseModel): - id: int - age: int +class RequestBodyModel(BaseModel): name: str nickname: Optional[str] From 5d8c3d60c66cf25441ddb16570813f7f78c6c6bc Mon Sep 17 00:00:00 2001 From: OmarThinks Date: Sat, 30 Jan 2021 10:58:40 +0200 Subject: [PATCH 14/17] readme.py:created --- example_app/readme.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 example_app/readme.py diff --git a/example_app/readme.py b/example_app/readme.py new file mode 100644 index 0000000..e69de29 From 9c9c61a6a3a4daa3567faa59e2a3542412f03e21 Mon Sep 17 00:00:00 2001 From: OmarThinks Date: Sat, 30 Jan 2021 11:00:39 +0200 Subject: [PATCH 15/17] readme.py:filled --- example_app/readme.py | 52 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/example_app/readme.py b/example_app/readme.py index e69de29..19ed069 100644 --- a/example_app/readme.py +++ b/example_app/readme.py @@ -0,0 +1,52 @@ +from typing import Optional +from flask import Flask, request +from pydantic import BaseModel + +from flask_pydantic import validate + +app = Flask("flask_pydantic_app") + +class RequestBodyModel(BaseModel): + name: str + nickname: Optional[str] + + +class QueryModel(BaseModel): + age: int + +@app.route("/", methods=["GET"]) +@validate() +def get(query:QueryModel): + age = query.age + return ResponseModel( + age=age, + id=0, name="abc", nickname="123" + ) + +class ResponseModel(BaseModel): + id: int + age: int + name: str + nickname: Optional[str] + +@app.route("/", methods=["POST"]) +@validate() +def post(body:RequestBodyModel): + name = body.name + nickname = body.nickname + return ResponseModel( + name=name, nickname=nickname,id=0, age=1000 + ) + +@app.route("/both", methods=["POST"]) +@validate() +def get_and_post(body:RequestBodyModel,query:QueryModel): + name = body.name # From request body + nickname = body.nickname # From request body + age = query.age # from query parameters + return ResponseModel( + age=age, name=name, nickname=nickname, + id=0 + ) + +app.run(debug=True) \ No newline at end of file From ae7c42bd3b0778f85238af20a3f4d6db2a0e2f0f Mon Sep 17 00:00:00 2001 From: OmarThinks Date: Sat, 30 Jan 2021 11:05:45 +0200 Subject: [PATCH 16/17] readme.py:add:curl --- example_app/readme.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/example_app/readme.py b/example_app/readme.py index 19ed069..f8f14cf 100644 --- a/example_app/readme.py +++ b/example_app/readme.py @@ -22,6 +22,15 @@ def get(query:QueryModel): age=age, id=0, name="abc", nickname="123" ) +""" +curl --location --request GET 'http://127.0.0.1:5000/' +curl --location --request GET 'http://127.0.0.1:5000/?ageeee=5' +curl --location --request GET 'http://127.0.0.1:5000/?age=abc' + +curl --location --request GET 'http://127.0.0.1:5000/?age=5' +""" + + class ResponseModel(BaseModel): id: int @@ -38,6 +47,22 @@ def post(body:RequestBodyModel): name=name, nickname=nickname,id=0, age=1000 ) +""" +curl --location --request POST 'http://127.0.0.1:5000/' + +curl --location --request POST 'http://127.0.0.1:5000/' \ +--header 'Content-Type: application/json' \ +--data-raw '{' + +curl --location --request POST 'http://127.0.0.1:5000/' \ +--header 'Content-Type: application/json' \ +--data-raw '{"nameee":123}' + +curl --location --request POST 'http://127.0.0.1:5000/' \ +--header 'Content-Type: application/json' \ +--data-raw '{"name":123}' +""" + @app.route("/both", methods=["POST"]) @validate() def get_and_post(body:RequestBodyModel,query:QueryModel): @@ -49,4 +74,13 @@ def get_and_post(body:RequestBodyModel,query:QueryModel): id=0 ) +""" +curl --location --request POST 'http://127.0.0.1:5000/both' \ +--header 'Content-Type: application/json' \ +--data-raw '{"name":123}' + +curl --location --request POST 'http://127.0.0.1:5000/both?age=40' \ +--header 'Content-Type: application/json' \ +--data-raw '{"name":123}' +""" app.run(debug=True) \ No newline at end of file From bdc5dd67f5efc42b7e5a8c96dc49c3efd59f5376 Mon Sep 17 00:00:00 2001 From: OmarThinks Date: Sat, 30 Jan 2021 11:22:15 +0200 Subject: [PATCH 17/17] created example app --- README.md | 13 ++++++++++++- example_app/{readme.py => example.py} | 0 2 files changed, 12 insertions(+), 1 deletion(-) rename example_app/{readme.py => example.py} (100%) diff --git a/README.md b/README.md index ec9381f..9ad6025 100644 --- a/README.md +++ b/README.md @@ -75,6 +75,11 @@ def get(query:QueryModel): app.run(debug=True) ``` + + This is the full example + + + - `age` query parameter is a required `int` - `curl --location --request GET 'http://127.0.0.1:5000/'` - if none is provided the response contains: @@ -132,6 +137,10 @@ def post(body:RequestBodyModel): ) ``` + + This is the full example + + ### Example 3: BOTH query paramaters and request body ```python @@ -148,7 +157,9 @@ def get_and_post(body:RequestBodyModel,query:QueryModel): ) ``` - + + This is the full example + diff --git a/example_app/readme.py b/example_app/example.py similarity index 100% rename from example_app/readme.py rename to example_app/example.py