def request(cls, method, url, payload=None): response = cls.client().request(method, url, payload=payload) try: result = response.json() except ValueError as e: # JSONDecodeError inherits from ValueError raise_response_error(e, response, logger) return result
def instantiate_from_response(cls, response): try: resource_instance = cls(response[cls.id_attribute]) except KeyError as e: raise_response_error(e, response, logger) resource_instance.update(response) return resource_instance
def list(cls, instantiate=False): resources = cls.request('get', cls.class_url()) if type(resources) is dict: try: resources = resources['resources'] except KeyError as e: raise_response_error(e, response, logger) if instantiate: return cls.instantiate_list(resources) else: return resources
def query(query_id, parse_dates=True, use_column_names=True, api_base='https://api.contiamo.com'): http_client = HTTPClient() url, token = query_url_from_identifier(query_id, api_base) response = http_client.request( 'get', url, params={'resource_token': token}) try: json_response = response.json() except ValueError as e: # JSONDecodeError inherits from ValueError raise_response_error(e, response, logger) return parse_query_result(json_response, parse_dates=parse_dates, use_column_names=use_column_names)
def list(cls, instantiate=False): response = cls.request('get', cls.class_url()) # resources can be returned as list or dictionary if type(response) is list: resources = response else: try: resources = response['resources'] except (TypeError, KeyError) as e: raise_response_error(e, response, logger) if instantiate: return cls.instantiate_list(resources) else: return resources
def _make_request(self, url, file_object=None, file_type=None): kwargs = {'params': {'resource_token': self.token}} if file_object: if not file_type: raise ValueError( 'file_type must be specified if file_object is present') param_name = 'file-json' if file_type == 'jsonl' else 'file' kwargs.update({'files': {param_name: file_object}}) response = self.client.request('post', url, **kwargs) try: json_response = response.json() except ValueError as e: # JSONDecodeError inherits from ValueError raise_response_error(e, response, logger) return json_response