Source code for smarter.common.utils.uri

"""
Smarter.common.utils.uri
=========================

Helpers for building absolute URIs from Django or DRF request objects.

This module provides the ``smarter_build_absolute_uri`` function, which attempts to construct
an absolute URI from a given request object. It supports Django's ``HttpRequest``, Django REST Framework's
``Request``, and mock objects for testing. The function is robust to missing or malformed request data and
returns a fallback test URL if the request cannot be resolved.

**Example usage:**

.. code-block:: python

    from smarter.common.utils import smarter_build_absolute_uri
    from django.http import HttpRequest

    request = HttpRequest()
    request.META['HTTP_HOST'] = 'localhost:9357'
    request.path = '/api/v1/resource/'
    url = smarter_build_absolute_uri(request)
    print(url)  # Output: http://localhost:9357/api/v1/resource/
"""

from typing import Optional
from unittest.mock import Mock

from django.http import HttpRequest

from smarter.lib import logging
from smarter.lib.django.validators import SmarterValidator

logger = logging.getSmarterLogger(__name__)


[docs] def smarter_build_absolute_uri(request: "HttpRequest") -> Optional[str]: """ Attempts to construct the absolute URI for a given request object. :param request: The request object, which may be an instance of :class:`django.http.HttpRequest`, :class:`rest_framework.request.Request`, :class:`django.core.handlers.wsgi.ASGIRequest`, or a mock object for testing. :type request: "HttpRequest" or compatible type :return: The absolute URI as a string, or a fallback test URL if the request is invalid or cannot be resolved. :rtype: Optional[str] .. note:: - If the request is a Django REST Framework ``Request``, it is recast to a Django ``HttpRequest``. - If the request is a mock object (e.g., from unit tests), a synthetic test URL is returned. - The function first tries to use Django's ``build_absolute_uri`` method. If unavailable, it attempts to build the URL from scheme, host, and path attributes. - If all attempts fail, a generic fallback URL is returned. .. warning:: If the request is ``None`` or cannot be resolved, the function logs a warning and returns a fallback test URL. Always validate the returned URL before using it in production. **Example usage:** .. code-block:: python from smarter.common.utils import smarter_build_absolute_uri from django.http import HttpRequest request = HttpRequest() request.META['HTTP_HOST'] = 'localhost:9357' request.path = '/api/v1/resource/' url = smarter_build_absolute_uri(request) print(url) # Output: http://localhost:9357/api/v1/resource/ # Example with DRF Request from rest_framework.request import Request drf_request = Request(...) url = smarter_build_absolute_uri(drf_request) print(url) # Example with None url = smarter_build_absolute_uri(None) print(url) # Output: http://testserver/unknown/ """ def get_host(request: "HttpRequest") -> str: """Helper function to extract the host from the request object.""" try: # Try Django's get_host method first. works well except that # it can raise KeyError on certain edge cases. if hasattr(request, "get_host"): retval = request.get_host() logger.debug( "obtained host from %s request.get_host(): %s", type(request).__name__, retval, ) return retval except KeyError: pass if hasattr(request, "META") and "HTTP_HOST" in request.META: retval = request.META["HTTP_HOST"] logger.debug( "obtained host from %s request.META['HTTP_HOST']: %s", type(request).__name__, retval, ) return retval if hasattr(request, "META") and "SERVER_NAME" in request.META: retval = request.META["SERVER_NAME"] logger.debug( "obtained host from %s request.META['SERVER_NAME']: %s", type(request).__name__, retval, ) return retval logger.warning( "could not determine host from %s request; returning 'testserver'", type(request).__name__, ) return "testserver" if request is None: retval = "http://testserver/unknown/" logger.warning( "called with None request. Returning fallback URL: %s", retval, ) return retval if isinstance(request, Mock): retval = "http://testserver/mockpath/" logger.debug( "called with Mock request; returning fake test URL: %s", retval, ) return retval # Try to use Django's build_absolute_uri if available if hasattr(request, "build_absolute_uri") and getattr(request, "META", {}).get("SERVER_NAME") is not None: try: url = request.build_absolute_uri() if url: return url # pylint: disable=W0718 except Exception as e: logger.warning( "failed to call request.build_absolute_uri(): %s", logging.SmarterFormattedText.text(str(e), logging.SmarterFormattedText.DARK_RED), ) # Try to build from scheme, host, and path try: scheme = getattr(request, "scheme", None) or getattr(request, "META", {}).get("wsgi.url_scheme", "http") host = get_host(request) path = getattr(request, "get_full_path", lambda: None)() or "/" url = f"{scheme}://{host}{path}" if SmarterValidator.is_valid_url(url): logger.debug( "built URL from request attributes: %s", url, ) return url except KeyError as e: logger.debug( "could not build url from request attributes due to a KeyError: %s", logging.SmarterFormattedText.text(str(e), logging.SmarterFormattedText.DARK_RED), ) # pylint: disable=W0718 except Exception as e: logger.debug( "failed to build URL from request attributes: %s (%s)", logging.SmarterFormattedText.text(str(e), logging.SmarterFormattedText.DARK_RED), type(e), ) # do this last since we have to import # pylint: disable=import-outside-toplevel from rest_framework.request import Request if isinstance(request, Request): # recast DRF Request to Django HttpRequest # pylint: disable=W0212 if hasattr(request, "_request"): logger.debug( "recasting DRF Request to Django HttpRequest", ) request = request._request # type: ignore if hasattr(request, "build_absolute_uri"): logger.debug( "obtaining URL from recast DRF request.build_absolute_uri()", ) return request.build_absolute_uri() # Fallback: synthesize a generic test URL logger.debug( "could not determine URL, returning fallback test URL", ) return "http://testserver/unknown/"
__all__ = ["smarter_build_absolute_uri"]