Skip to content

Decorators

apikey_required

apikey_required(func)

Wraps endpoint and check for FIO API Key

Parameters:

Name Type Description Default
func method

Wrapped endpoint method

required

Raises:

Type Description
SystemExit

Decorator can only be used on endpoints

NoAPIKeyProvided

FIO API Key not provided, but endpoint requires it.

Returns:

Name Type Description
method any

Executed endpoint method

Source code in fio_wrapper/decorator.py
def apikey_required(func) -> any:
    """Wraps endpoint and check for FIO API Key

    Args:
        func (method): Wrapped endpoint method

    Raises:
        SystemExit: Decorator can only be used on endpoints
        NoAPIKeyProvided: FIO API Key not provided, but endpoint requires it.

    Returns:
        method: Executed endpoint method
    """

    @wraps(func)
    def wrapper_apikey_required(self, *args, **kwargs):
        # can only decorate endpoint functions
        if self.adapter is None or not isinstance(self.adapter, FIOAdapter):
            raise SystemExit("apikey_required decorator can only be used on endpoints")

        # requires API Key set in adapter
        if self.adapter.header is None or self.adapter.header["Authorization"] is None:
            raise NoAPIKeyProvided(
                "FIO API Key not provided. This endpoint requires an API key."
            )

        return func(self, *args, **kwargs)

    return wrapper_apikey_required