def fa(self, key: str = None, original: bool = False) -> ListDict: if original is True and key is None: # Fetch all as original cursor data = super().fetchall() table_keys = self.get_column_info().keys() table_infos = self.get_column_info() for field in table_keys: if table_infos[field]['type'] == 17: for key, value in data: try: data[key][field] = data[key][field].tobytes().decode('utf-8') # Unescape bytea value except TypeError: # TODO: Do not know why it raise TypeError exception, it works, but its strange pass else: if key is None or key not in self.get_column_info().keys(): # Fetch all as a list data = [] while 1: d = self.fo() if d is None: break data.append(dict(d)) else: # Fetch all as OrderedDict under key data = OrderedDict() while 1: d = self.fo() if d is None: break data[d[key]] = d tools.check_request_timeout() # Application hook to return 408 if time is over return data
def send_request(self, api_endpoint: str, data: dict = {}) -> dict: auth_token = self._abiospool.auth_token if auth_token is None: # Auth token does not exists, get new one self.authenticate() auth_token = self._abiospool.auth_token if auth_token is None: error_message = 'Cannot connect to Provider 2' self._logging(error_message, log_type='error') raise cherrypy.HTTPError(401, error_message) ac = api_config[self._name] url_start = '{}{}?'.format(ac['url'], api_endpoint) if '?' in api_endpoint: url_start = '{}&'.format(url_start[0:-1]) url = '{}access_token={}'.format(url_start, auth_token) if self._abiospool.request_permission: response = requests.get(url, data=to_json(data)) if response.status_code == 401 and response.json()['error_description'] == 'Access token is not valid.': # Unauthorized -> authenticate & repeat request error_message = 'Provider 2 Unauthorized: {} {}'.format(response.json()['error_description'], auth_token) self._logging(error_message, url, 'error') info_message = 'Reconnect to Provider 2' self._logging(info_message) self.authenticate() response = requests.get(url, data=to_json(data)) tools.check_request_timeout() # Application hook to return 408 if time is over return response.headers, response.json()
def q(self, query: str, params: dict = None) -> IntNone: # Execute query super().execute(query, params) if isinstance(super().rowcount, int): return super().rowcount tools.check_request_timeout() # Application hook to return 408 if time is over return None
def send_request(self, api_endpoint: str, data: dict = {}) -> dict: ac = api_config[self._name] url_start = '{}{}?'.format(ac['url'], api_endpoint) if '?' in api_endpoint: url_start = '{}&'.format(url_start[0:-1]) url = '{}token={}'.format(url_start, ac['auth_token']) try: if self._pandapool.request_permission: response = requests.get(url, data=to_json(data)) if response.status_code == 401: raise AuthenticationError(response.json()['error']) elif 'error' in response.json(): raise Exception(response.json()['error']) except AuthenticationError as e: error_message = 'Authentication error on Provider 1: {}'.format(e) self._logging(error_message, url, 'error') raise cherrypy.HTTPError(401, error_message) except Exception as e: error_message = 'Request to Provider 1 was not succesfull: {}'.format( e) self._logging(error_message, url, 'error') raise cherrypy.HTTPError(500, error_message) tools.check_request_timeout( ) # Application hook to return 408 if time is over return response.headers, response.json()
def fo(self) -> dict: # Fetch one data = super().fetchone() table_keys = self.get_column_info().keys() table_infos = self.get_column_info() for field in table_keys: if table_infos[field]['type'] == 17: try: data[field] = data[field].tobytes().decode('utf-8') # Unescape bytea value except TypeError: # TODO: Do not know why it raise TypeError exception, it works, but its strange pass tools.check_request_timeout() # Application hook to return 408 if time is over return data