From 613b23748d345f4c5f4f45096625765906100311 Mon Sep 17 00:00:00 2001 From: jacob Date: Wed, 2 Jan 2019 14:52:25 +0800 Subject: [PATCH 1/3] Add example of amending request object --- docs/sanic/examples.rst | 8 ++++++++ examples/amending_request_object.py | 30 +++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 examples/amending_request_object.py diff --git a/docs/sanic/examples.rst b/docs/sanic/examples.rst index d6e99d1f94..e2a7af1462 100644 --- a/docs/sanic/examples.rst +++ b/docs/sanic/examples.rst @@ -156,4 +156,12 @@ execution support provided by the ``pytest-xdist`` plugin. .. literalinclude:: ../../examples/pytest_xdist.py + +Amending Request Object +~~~~~~~~~~~~~~~~~~~~~~~ + +The ``request`` object in ``Sanic`` is a kind of ``dict`` object, this means that ``reqeust`` object can be manipulated as a regular ``dict`` object. + +.. literalinclude:: ../../examples/amending_request_object.py + For more examples and useful samples please visit the `Huge-Sanic's GitHub Page `_ diff --git a/examples/amending_request_object.py b/examples/amending_request_object.py new file mode 100644 index 0000000000..55d889f79e --- /dev/null +++ b/examples/amending_request_object.py @@ -0,0 +1,30 @@ +from sanic import Sanic +from sanic.response import text +from random import randint + +app = Sanic() + + +@app.middleware('request') +def append_request(request): + # Add new key with random value + request['num'] = randint(0, 100) + + +@app.get('/pop') +def pop_handler(request): + # Pop key from request object + num = request.pop('num') + return text(num) + + +@app.get('/key_exist') +def key_exist_handler(request): + # Check the key is exist or not + if 'num' in request: + return text('num exist in request') + + return text('num does not exist in reqeust') + + +app.run(host="0.0.0.0", port=8000, debug=True) From ec5b790b51714e22454ae642bf9e29414bd68b63 Mon Sep 17 00:00:00 2001 From: jacob Date: Wed, 2 Jan 2019 17:29:01 +0800 Subject: [PATCH 2/3] Extend example of modifying the request in middleware document --- docs/sanic/extensions.md | 2 +- docs/sanic/middleware.md | 15 +++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/docs/sanic/extensions.md b/docs/sanic/extensions.md index 7a5a7b915d..d365306916 100644 --- a/docs/sanic/extensions.md +++ b/docs/sanic/extensions.md @@ -41,7 +41,7 @@ A list of Sanic extensions created by the community. ## Project Creation Template -- [cookiecutter-sanic](https://github.com/harshanarayana/cookiecutter-sanic) Get your sanic application up and running in a matter of second in a well defined project structure. +- [cookiecutter-sanic](https://github.com/harshanarayana/cookiecutter-sanic): Get your sanic application up and running in a matter of second in a well defined project structure. Batteries included for deployment, unit testing, automated release management and changelog generation. ## Templating diff --git a/docs/sanic/middleware.md b/docs/sanic/middleware.md index 823d3916ce..14880d7ac5 100644 --- a/docs/sanic/middleware.md +++ b/docs/sanic/middleware.md @@ -36,20 +36,31 @@ this. ``` app = Sanic(__name__) + +@app.middleware('request') +async def add_key(request): + # Add a key to request object like dict object + request['foo'] = 'bar' + + @app.middleware('response') async def custom_banner(request, response): response.headers["Server"] = "Fake-Server" + @app.middleware('response') async def prevent_xss(request, response): response.headers["x-xss-protection"] = "1; mode=block" + app.run(host="0.0.0.0", port=8000) ``` -The above code will apply the two middleware in order. First, the middleware +The above code will apply the three middleware in order. The first middleware +**add_key** will add a new key `foo` into `request` object. This worked due to +`request` object can be manipulated like `dict` object. Then, the second middleware **custom_banner** will change the HTTP response header *Server* to -*Fake-Server*, and the second middleware **prevent_xss** will add the HTTP +*Fake-Server*, and the last middleware **prevent_xss** will add the HTTP header for preventing Cross-Site-Scripting (XSS) attacks. These two functions are invoked *after* a user function returns a response. From f0ada573bbd6d2c256a0856ade42d174bdfb47ee Mon Sep 17 00:00:00 2001 From: jacob Date: Wed, 2 Jan 2019 20:37:26 +0800 Subject: [PATCH 3/3] Fix a grammar error --- docs/sanic/middleware.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/sanic/middleware.md b/docs/sanic/middleware.md index 14880d7ac5..820e3d85c5 100644 --- a/docs/sanic/middleware.md +++ b/docs/sanic/middleware.md @@ -57,7 +57,7 @@ app.run(host="0.0.0.0", port=8000) ``` The above code will apply the three middleware in order. The first middleware -**add_key** will add a new key `foo` into `request` object. This worked due to +**add_key** will add a new key `foo` into `request` object. This worked because `request` object can be manipulated like `dict` object. Then, the second middleware **custom_banner** will change the HTTP response header *Server* to *Fake-Server*, and the last middleware **prevent_xss** will add the HTTP