Skip to content

Instantly share code, notes, and snippets.

@pavelpy
Created October 10, 2018 11:39
Show Gist options
  • Select an option

  • Save pavelpy/147660baa0dcf781d055c5fc703802d7 to your computer and use it in GitHub Desktop.

Select an option

Save pavelpy/147660baa0dcf781d055c5fc703802d7 to your computer and use it in GitHub Desktop.

Revisions

  1. pavelpy created this gist Oct 10, 2018.
    32 changes: 32 additions & 0 deletions bypass_arguments_in_other_class_methods.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    """
    >>> api_instance = BypassApiKey(RemoteServerApi())
    >>> api_instance.method_that_need_access_token(1, 2, 3)
    [(1, 2, 3), {'access_token': 'test'}]
    """
    import functools
    ACCESS_TOKEN = 'test'


    class RemoteServerApi:
    def method_that_need_access_token(self, *args, **kwargs):
    if 'access_token' in kwargs:
    return [args, kwargs]
    else:
    raise Exception('I will not work without access_token')


    def bypass_api_key(func):
    @functools.wraps(func)
    def wrapper_decorator(*args, **kwargs):
    kwargs.update(dict(access_token=ACCESS_TOKEN))
    value = func(*args, **kwargs)
    return value
    return wrapper_decorator


    class BypassApiKey:
    def __init__(self, class_instance):
    self.class_instance = class_instance

    def __getattr__(self, item):
    return bypass_api_key(getattr(self.class_instance, item))