pelican-port-forwarding/api/__init__.py
2025-03-02 14:49:18 +00:00

41 lines
865 B
Python

import requests
import enum
from typing import Tuple
class ApiAuthType(enum.Enum):
NoAuth = 0
Header = 1
Cookie = 2
class Api(object):
base_url: str = None
def transform(self, data):
return data
def apply_authentication(self) -> Tuple[ApiAuthType, dict]:
raise NotImplementedError
def _get(self, endpoint, raw: bool = False):
url = self.base_url + endpoint
headers = {
"Accept": "application/json"
}
cookies = {}
ty, auth = self.apply_authentication()
if ty == ApiAuthType.Header:
headers.update(auth)
elif ty == ApiAuthType.Cookie:
cookies.update(auth)
response = requests.get(url, headers=headers, cookies=cookies)
if raw:
return response
return self.transform(response.json())