middleware.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. """middleware class"""
  2. from abc import ABC, abstractmethod
  3. import requests
  4. # from authentication_handler import AuthenticationHandler
  5. class MiddlewareBase(ABC):
  6. """Middleware abstract base class"""
  7. @abstractmethod
  8. def get(self, path, add_c_var=False):
  9. """Send get request"""
  10. @abstractmethod
  11. def post(self, path, data=None):
  12. """Send post request"""
  13. class LocalAuthentication(MiddlewareBase):
  14. """Local authentication"""
  15. def __init__(self, username, password, login_method):
  16. self.username = username
  17. self.password = password
  18. self.login_method = login_method
  19. super().__init__()
  20. def get(self, path, add_c_var=False):
  21. """Send get requests"""
  22. def post(self, path, data=None):
  23. """Send post request"""
  24. class RemoteAuthentication(MiddlewareBase):
  25. """Remote authentication"""
  26. def __init__(self, api_url, authentication_key):
  27. self.api_url = api_url
  28. self.headers = {
  29. 'Authorization': authentication_key
  30. }
  31. super().__init__()
  32. def get(self, path, add_c_var=False):
  33. """Send get requests"""
  34. try:
  35. response = requests.get(
  36. '{}{}'.format(self.api_url, path), headers=self.headers
  37. )
  38. return response.text
  39. except requests.exceptions.Timeout:
  40. print('timeout')
  41. except requests.exceptions.RequestException as exception:
  42. print('request exception')
  43. raise SystemExit(exception)
  44. return None
  45. def post(self, path, data=None):
  46. """Send post request"""