|
| 1 | +title: flask.app Headers code examples |
| 2 | +category: page |
| 3 | +slug: flask-app-headers-examples |
| 4 | +sortorder: 500021001 |
| 5 | +toc: False |
| 6 | +sidebartitle: flask.app Headers |
| 7 | +meta: Python example code for the Headers class from the flask.app module of the Flask project. |
| 8 | + |
| 9 | + |
| 10 | +Headers is a class within the flask.app module of the Flask project. |
| 11 | + |
| 12 | + |
| 13 | +## Example 1 from flask-restx |
| 14 | +[Flask RESTX](https://github.com/python-restx/flask-restx) is an |
| 15 | +extension that makes it easier to build |
| 16 | +[RESTful APIs](/application-programming-interfaces.html) into |
| 17 | +your applications. Flask RESTX aims for minimal configuration to |
| 18 | +get basic APIs running for existing applications and it exposes |
| 19 | +endpoint documentation using [Swagger](https://swagger.io/). |
| 20 | + |
| 21 | +Flask RESTX is provided as open source under the |
| 22 | +[BSD 3-Clause license](https://github.com/python-restx/flask-restx/blob/master/LICENSE). |
| 23 | + |
| 24 | +[**flask-restx / flask_restx / api.py**](https://github.com/python-restx/flask-restx/blob/master/flask_restx/./api.py) |
| 25 | + |
| 26 | +```python |
| 27 | +# api.py |
| 28 | +from __future__ import unicode_literals |
| 29 | + |
| 30 | +import difflib |
| 31 | +import inspect |
| 32 | +from itertools import chain |
| 33 | +import logging |
| 34 | +import operator |
| 35 | +import re |
| 36 | +import six |
| 37 | +import sys |
| 38 | + |
| 39 | +from collections import OrderedDict |
| 40 | +from functools import wraps, partial |
| 41 | +from types import MethodType |
| 42 | + |
| 43 | +from flask import url_for, request, current_app |
| 44 | +from flask import make_response as original_flask_make_response |
| 45 | +from flask.helpers import _endpoint_from_view_func |
| 46 | +from flask.signals import got_request_exception |
| 47 | + |
| 48 | +from jsonschema import RefResolver |
| 49 | + |
| 50 | +from werkzeug.utils import cached_property |
| 51 | +~~from werkzeug.datastructures import Headers |
| 52 | +from werkzeug.exceptions import ( |
| 53 | + HTTPException, |
| 54 | + MethodNotAllowed, |
| 55 | + NotFound, |
| 56 | + NotAcceptable, |
| 57 | + InternalServerError, |
| 58 | +) |
| 59 | +from werkzeug.wrappers import BaseResponse |
| 60 | + |
| 61 | +from . import apidoc |
| 62 | +from .mask import ParseError, MaskError |
| 63 | +from .namespace import Namespace |
| 64 | +from .postman import PostmanCollectionV1 |
| 65 | +from .resource import Resource |
| 66 | +from .swagger import Swagger |
| 67 | +from .utils import default_id, camel_to_dash, unpack |
| 68 | +from .representations import output_json |
| 69 | +from ._http import HTTPStatus |
| 70 | + |
| 71 | +RE_RULES = re.compile("(<.*>)") |
| 72 | + |
| 73 | +HEADERS_BLACKLIST = ("Content-Length",) |
| 74 | + |
| 75 | +DEFAULT_REPRESENTATIONS = [("application/json", output_json)] |
| 76 | + |
| 77 | + |
| 78 | +## ... source file abbreviated to get to Headers examples ... |
| 79 | + |
| 80 | + |
| 81 | + return self.handle_error(e) |
| 82 | + except Exception as f: |
| 83 | + return original_handler(f) |
| 84 | + return original_handler(e) |
| 85 | + |
| 86 | + def handle_error(self, e): |
| 87 | + got_request_exception.send(current_app._get_current_object(), exception=e) |
| 88 | + |
| 89 | + if ( |
| 90 | + not isinstance(e, HTTPException) |
| 91 | + and current_app.propagate_exceptions |
| 92 | + and not isinstance(e, tuple(self.error_handlers.keys())) |
| 93 | + ): |
| 94 | + |
| 95 | + exc_type, exc_value, tb = sys.exc_info() |
| 96 | + if exc_value is e: |
| 97 | + raise |
| 98 | + else: |
| 99 | + raise e |
| 100 | + |
| 101 | + include_message_in_response = current_app.config.get( |
| 102 | + "ERROR_INCLUDE_MESSAGE", True |
| 103 | + ) |
| 104 | + default_data = {} |
| 105 | + |
| 106 | +~~ headers = Headers() |
| 107 | + |
| 108 | + for typecheck, handler in six.iteritems(self._own_and_child_error_handlers): |
| 109 | + if isinstance(e, typecheck): |
| 110 | + result = handler(e) |
| 111 | + default_data, code, headers = unpack( |
| 112 | + result, HTTPStatus.INTERNAL_SERVER_ERROR |
| 113 | + ) |
| 114 | + break |
| 115 | + else: |
| 116 | + if isinstance(e, HTTPException): |
| 117 | + code = HTTPStatus(e.code) |
| 118 | + if include_message_in_response: |
| 119 | + default_data = {"message": getattr(e, "description", code.phrase)} |
| 120 | + headers = e.get_response().headers |
| 121 | + elif self._default_error_handler: |
| 122 | + result = self._default_error_handler(e) |
| 123 | + default_data, code, headers = unpack( |
| 124 | + result, HTTPStatus.INTERNAL_SERVER_ERROR |
| 125 | + ) |
| 126 | + else: |
| 127 | + code = HTTPStatus.INTERNAL_SERVER_ERROR |
| 128 | + if include_message_in_response: |
| 129 | + default_data = { |
| 130 | + "message": code.phrase, |
| 131 | + |
| 132 | + |
| 133 | +## ... source file continues with no further Headers examples... |
| 134 | + |
| 135 | +``` |
| 136 | + |
0 commit comments