Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
hub-client / dockerhub / marketo / decorators.py
Size: Mime:
import logging

log = logging.getLogger(__name__)


def authenticate(method):
    """
    decorator used to wrap methods that require authentication

    :param method:  wrapped method
    :returns: original method
    """
    def wrapper(self, *args, **kwargs):
        self.handle_authentication()
        return method(self, *args, **kwargs)
    return wrapper


def increment_api_calls(method):
    """
    Decorator used to increment the number of times a method is called.  This
    is intended to track api requests at the adapter level and for individual
    method calls.  It's also more of a debug feature and not intended for
    reporting quality metrics.

    :param method:
    :return:
    """
    def wrapper(self, *args, **kwargs):
        if hasattr(self, '_track_api_usage'):
            if hasattr(self, '_api_calls'):
                self._api_calls += 1
            else:
                setattr(self, '_api_calls', 1)
        return method(self, *args, **kwargs)
    return wrapper


def track_api_calls(method):
    """
    Decorator used at the adapter layer, instrumenting the class to track. It's
    also more of a debug feature and not intended for reporting quality
    metrics.

    :param method:
    :return:
    """
    def wrapper(self, *args, **kwargs):
        track_api_usage = getattr(self, '_track_api_usage', False)
        if track_api_usage:
            _client = getattr(self, '_client')
            if _client is not None:
                setattr(_client, '_track_api_usage', True)
                setattr(_client, '_api_calls', 0)
        return_value = method(self, *args, **kwargs)
        if track_api_usage:
            _client = getattr(self, '_client')
            if _client is not None:
                _api_calls = getattr(_client, '_api_calls')
                log.info(
                    "Number of api calls for method `%s`: %s",
                    method.__name__, _api_calls
                )
                setattr(_client, '_api_calls', 0)
        return return_value
    return wrapper