Пример #1
0
 def _retrieve_data_from_google_api(self):
     google_discovery_api_hook = GoogleDiscoveryApiHook(
         gcp_conn_id=self.gcp_conn_id,
         delegate_to=self.delegate_to,
         api_service_name=self.google_api_service_name,
         api_version=self.google_api_service_version)
     google_api_response = google_discovery_api_hook.query(
         endpoint=self.google_api_endpoint_path,
         data=self.google_api_endpoint_params,
         paginate=self.google_api_pagination,
         num_retries=self.google_api_num_retries)
     return google_api_response
Пример #2
0
    def test_get_conn(self, mock_authorize, mock_build):
        google_discovery_api_hook = GoogleDiscoveryApiHook(
            gcp_conn_id='google_test',
            api_service_name='youtube',
            api_version='v2')

        google_discovery_api_hook.get_conn()

        mock_build.assert_called_once_with(
            serviceName=google_discovery_api_hook.api_service_name,
            version=google_discovery_api_hook.api_version,
            http=mock_authorize.return_value,
            cache_discovery=False)
Пример #3
0
    def test_query(self, mock_get_conn, mock_getattr):
        google_discovery_api_hook = GoogleDiscoveryApiHook(
            gcp_conn_id='google_test',
            api_service_name='analyticsreporting',
            api_version='v4')
        endpoint = 'analyticsreporting.reports.batchGet'
        data = {
            'body': {
                'reportRequests': [{
                    'viewId':
                    '180628393',
                    'dateRanges': [{
                        'startDate': '7daysAgo',
                        'endDate': 'today'
                    }],
                    'metrics': [{
                        'expression': 'ga:sessions'
                    }],
                    'dimensions': [{
                        'name': 'ga:country'
                    }]
                }]
            }
        }
        num_retries = 1

        google_discovery_api_hook.query(endpoint,
                                        data,
                                        num_retries=num_retries)

        google_api_endpoint_name_parts = endpoint.split('.')
        mock_getattr.assert_has_calls([
            call(mock_get_conn.return_value,
                 google_api_endpoint_name_parts[1]),
            call()(),
            call(mock_getattr.return_value.return_value,
                 google_api_endpoint_name_parts[2]),
            call()(**data),
            call()().execute(num_retries=num_retries)
        ])
Пример #4
0
    def test_query_with_pagination(self, mock_get_conn, mock_getattr):
        google_api_conn_client_sub_call = mock_getattr.return_value.return_value
        mock_getattr.return_value.side_effect = [
            google_api_conn_client_sub_call, google_api_conn_client_sub_call,
            google_api_conn_client_sub_call, google_api_conn_client_sub_call,
            google_api_conn_client_sub_call, None
        ]
        google_discovery_api_hook = GoogleDiscoveryApiHook(
            gcp_conn_id='google_test',
            api_service_name='analyticsreporting',
            api_version='v4')
        endpoint = 'analyticsreporting.reports.batchGet'
        data = {
            'body': {
                'reportRequests': [{
                    'viewId':
                    '180628393',
                    'dateRanges': [{
                        'startDate': '7daysAgo',
                        'endDate': 'today'
                    }],
                    'metrics': [{
                        'expression': 'ga:sessions'
                    }],
                    'dimensions': [{
                        'name': 'ga:country'
                    }]
                }]
            }
        }
        num_retries = 1

        google_discovery_api_hook.query(endpoint,
                                        data,
                                        paginate=True,
                                        num_retries=num_retries)

        api_endpoint_name_parts = endpoint.split('.')
        google_api_conn_client = mock_get_conn.return_value
        mock_getattr.assert_has_calls([
            call(google_api_conn_client, api_endpoint_name_parts[1]),
            call()(),
            call(google_api_conn_client_sub_call, api_endpoint_name_parts[2]),
            call()(**data),
            call()().__bool__(),
            call()().execute(num_retries=num_retries),
            call(google_api_conn_client, api_endpoint_name_parts[1]),
            call()(),
            call(google_api_conn_client_sub_call,
                 api_endpoint_name_parts[2] + '_next'),
            call()(google_api_conn_client_sub_call,
                   google_api_conn_client_sub_call.execute.return_value),
            call()().__bool__(),
            call()().execute(num_retries=num_retries),
            call(google_api_conn_client, api_endpoint_name_parts[1]),
            call()(),
            call(google_api_conn_client_sub_call,
                 api_endpoint_name_parts[2] + '_next'),
            call()(google_api_conn_client_sub_call,
                   google_api_conn_client_sub_call.execute.return_value)
        ])