Coverage for fio_wrapper/decorator.py: 100%

12 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-11-16 11:50 +0100

1from functools import wraps 

2 

3from fio_wrapper.exceptions import NoAPIKeyProvided 

4from fio_wrapper.fio_adapter import FIOAdapter 

5 

6 

7def apikey_required(func) -> any: 

8 """Wraps endpoint and check for FIO API Key 

9 

10 Args: 

11 func (method): Wrapped endpoint method 

12 

13 Raises: 

14 SystemExit: Decorator can only be used on endpoints 

15 NoAPIKeyProvided: FIO API Key not provided, but endpoint requires it. 

16 

17 Returns: 

18 method: Executed endpoint method 

19 """ 

20 

21 @wraps(func) 

22 def wrapper_apikey_required(self, *args, **kwargs): 

23 # can only decorate endpoint functions 

24 if self.adapter is None or not isinstance(self.adapter, FIOAdapter): 

25 raise SystemExit("apikey_required decorator can only be used on endpoints") 

26 

27 # requires API Key set in adapter 

28 if self.adapter.header is None or self.adapter.header["Authorization"] is None: 

29 raise NoAPIKeyProvided( 

30 "FIO API Key not provided. This endpoint requires an API key." 

31 ) 

32 

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

34 

35 return wrapper_apikey_required