Add request.id (#2005)

This commit is contained in:
Adam Hopkins
2021-01-19 04:25:39 +02:00
committed by GitHub
parent 6c03dd87b1
commit 0d7e2f0d67
3 changed files with 104 additions and 0 deletions

View File

@@ -34,6 +34,7 @@ DEFAULT_CONFIG = {
"REAL_IP_HEADER": None,
"PROXIES_COUNT": None,
"FORWARDED_FOR_HEADER": "X-Forwarded-For",
"REQUEST_ID_HEADER": "X-Request-ID",
"FALLBACK_ERROR_FORMAT": "html",
"REGISTER": True,
}

View File

@@ -1,4 +1,5 @@
import email.utils
import uuid
from collections import defaultdict, namedtuple
from http.cookies import SimpleCookie
@@ -51,6 +52,7 @@ class Request:
__slots__ = (
"__weakref__",
"_cookies",
"_id",
"_ip",
"_parsed_url",
"_port",
@@ -82,6 +84,7 @@ class Request:
self.raw_url = url_bytes
# TODO: Content-Encoding detection
self._parsed_url = parse_url(url_bytes)
self._id = None
self.app = app
self.headers = headers
@@ -110,6 +113,10 @@ class Request:
class_name = self.__class__.__name__
return f"<{class_name}: {self.method} {self.path}>"
@classmethod
def generate_id(*_):
return uuid.uuid4()
async def respond(
self, response=None, *, status=200, headers=None, content_type=None
):
@@ -148,6 +155,26 @@ class Request:
if not self.body:
self.body = b"".join([data async for data in self.stream])
@property
def id(self):
if not self._id:
self._id = self.headers.get(
self.app.config.REQUEST_ID_HEADER,
self.__class__.generate_id(self),
)
# Try casting to a UUID or an integer
if isinstance(self._id, str):
try:
self._id = uuid.UUID(self._id)
except ValueError:
try:
self._id = int(self._id)
except ValueError:
...
return self._id
@property
def json(self):
if self.parsed_json is None: