Python 使用装饰器进行客户端身份验证

Python client authentication with decorators

我想在 REST API 之上构建一个 python 客户端,该客户端使用 api_token 进行身份验证。因此所有 api 调用都需要 api_token。因为添加字段非常难看

'token=...'

例如

a = f1(5, token='token')
b = f2(6, 12, token='token')
c = f3(2, 'a', token='token')

其中 f1 和 f2 内部委托给 REST api

每个函数调用。我想要的是:

auth = authenticate('token')

a = f1(5)
b = f2(6, 12,)
c = f3(2, 'a')

我能做的是创建一个 class 并使所有函数成为成员函数。因此,我们将有:

auth = calculator('token')
a = auth.f1(5)
b = auth.f2(6, 12,)
c = auth.f3(2, 'a')

但这也有点难看。我试图让它与装饰器一起工作,但到目前为止无济于事。

class authenticate:
   def __init__(self, token):
       self.token = token

   def __call__(self, func):
       def functor(*args, **kwargs):
           return func(*args, **kwargs, key=self.authentication)
       return functor 

@authenticate
def f1(a, key):
    data = a
    result = requests.get(1, data, key)
    return result

然而,这似乎无处可去。我还想知道这是否可行,因为装饰器是在导入时执行的,令牌是在运行时添加的。

关于如何使这项工作有任何建议,或者有人知道是否有其他标准模式吗?

经过一番摸索,我们得出了以下结论:

class authenticate:
   # start empty key
   key = None

   @classmethod
   """ add the token """
   def set_key(cls, token):
       cls.token = token

   def __init__(self, func=None):
       if func is not None:
           self.func = func
       else:
           print('no function')

   def __call__(self, *arg):
       """
       add authentication to function func
       """
       ret = self.func(*arg, auth_key=self.key)
       return ret

@authenticate
def f1(a, key):
    data = a
    result = requests.get(1, data, key)
    return result

然后你可以运行编码如下:

authentication_key = 'token'

print('Initiate class')
authenticate().set_key(key=authentication_key)

print('Run f1(5)')
a1 = f1(5) # no token needed!
a2 = f2(6, 12) # again no token needed as it is in the decorator
print(a1)

这或多或少如我所愿,我发现它比 class 方法更简洁。如果有人有更好的建议或改进请告诉我。