def get_target_export(self, export_job_id): """ Get specific target export job id's status. `Returns:` Parsons Table See :ref:`parsons-table` for output options. """ response = self.connection.get_request( f'targetExportJobs/{export_job_id}') json_string = json.dumps(response) json_obj = json.loads(json_string) for i in json_obj: job_status = i['jobStatus'] if job_status == 'Complete': for j in json_obj: csv = j['file']['downloadUrl'] response_csv = requests.get(csv) return Table.from_csv_string(response_csv.text) elif job_status == 'Pending' or job_status == 'InProcess': logger.info( f'Target export job is pending or in process for {export_job_id}.' ) else: raise TargetsFailed(f'Target export failed for {export_job_id}')
def get_cached_query_results(self, query_id=None, query_api_key=None): """ Get the results from a cached query result and get back the CSV http response object back with the CSV string in result.content `Args:` query_id: str or int The query id of the query query_api_key: str If you did not supply a user_api_key on the Redash object, then you can supply a query_api_key to get cached results back anonymously. `Returns:` Table Class """ query_id = check('REDASH_QUERY_ID', query_id) query_api_key = check('REDASH_QUERY_API_KEY', query_api_key, optional=True) params = {} if not self.user_api_key and query_api_key: params['api_key'] = query_api_key response = self.session.get( f'{self.base_url}/api/queries/{query_id}/results.csv', params=params, verify=self.verify) if response.status_code != 200: raise RedashQueryFailed( f'Failed getting results for query {query_id}. {response.text}' ) return Table.from_csv_string(response.text)
def test_from_csv_string(self): path = self.tbl.to_csv() # Pull the file into a string with open(path, 'r') as f: str = f.read() result_tbl = Table.from_csv_string(str) assert_matching_tables(self.tbl, result_tbl)
def get_fresh_query_results(self, query_id=None, params=None): """ Make a fresh query result and get back the CSV http response object back with the CSV string in result.content `Args:` query_id: str or int The query id of the query params: dict If there are values for the redash query parameters (described https://redash.io/help/user-guide/querying/query-parameters e.g. "{{datelimit}}" in the query), then this is a dict that will pass the parameters in the POST. We add the "p_" prefix for parameters, so if your query had ?p_datelimit=.... in the url, you should just set 'datelimit' in params here. If you set this with REDASH_QUERY_PARAMS environment variable instead of passing the values, then you must include the "p_" prefixes and it should be a single url-encoded string as you would see it in the URL bar. `Returns:` Table Class """ query_id = check('REDASH_QUERY_ID', query_id, optional=True) params_from_env = check('REDASH_QUERY_PARAMS', '', optional=True) redash_params = ( {'p_%s' % k: str(v).replace("'", "''") for k, v in params.items()} if params else {}) response = self.session.post( f'{self.base_url}/api/queries/{query_id}/refresh?{params_from_env}', params=redash_params, verify=self.verify) if response.status_code != 200: raise RedashQueryFailed( f'Refresh failed for query {query_id}. {response.text}') job = response.json()['job'] result_id = self._poll_job(self.session, job, query_id) if result_id: response = self.session.get( f'{self.base_url}/api/queries/{query_id}/results/{result_id}.csv', verify=self.verify) if response.status_code != 200: raise RedashQueryFailed( f'Failed getting results for query {query_id}. {response.text}' ) else: raise RedashQueryFailed( f'Failed getting result {query_id}. {response.text}') return Table.from_csv_string(response.text)