File: util.py

package info (click to toggle)
flask-limiter 3.12-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,264 kB
  • sloc: python: 6,432; makefile: 165; sh: 67
file content (33 lines) | stat: -rw-r--r-- 987 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from __future__ import annotations

from collections.abc import Callable
from typing import Any

from flask import request


def get_remote_address() -> str:
    """
    :return: the ip address for the current request
     (or 127.0.0.1 if none found)

    """
    return request.remote_addr or "127.0.0.1"


def get_qualified_name(callable: Callable[..., Any]) -> str:
    """
    Generate the fully qualified name of a callable for use in storing
    mappings of decorated functions to rate limits

    The __qualname__ of the callable is appended in case there is a name
    clash in a module due to locally scoped functions that are decorated.

    TODO: Ideally __qualname__ should be enough, however view functions
     generated by class based views do not update that and therefore
     would not be uniquely identifiable unless __module__ & __name__
     are inspected.

    :meta private:
    """
    return f"{callable.__module__}.{callable.__name__}.{callable.__qualname__}"