Created
October 10, 2018 11:39
-
-
Save pavelpy/147660baa0dcf781d055c5fc703802d7 to your computer and use it in GitHub Desktop.
bypass arguments in other class methods
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
>>> 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)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment