util.py 987 B

123456789101112131415161718192021222324252627282930313233
  1. from __future__ import annotations
  2. from collections.abc import Callable
  3. from typing import Any
  4. from flask import request
  5. def get_remote_address() -> str:
  6. """
  7. :return: the ip address for the current request
  8. (or 127.0.0.1 if none found)
  9. """
  10. return request.remote_addr or "127.0.0.1"
  11. def get_qualified_name(callable: Callable[..., Any]) -> str:
  12. """
  13. Generate the fully qualified name of a callable for use in storing
  14. mappings of decorated functions to rate limits
  15. The __qualname__ of the callable is appended in case there is a name
  16. clash in a module due to locally scoped functions that are decorated.
  17. TODO: Ideally __qualname__ should be enough, however view functions
  18. generated by class based views do not update that and therefore
  19. would not be uniquely identifiable unless __module__ & __name__
  20. are inspected.
  21. :meta private:
  22. """
  23. return f"{callable.__module__}.{callable.__name__}.{callable.__qualname__}"