diff options
author | Aric Coady <aric.coady@gmail.com> | 2016-06-05 14:12:55 -0700 |
---|---|---|
committer | Aric Coady <aric.coady@gmail.com> | 2016-07-17 11:05:45 -0700 |
commit | c060b46b0512b638bd32805bcae9f44af340bfba (patch) | |
tree | 7caf50f5ef6c585a9560406653e146d0b4592536 /cherrypy/lib/cptools.py | |
parent | adb7ff5aa1f48506e2838a22176c43c6f3aa4fb5 (diff) | |
download | cherrypy-git-c060b46b0512b638bd32805bcae9f44af340bfba.tar.gz |
Convert request params based on function annotations. Fixes #1441.
Diffstat (limited to 'cherrypy/lib/cptools.py')
-rw-r--r-- | cherrypy/lib/cptools.py | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/cherrypy/lib/cptools.py b/cherrypy/lib/cptools.py index fbed0e23..073216e0 100644 --- a/cherrypy/lib/cptools.py +++ b/cherrypy/lib/cptools.py @@ -630,3 +630,21 @@ def autovary(ignore=None, debug=False): v.sort() resp_h['Vary'] = ', '.join(v) request.hooks.attach('before_finalize', set_response_header, 95) + + +def convert_params(exception=ValueError, error=400): + """Convert request params based on function annotations, with error handling. + + exception + Exception class to catch. + + status + The HTTP error code to return to the client on failure. + """ + request = cherrypy.serving.request + types = request.handler.callable.__annotations__ + try: + for key in set(types).intersection(request.params): + request.params[key] = types[key](request.params[key]) + except exception as exc: + raise cherrypy.HTTPError(error, str(exc)) |