Skip to content

Commit 3a2a0db

Browse files
fix(python/fastapi): prefix path capturing (#1198)
## 🧰 Changes Fix prefix path not captured issue. We had a problem with FastAPI integration that the Middleware did not captured the path for each request and sent only “/” instead. ## 🧬 QA & Testing Try sending a few test requests with paths, for example, “/hello/world”. Now it should not be empty.
1 parent ab89594 commit 3a2a0db

File tree

1 file changed

+62
-18
lines changed

1 file changed

+62
-18
lines changed

packages/python/readme_metrics/PayloadBuilder.py

+62-18
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,61 @@ def _get_query_string(self, request):
309309

310310
return result
311311

312+
def _get_scheme(self, request):
313+
"""Helper function to get the scheme string for a request, translating fields from
314+
either ASGIRequest or WSGIRequest object.
315+
316+
Args:
317+
request (Request): Request object containing the request information, either
318+
a ASGIRequest or WSGIRequest`.
319+
320+
Returns:
321+
str: Scheme string, for example "http"
322+
"""
323+
if hasattr(request, "environ") and "wsgi.url_scheme" in request.environ:
324+
return request.environ["wsgi.url_scheme"]
325+
if hasattr(request, "scheme"):
326+
return request.scheme
327+
return None
328+
329+
def _get_host(self, request):
330+
"""Helper function to get the host string for a request, translating fields from
331+
either ASGIRequest or WSGIRequest object.
332+
333+
Args:
334+
request (Request): Request object containing the request information, either
335+
a ASGIRequest or WSGIRequest`.
336+
337+
Returns:
338+
str: Host string, for example "localhost:8080"
339+
"""
340+
# pylint: disable=protected-access
341+
if hasattr(request, "_get_raw_host"):
342+
# Django request objects already have a properly formatted host field
343+
return request._get_raw_host()
344+
if hasattr(request, "environ") and "HTTP_HOST" in request.environ:
345+
return request.environ["HTTP_HOST"]
346+
if hasattr(request, "scope"):
347+
return request.headers.get("host")
348+
return None
349+
350+
def _get_path(self, request):
351+
"""Helper function to get the path string for a request, translating fields from
352+
either ASGIRequest or WSGIRequest object.
353+
354+
Args:
355+
request (Request): Request object containing the request information, either
356+
a ASGIRequest or WSGIRequest`.
357+
358+
Returns:
359+
str: Path string, for example "/hello/world"
360+
"""
361+
if hasattr(request, "environ") and "PATH_INFO" in request.environ:
362+
return request.environ["PATH_INFO"]
363+
if hasattr(request, "scope"):
364+
return request.scope["path"]
365+
return None
366+
312367
def _build_base_url(self, request):
313368
"""Helper function to get the base URL for a request (full URL excluding the
314369
query string), translating fields from either a Werkzeug Request object or a
@@ -325,30 +380,19 @@ def _build_base_url(self, request):
325380
if hasattr(request, "base_url"):
326381
# Werkzeug request objects already have exactly what we need
327382
base_url = str(request.base_url)
383+
if hasattr(request, "url") and hasattr(request.url, "path"):
384+
base_url = base_url[:-1]
385+
if len(request.url.path) > 1:
386+
base_url += request.url.path
328387
if len(query_string) > 0:
329388
base_url += f"?{query_string}"
330389
return base_url
331390

332391
scheme, host, path = None, None, None
333392

334-
if hasattr(request, "environ") and "wsgi.url_scheme" in request.environ:
335-
scheme = request.environ["wsgi.url_scheme"]
336-
elif hasattr(request, "scheme"):
337-
scheme = request.scheme
338-
339-
# pylint: disable=protected-access
340-
if hasattr(request, "_get_raw_host"):
341-
# Django request objects already have a properly formatted host field
342-
host = request._get_raw_host()
343-
elif hasattr(request, "environ") and "HTTP_HOST" in request.environ:
344-
host = request.environ["HTTP_HOST"]
345-
elif hasattr(request, "scope"):
346-
host = request.headers.get("host")
347-
348-
if hasattr(request, "environ") and "PATH_INFO" in request.environ:
349-
path = request.environ["PATH_INFO"]
350-
elif hasattr(request, "scope"):
351-
path = request.scope["path"]
393+
scheme = self._get_scheme(request)
394+
host = self._get_host(request)
395+
path = self._get_path(request)
352396

353397
if scheme and path and host:
354398
if len(query_string) > 0:

0 commit comments

Comments
 (0)