This repository was archived by the owner on May 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 305
Expand file tree
/
Copy pathdatafold_api.py
More file actions
163 lines (135 loc) · 5.34 KB
/
datafold_api.py
File metadata and controls
163 lines (135 loc) · 5.34 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import dataclasses
import enum
from typing import Any, Dict, List, Optional
import pydantic
import requests
class TestDataSourceStatus(str, enum.Enum):
SUCCESS = "ok"
FAILED = "error"
SKIP = "skip"
UNKNOWN = "unknown"
class TCloudApiDataSourceSchema(pydantic.BaseModel):
title: str
properties: Dict[str, Dict[str, Any]]
required: List[str]
secret: List[str]
class TCloudApiDataSourceConfigSchema(pydantic.BaseModel):
name: str
db_type: str
config_schema: TCloudApiDataSourceSchema
class TCloudApiDataSource(pydantic.BaseModel):
id: Optional[int] = None
name: str
type: str
is_paused: Optional[bool] = False
hidden: Optional[bool] = False
temp_schema: Optional[str] = None
disable_schema_indexing: Optional[bool] = False
disable_profiling: Optional[bool] = False
catalog_include_list: Optional[str] = None
catalog_exclude_list: Optional[str] = None
schema_indexing_schedule: Optional[str] = None
schema_max_age_s: Optional[int] = None
profile_schedule: Optional[str] = None
profile_exclude_list: Optional[str] = None
profile_include_list: Optional[str] = None
discourage_manual_profiling: Optional[bool] = False
lineage_schedule: Optional[str] = None
float_tolerance: Optional[float] = 0.0
options: Optional[Dict[str, Any]] = None
queue_name: Optional[str] = None
scheduled_queue_name: Optional[str] = None
groups: Optional[Dict[int, bool]] = None
view_only: Optional[bool] = False
created_from: Optional[str] = None
source: Optional[str] = None
max_allowed_connections: Optional[int] = None
last_test: Optional[Any] = None
secret_id: Optional[int] = None
class TDsConfig(pydantic.BaseModel):
name: str
type: str
temp_schema: str
float_tolerance: float = 0.0
options: Dict[str, Any]
disable_schema_indexing: bool = True
disable_profiling: bool = True
class TCloudApiDataDiff(pydantic.BaseModel):
data_source1_id: int
data_source2_id: int
table1: List[str]
table2: List[str]
pk_columns: List[str]
class TCloudDataSourceTestResult(pydantic.BaseModel):
status: TestDataSourceStatus
message: str
outcome: str
class TCloudApiDataSourceTestResult(pydantic.BaseModel):
name: str
status: str
result: TCloudDataSourceTestResult
@dataclasses.dataclass
class DatafoldAPI:
api_key: str
host: str = "https://app.datafold.com"
timeout: int = 30
def __post_init__(self):
self.host = self.host.rstrip("/")
self.headers = {
"Authorization": f"Key {self.api_key}",
"Content-Type": "application/json",
}
def make_get_request(self, url: str) -> Any:
rv = requests.get(url=f"{self.host}/{url}", headers=self.headers, timeout=self.timeout)
rv.raise_for_status()
return rv
def make_post_request(self, url: str, payload: Any) -> Any:
rv = requests.post(url=f"{self.host}/{url}", headers=self.headers, json=payload, timeout=self.timeout)
rv.raise_for_status()
return rv
def get_data_sources(self) -> List[TCloudApiDataSource]:
rv = self.make_get_request(url="api/data_sources")
rv.raise_for_status()
return [TCloudApiDataSource(**item) for item in rv.json()]
def create_data_source(self, config: TDsConfig) -> TCloudApiDataSource:
# TODO: replace an internal url by a public one
rv = self.make_post_request(url="api/internal/data_sources", payload=config.dict())
return TCloudApiDataSource(**rv.json())
def get_data_source_schema_config(self) -> List[TCloudApiDataSourceConfigSchema]:
# TODO: replace an internal url by a public one
rv = self.make_get_request(url="api/internal/data_sources/types")
return [
TCloudApiDataSourceConfigSchema(
name=item["name"],
db_type=item["type"],
config_schema=TCloudApiDataSourceSchema(
title=item["configuration_schema"]["title"],
properties=item["configuration_schema"]["properties"],
required=item["configuration_schema"]["required"],
secret=item["configuration_schema"]["secret"],
),
)
for item in rv.json()
]
def create_data_diff(self, payload: TCloudApiDataDiff) -> int:
rv = self.make_post_request(url="api/v1/datadiffs", payload=payload.dict())
return rv.json()["id"]
def test_data_source(self, data_source_id: int) -> int:
# TODO: replace an internal url by a public one
rv = self.make_post_request(f"api/internal/data_sources/{data_source_id}/test", {})
return rv.json()["job_id"]
def check_data_source_test_results(self, job_id: int) -> List[TCloudApiDataSourceTestResult]:
# TODO: replace an internal url by a public one
rv = self.make_get_request(f"api/internal/data_sources/test/{job_id}")
return [
TCloudApiDataSourceTestResult(
name=item["step"],
status=item["status"],
result=TCloudDataSourceTestResult(
status=item["result"]["code"].lower(),
message=item["result"]["message"],
outcome=item["result"]["outcome"],
),
)
for item in rv.json()["results"]
]