author | Tero Marttila <terom@paivola.fi> |
Sat, 31 Jan 2015 18:49:51 +0200 | |
changeset 114 | 0f5e58ffe624 |
parent 107 | 05707929ff6f |
permissions | -rw-r--r-- |
106 | 1 |
import asyncio |
2 |
import qmsk.web.application |
|
3 |
import werkzeug |
|
107
05707929ff6f
qmsk.web.async: handle HTTPExceptions
Tero Marttila <terom@paivola.fi>
parents:
106
diff
changeset
|
4 |
import werkzeug.exceptions |
106 | 5 |
|
6 |
class Handler (qmsk.web.application.Handler) : |
|
7 |
@asyncio.coroutine |
|
8 |
def process_async (self, **params) : |
|
9 |
""" |
|
10 |
Process request asynchronously. |
|
11 |
||
12 |
May optionally return a Response, to e.g. redirect after POST. |
|
13 |
""" |
|
14 |
||
15 |
pass |
|
16 |
||
17 |
@asyncio.coroutine |
|
18 |
def respond_async (self, **params): |
|
19 |
response = yield from self.process_async(**params) |
|
20 |
||
21 |
if response : |
|
22 |
return response |
|
23 |
||
24 |
return self.respond(**params) |
|
25 |
||
26 |
class Application (qmsk.web.application.Application) : |
|
27 |
@asyncio.coroutine |
|
28 |
def __call__ (self, environ, start_response): |
|
29 |
request = werkzeug.Request(environ) |
|
107
05707929ff6f
qmsk.web.async: handle HTTPExceptions
Tero Marttila <terom@paivola.fi>
parents:
106
diff
changeset
|
30 |
|
05707929ff6f
qmsk.web.async: handle HTTPExceptions
Tero Marttila <terom@paivola.fi>
parents:
106
diff
changeset
|
31 |
handler = None |
106 | 32 |
|
33 |
try: |
|
107
05707929ff6f
qmsk.web.async: handle HTTPExceptions
Tero Marttila <terom@paivola.fi>
parents:
106
diff
changeset
|
34 |
handler, params = self.lookup(request) |
05707929ff6f
qmsk.web.async: handle HTTPExceptions
Tero Marttila <terom@paivola.fi>
parents:
106
diff
changeset
|
35 |
|
106 | 36 |
handler.init() |
37 |
||
38 |
response = yield from handler.respond_async(**params) |
|
107
05707929ff6f
qmsk.web.async: handle HTTPExceptions
Tero Marttila <terom@paivola.fi>
parents:
106
diff
changeset
|
39 |
|
05707929ff6f
qmsk.web.async: handle HTTPExceptions
Tero Marttila <terom@paivola.fi>
parents:
106
diff
changeset
|
40 |
except werkzeug.exceptions.HTTPException as error : |
05707929ff6f
qmsk.web.async: handle HTTPExceptions
Tero Marttila <terom@paivola.fi>
parents:
106
diff
changeset
|
41 |
response = error |
106 | 42 |
|
43 |
finally: |
|
107
05707929ff6f
qmsk.web.async: handle HTTPExceptions
Tero Marttila <terom@paivola.fi>
parents:
106
diff
changeset
|
44 |
if handler: |
05707929ff6f
qmsk.web.async: handle HTTPExceptions
Tero Marttila <terom@paivola.fi>
parents:
106
diff
changeset
|
45 |
handler.cleanup() |
106 | 46 |
|
47 |
return response(environ, start_response) |