import requests
from requests.exceptions import RequestException


class Request:
    """Base request class, which uses 'requests' library

    :return: Request result
    :rtype: JSON or PDF file
    """
    base_url = None
    _access_key = None
    _api_key = None
    content_type = None
    inn = None
    ogrn = None
    
    def __init__(self, base_url: str, access_key: str = None, api_key: str = None, inn: str = None, ogrn: str = None):
        self.base_url = base_url
        self._access_key = access_key
        self._api_key = api_key
        self.inn = inn
        self.ogrn = ogrn
    
    def get(self, path: str, **add_params):
        full_url = f'{self.base_url}{path}'
        payload = None
        
        if self._access_key:
            payload = {'key': self._access_key, 'inn': self.inn, 'ogrn': self.ogrn}
            payload.update(add_params)
        elif self._api_key:
            payload = {'api-key': self._api_key, 'inn': self.inn, 'ogrn': self.ogrn}
            payload.update(add_params)
        
        try:
            response = requests.get(url=full_url, params=payload)

            if ('Content-Type' in response.headers and response.headers['Content-Type'] == 'application/json') or \
                    ('Content-Type' in response.headers and response.headers['Content-Type'] == 'application/json; charset=utf-8'):
                return response.json()
            else:
                return response
        except RequestException as e:
            return f'Error: {e}'
