-
Notifications
You must be signed in to change notification settings - Fork 208
/
rest_api_extractor.py
70 lines (51 loc) · 2.08 KB
/
rest_api_extractor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# Copyright Contributors to the Amundsen project.
# SPDX-License-Identifier: Apache-2.0
import importlib
import logging
from typing import (
Any, Dict, Iterator, Optional,
)
from pyhocon import ConfigTree
from databuilder.extractor.base_extractor import Extractor
from databuilder.rest_api.base_rest_api_query import BaseRestApiQuery
REST_API_QUERY = 'restapi_query'
MODEL_CLASS = 'model_class'
# Static record that will be added into extracted record
# For example, DashboardMetadata requires product name (static name) of Dashboard and REST api does not provide
# it. and you can add {'product': 'mode'} so that it will be included in the record.
STATIC_RECORD_DICT = 'static_record_dict'
LOGGER = logging.getLogger(__name__)
class RestAPIExtractor(Extractor):
"""
An Extractor that calls one or more REST API to extract the data.
This extractor almost entirely depends on RestApiQuery.
"""
def init(self, conf: ConfigTree) -> None:
self._restapi_query: BaseRestApiQuery = conf.get(REST_API_QUERY)
self._iterator: Optional[Iterator[Dict[str, Any]]] = None
self._static_dict = conf.get(STATIC_RECORD_DICT, dict())
LOGGER.info('static record: %s', self._static_dict)
model_class = conf.get(MODEL_CLASS, None)
if model_class:
module_name, class_name = model_class.rsplit(".", 1)
mod = importlib.import_module(module_name)
self.model_class = getattr(mod, class_name)
def extract(self) -> Any:
"""
Fetch one result row from RestApiQuery, convert to {model_class} if specified before
returning.
:return:
"""
if not self._iterator:
self._iterator = self._restapi_query.execute()
try:
record = next(self._iterator)
except StopIteration:
return None
if self._static_dict:
record.update(self._static_dict)
if hasattr(self, 'model_class'):
return self.model_class(**record)
return record
def get_scope(self) -> str:
return 'extractor.restapi'