123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- """middleware class"""
- from abc import ABC, abstractmethod
- import requests
- class MiddlewareBase(ABC):
- """Middleware abstract base class"""
- @abstractmethod
- def get(self, path, add_c_var=False):
- """Send get request"""
- @abstractmethod
- def post(self, path, data=None):
- """Send post request"""
- class LocalAuthentication(MiddlewareBase):
- """Local authentication"""
- def __init__(self, username, password, login_method):
- self.username = username
- self.password = password
- self.login_method = login_method
- super().__init__()
- def get(self, path, add_c_var=False):
- """Send get requests"""
- def post(self, path, data=None):
- """Send post request"""
- class RemoteAuthentication(MiddlewareBase):
- """Remote authentication"""
- def __init__(self, api_url, authentication_key):
- self.api_url = api_url
- self.headers = {
- 'Authorization': authentication_key
- }
- super().__init__()
- def get(self, path, add_c_var=False):
- """Send get requests"""
- try:
- response = requests.get(
- '{}{}'.format(self.api_url, path), headers=self.headers
- )
- return response.text
- except requests.exceptions.Timeout:
- print('timeout')
- except requests.exceptions.RequestException as exception:
- print('request exception')
- raise SystemExit(exception)
- return None
- def post(self, path, data=None):
- """Send post request"""
|