WhintPy 1.1

https://sourceforge.net/projects/whintpy/

Module whintpy.connection

Class BaseAuthentication

Description

Base class for all authentication back-ends.

Its purpose is to provide an easy interface to authenticate a user against the WhintPy API.

Any authentication method is characterized by two parameters: its name and its identifier. The name is specific to each authentication backend. but the identifier is specific to each authentication instance.

Constructor

Initialize the authentication method.

Parameters
  • method_id: (str | None) Identifier of the authentication method
  • args: (dict) Credentials for a subclass of BaseAuthentication.
  • kwargs: (dict) Keywords for a subclass of BaseAuthentication.
Raises
  • TypeError: Invalid given method_id
View Source
def __init__(self, method_id: str | None=None, *args, **kwargs):
    """Initialize the authentication method.

    :param method_id: (str | None) Identifier of the authentication method
    :param args: (dict) Credentials for a subclass of BaseAuthentication.
    :param kwargs: (dict) Keywords for a subclass of BaseAuthentication.
    :raises: TypeError: Invalid given ``method_id``

    """
    self._available = True
    self.__method_id = None
    if method_id is not None:
        if isinstance(method_id, str) is False:
            raise TypeError(f"BaseAuthentication method_id must be a string. Got '{type(method_id).__name__}' instead.")
        if len(method_id) > 0:
            self.__method_id = method_id

Public functions

get_available

Return True if authentication is available.

View Source
def get_available(self) -> bool:
    """Return True if authentication is available."""
    return self._available

name

Return the name of the authentication method.

Must be overridden by subclasses.

Returns
  • (str) The name of the authentication method.
View Source
@staticmethod
def name() -> str:
    """Return the name of the authentication method.

        Must be overridden by subclasses.

        :return: (str) The name of the authentication method.

        """
    return BaseAuthentication._get_default_name(BaseAuthentication)

get_method_id

Return the id of the authentication method or None.

Returns
  • (str | None) The id of the method if set, None otherwise.
View Source
def get_method_id(self) -> str | None:
    """Return the id of the authentication method or None.

        :return: (str | None) The id of the method if set, None otherwise.

        """
    return self.__method_id

authenticate

The authentication base method, must be overridden.

Must be overridden by subclasses.

Parameters
  • args: (any) the arguments of the credentials depends on the method
  • kwargs: (dict) the arguments of the credentials depends on the method
Returns
  • (bool) True if the authentication is successful, False otherwise
View Source
def authenticate(self, *args, **kwargs) -> tuple[bool, str]:
    """The authentication base method, must be overridden.

        Must be overridden by subclasses.

        :param args: (any) the arguments of the credentials depends on the method
        :param kwargs: (dict) the arguments of the credentials depends on the method
        :return: (bool) True if the authentication is successful, False otherwise

        """
    raise NotImplementedError("{:s} does not implement 'authenticate' method.".format(self.__class__.__name__))

get_args

Return the arguments of the authentication method.

Must be overridden by subclasses.

Returns
  • (list) The arguments of the method
View Source
def get_args(self) -> list:
    """Return the arguments of the authentication method.

        Must be overridden by subclasses.

        :return: (list) The arguments of the method

        """
    raise NotImplementedError("{:s} does not implement 'get_args' method.".format(self.__class__.__name__))

Private functions

_get_default_name

Return a default name for the given authentication method.

Parameters
  • obj: (class) The authentication method to use.
Returns
  • (str) The default name of the authentication method.
Raises
  • TypeError: Invalid given parameter.
View Source
@staticmethod
def _get_default_name(obj: object) -> str:
    """Return a default name for the given authentication method.

        :param obj: (class) The authentication method to use.
        :return: (str) The default name of the authentication method.
        :raises: TypeError: Invalid given parameter.

        """
    if inspect.isclass(obj) is False:
        raise TypeError("Invalid parameter to define a default name. Expected a 'class'.")
    return obj.__name__.replace('Authentication', '').lower()