def application(env: Dict[str, Any], start_response: Callable[[str, Any], Any]) -> Tuple[bytes]: try: ctx = _create_context(env) if 'application/json' not in ctx.get_header('Accept'): raise errors.HttpNotAcceptable( 'ValidationError', 'This API only supports JSON responses.') for url, allowed_methods in routes.routes.items(): match = re.fullmatch(url, ctx.url) if match: if ctx.method not in allowed_methods: raise errors.HttpMethodNotAllowed( 'ValidationError', 'Allowed methods: %r' % allowed_methods) handler = allowed_methods[ctx.method] break else: raise errors.HttpNotFound( 'ValidationError', 'Requested path ' + ctx.url + ' was not found.') try: ctx.session = db.session() try: for hook in middleware.pre_hooks: hook(ctx) try: response = handler(ctx, match.groupdict()) except Exception: ctx.session.rollback() raise finally: for hook in middleware.post_hooks: hook(ctx) finally: db.session.remove() start_response('200', [('content-type', 'application/json')]) return (_dump_json(response).encode('utf-8'), ) except Exception as ex: for exception_type, ex_handler in errors.error_handlers.items(): if isinstance(ex, exception_type): ex_handler(ex) raise except errors.BaseHttpError as ex: start_response('%d %s' % (ex.code, ex.reason), [('content-type', 'application/json')]) blob = { 'name': ex.name, 'title': ex.title, 'description': ex.description, } if ex.extra_fields is not None: for key, value in ex.extra_fields.items(): blob[key] = value return (_dump_json(blob).encode('utf-8'), )
def application(env, start_response): try: ctx = _create_context(env) if 'application/json' not in ctx.get_header('Accept'): raise errors.HttpNotAcceptable( 'ValidationError', 'This API only supports JSON responses.') for url, allowed_methods in routes.routes.items(): match = re.fullmatch(url, ctx.url) if match: if ctx.method not in allowed_methods: raise errors.HttpMethodNotAllowed( 'ValidationError', 'Allowed methods: %r' % allowed_methods) handler = allowed_methods[ctx.method] break else: raise errors.HttpNotFound( 'ValidationError', 'Requested path ' + ctx.url + ' was not found.') try: ctx.session = db.session() try: for hook in middleware.pre_hooks: hook(ctx) try: response = handler(ctx, match.groupdict()) finally: for hook in middleware.post_hooks: hook(ctx) finally: db.session.remove() start_response('200', [('content-type', 'application/json')]) return (_dump_json(response).encode('utf-8'),) except Exception as ex: for exception_type, handler in errors.error_handlers.items(): if isinstance(ex, exception_type): handler(ex) raise except errors.BaseHttpError as ex: start_response( '%d %s' % (ex.code, ex.reason), [('content-type', 'application/json')]) blob = { 'name': ex.name, 'title': ex.title, 'description': ex.description, } if ex.extra_fields is not None: for key, value in ex.extra_fields.items(): blob[key] = value return (_dump_json(blob).encode('utf-8'),)
def process_request(self, request, _response): request.context.session = db.session() db.reset_query_count()
def application(env: Dict[str, Any], start_response: Callable[[str, Any], Any]) -> Tuple[bytes]: try: ctx = _create_context(env) if "application/json" not in ctx.get_header("Accept"): raise errors.HttpNotAcceptable( "ValidationError", "This API only supports JSON responses.") for url, allowed_methods in routes.routes.items(): match = re.fullmatch(url, ctx.url) if match: if ctx.method not in allowed_methods: raise errors.HttpMethodNotAllowed( "ValidationError", "Allowed methods: %r" % allowed_methods, ) handler = allowed_methods[ctx.method] break else: raise errors.HttpNotFound( "ValidationError", "Requested path " + ctx.url + " was not found.", ) try: ctx.session = db.session() try: for hook in middleware.pre_hooks: hook(ctx) try: response = handler(ctx, match.groupdict()) except Exception: ctx.session.rollback() raise finally: for hook in middleware.post_hooks: hook(ctx) finally: db.session.remove() start_response("200", [("content-type", "application/json")]) return (_dump_json(response).encode("utf-8"), ) except Exception as ex: for exception_type, ex_handler in errors.error_handlers.items(): if isinstance(ex, exception_type): ex_handler(ex) raise except errors.BaseHttpError as ex: start_response( "%d %s" % (ex.code, ex.reason), [("content-type", "application/json")], ) blob = { "name": ex.name, "title": ex.title, "description": ex.description, } if ex.extra_fields is not None: for key, value in ex.extra_fields.items(): blob[key] = value return (_dump_json(blob).encode("utf-8"), )
def process_request(self, request, _response): request.context.session = db.session()