示例#1
0
    def _build_restapi_query(self) -> RestApiQuery:

        dashes_query = RedashPaginatedRestApiQuery(
            query_to_join=EmptyRestApiQuerySeed(),
            url=f'{self._api_base_url}/dashboards',
            params=self._get_default_api_query_params(),
            json_path=
            'results[*].[id,name,slug,created_at,updated_at,is_archived,is_draft,user]',
            field_names=[
                'dashboard_id', 'dashboard_name', 'slug', 'created_timestamp',
                'last_modified_timestamp', 'is_archived', 'is_draft', 'user'
            ],
            skip_no_result=True)

        if self._redash_version >= 9:
            dashboard_url = f'{self._api_base_url}/dashboards/{{dashboard_id}}'
        else:
            dashboard_url = f'{self._api_base_url}/dashboards/{{slug}}'

        return RestApiQuery(query_to_join=dashes_query,
                            url=dashboard_url,
                            params=self._get_default_api_query_params(),
                            json_path='widgets',
                            field_names=['widgets'],
                            skip_no_result=True)
示例#2
0
    def _build_restapi_query(self) -> RestApiQuery:
        databricks_sql_dashboard_query = DatabricksSQLPaginatedRestApiQuery(
            query_to_join=EmptyRestApiQuerySeed(),
            url=self._databricks_sql_dashboards_api_base,
            params={"headers": self._get_databrick_request_headers()},
            json_path="results[*].[id,name,tags,updated_at,created_at,user]",
            field_names=[
                "dashboard_id",
                "dashboard_name",
                "tags",
                "last_modified_timestamp",
                "created_timestamp",
                "user",
            ],
            skip_no_results=True,
        )

        return RestApiQuery(
            query_to_join=databricks_sql_dashboard_query,
            url=f"{self._databricks_sql_dashboards_api_base}/{{dashboard_id}}",
            params={"headers": self._get_databrick_request_headers()},
            json_path="widgets",
            field_names=["widgets"],
            skip_no_result=True,
        )
    def test_paginated_rest_api_query(self) -> None:
        paged_content = [{
            'page': 1,
            'page_size': 5,
            'count': 12,
            'results': [{
                'test': True
            }] * 5
        }, {
            'page': 2,
            'page_size': 5,
            'count': 12,
            'results': [{
                'test': True
            }] * 5
        }, {
            'page': 3,
            'page_size': 5,
            'count': 12,
            'results': [{
                'test': True
            }] * 2
        }, {
            'page': 4,
            'page_size': 5,
            'count': 12,
            'results': []
        }]

        with patch('databuilder.rest_api.rest_api_query.requests.get'
                   ) as mock_get:
            # .json() is called twice (ugh), so we have to double each page
            mock_get.return_value.json.side_effect = [
                page for page in paged_content for page in [page] * 2
            ]

            q = RedashPaginatedRestApiQuery(
                query_to_join=EmptyRestApiQuerySeed(),
                url='example.com',
                json_path='results[*].[test]',
                params={},
                field_names=['test'],
                skip_no_result=True)
            n_records = 0
            for record in q.execute():
                self.assertEqual(record['test'], True)
                n_records += 1

            self.assertEqual(n_records, 12)
    def _build_restapi_query(self):
        # type: () -> RestApiQuery

        dashes_query = RedashPaginatedRestApiQuery(
            query_to_join=EmptyRestApiQuerySeed(),
            url='{redash_api}/dashboards'.format(redash_api=self._api_base_url),
            params=self._get_default_api_query_params(),
            json_path='results[*].[id,name,slug,created_at,updated_at,is_archived,is_draft,user]',
            field_names=[
                'dashboard_id', 'dashboard_name', 'slug', 'created_timestamp',
                'last_modified_timestamp', 'is_archived', 'is_draft', 'user'
            ],
            skip_no_result=True
        )

        return RestApiQuery(
            query_to_join=dashes_query,
            url='{redash_api}/dashboards/{{slug}}'.format(redash_api=self._api_base_url),
            params=self._get_default_api_query_params(),
            json_path='widgets',
            field_names=['widgets'],
            skip_no_result=True
        )
    def test_empty_rest_api_query_seed(self) -> None:
        rest_api_query = EmptyRestApiQuerySeed()

        result = [v for v in rest_api_query.execute()]
        assert len(result) == 1