-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.py
529 lines (430 loc) · 16.6 KB
/
client.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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
#
# Copyright 2023 decodable Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from __future__ import annotations
from dataclasses import dataclass
from typing import Any, Dict, Optional, List
import requests
from typing_extensions import override
from decodable.client.api import Connector, ConnectionType, StartPosition
from decodable.client.types import FieldType
from decodable.config.client_config import (
DecodableControlPlaneClientConfig,
DecodableDataPlaneClientConfig,
)
@dataclass
class ApiResponse:
items: List[Any]
next_page_token: Optional[str]
@dataclass
class PreviewResponse:
id: str
output_stream_type: str
results: List[Dict[str, Any]]
next_token: str
@classmethod
def from_dict(cls, response: Dict[str, Any]) -> PreviewResponse:
return cls(
id=response["id"],
output_stream_type=response["output_stream_type"],
results=response["results"],
next_token=response["next_token"],
)
@dataclass
class AccountInfoResponse:
subscription: SubscriptionResponse
deployment_type: str
data_plane_hostname: str
uses_new_secrets: bool
@classmethod
def from_dict(cls, response: Dict[str, Any]) -> AccountInfoResponse:
return cls(
subscription=SubscriptionResponse.from_dict(response["subscription"]),
deployment_type=response["deployment_type"],
data_plane_hostname=response["data_plane_hostname"],
uses_new_secrets=response["uses_new_secrets"],
)
@dataclass
class SubscriptionResponse:
plan_id: str
capabilities: List[str]
@classmethod
def from_dict(cls, response: Dict[str, Any]) -> SubscriptionResponse:
return cls(
plan_id=response["plan_id"],
capabilities=response["capabilities"],
)
@dataclass
class PreviewTokensResponse:
data_plane_request: str
post_token: str
get_token: str
@classmethod
def from_dict(cls, response: Dict[str, Any]) -> PreviewTokensResponse:
return cls(
data_plane_request=response["data_plane_request"],
post_token=response["post_token"],
get_token=response["get_token"],
)
class DecodableAPIException(Exception):
@classmethod
def category(cls) -> str:
return "DecodableAPIException"
@override
def __str__(self) -> str:
return f"Decodable: {self.category()}: {self.args[0]}"
class InvalidRequest(DecodableAPIException):
@classmethod
def category(cls) -> str:
return "InvalidRequest"
class ResourceAlreadyExists(DecodableAPIException):
@classmethod
def category(cls) -> str:
return "ResourceAlreadyExists"
class ResourceNotFound(DecodableAPIException):
@classmethod
def category(cls) -> str:
return "ResourceNotFound"
def raise_api_exception(code: int, reason: str):
if code == 400:
raise InvalidRequest(reason)
elif code == 404:
raise ResourceNotFound(reason)
elif code == 409:
raise ResourceAlreadyExists(reason)
else:
raise DecodableAPIException(reason)
@dataclass
class SchemaField:
name: str
type: FieldType
def __hash__(self) -> int:
res = hash(self.name)
res ^= hash(self.type)
return res
class DecodableDataPlaneApiClient:
config: DecodableDataPlaneClientConfig
def __init__(self, config: DecodableDataPlaneClientConfig):
self.config = config
def start_preview(self, token: str, data_plane_request: str) -> PreviewResponse:
response = self._post_api_request(
data=data_plane_request,
bearer_token=token,
endpoint_url=f"{self.config.api_url}/sql/preview",
)
return PreviewResponse.from_dict(response.json())
def get_preview(self, auth_token: str, next_token: str) -> PreviewResponse:
response = self._get_api_request(
bearer_token=auth_token,
endpoint_url=f"{self.config.api_url}/sql/preview",
additional_headers={"decodable-token": next_token},
)
return PreviewResponse.from_dict(response.json())
def _get_api_request(
self,
bearer_token: str,
endpoint_url: str,
additional_headers: Optional[Dict[str, str]] = None,
) -> requests.Response:
headers = {
"accept": "application/json",
"Authorization": f"Bearer {bearer_token}",
}
if additional_headers is not None:
headers.update(additional_headers)
response = requests.get(
url=endpoint_url,
headers=headers,
)
if response.ok:
return response
else:
raise_api_exception(response.status_code, response.json())
def _post_api_request(
self, bearer_token: str, endpoint_url: str, payload: Any = None, data: Any = None
) -> requests.Response:
response = requests.post(
url=endpoint_url,
json=payload,
data=data,
headers={
"accept": "application/json",
"content-type": "application/json",
"Authorization": f"Bearer {bearer_token}",
},
)
if response.ok:
return response
else:
raise_api_exception(response.status_code, response.json())
class DecodableControlPlaneApiClient:
config: DecodableControlPlaneClientConfig
def __init__(self, config: DecodableControlPlaneClientConfig):
self.config = config
def test_connection(self) -> requests.Response:
response = requests.get(
url=f"{self.config.decodable_api_url()}/streams",
headers={
"accept": "application/json",
"authorization": f"Bearer {self.config.access_token}",
},
)
return response
def list_streams(self) -> ApiResponse:
response = self._get_api_request(
endpoint_url=f"{self.config.decodable_api_url()}/streams",
)
return self._parse_response(response.json())
def get_stream_id(self, name: str) -> Optional[str]:
streams = self.list_streams().items
stream_id = None
for stream in streams:
if stream["name"] == name:
stream_id = stream["id"]
return stream_id
def get_stream_information(self, stream_id: str) -> Dict[str, Any]:
endpoint_url = f"{self.config.decodable_api_url()}/streams/{stream_id}"
response = requests.get(
url=endpoint_url,
headers={
"accept": "application/json",
"authorization": f"Bearer {self.config.access_token}",
},
)
if response.ok:
return response.json()
else:
raise_api_exception(response.status_code, response.json())
def get_stream_from_sql(self, sql: str) -> Dict[str, Any]:
payload = {"sql": sql}
return self._post_api_request(
payload=payload,
endpoint_url=f"{self.config.decodable_api_url()}/pipelines/outputStream",
).json()
def create_stream(
self, name: str, schema_fields: List[SchemaField], watermark: Optional[str] = None
) -> ApiResponse:
payload = {
"schema": [{"name": field.name, "type": repr(field.type)} for field in schema_fields],
"name": name,
}
if watermark:
payload["watermark"] = watermark
return self._post_api_request(
payload=payload, endpoint_url=f"{self.config.decodable_api_url()}/streams"
).json()
def update_stream(self, stream_id: str, props: Dict[str, Any]) -> ApiResponse:
endpoint_url = f"{self.config.decodable_api_url()}/streams/{stream_id}"
return self._patch_api_request(payload=props, endpoint_url=endpoint_url).json()
def delete_stream(self, stream_id: str) -> None:
return self._delete_api_request(
endpoint_url=f"{self.config.decodable_api_url()}/streams/{stream_id}"
)
def clear_stream(self, stream_id: str) -> None:
self._post_api_request(
payload={}, endpoint_url=f"{self.config.decodable_api_url()}/streams/{stream_id}/clear"
)
def list_pipelines(self) -> ApiResponse:
response = self._get_api_request(
endpoint_url=f"{self.config.decodable_api_url()}/pipelines",
)
return self._parse_response(response.json())
def get_pipeline_id(self, name: str) -> Optional[str]:
pipelines = self.list_pipelines().items
pipeline_id = None
for pipeline in pipelines:
if pipeline["name"] == name:
pipeline_id = pipeline["id"]
return pipeline_id
def get_pipeline_information(self, pipeline_id: str) -> Dict[str, Any]:
endpoint_url = f"{self.config.decodable_api_url()}/pipelines/{pipeline_id}"
response = requests.get(
url=endpoint_url,
headers={
"accept": "application/json",
"authorization": f"Bearer {self.config.access_token}",
},
)
if response.ok:
return response.json()
else:
raise_api_exception(response.status_code, response.json())
def get_associated_streams(self, pipeline_id: str) -> ApiResponse:
response = self._get_api_request(
endpoint_url=f"{self.config.decodable_api_url()}/pipelines/{pipeline_id}/streams"
)
return self._parse_response(response.json())
def create_pipeline(self, sql: str, name: str, description: str) -> Dict[str, Any]:
payload = {
"sql": sql,
"name": name,
"description": description,
}
return self._post_api_request(
payload=payload, endpoint_url=f"{self.config.decodable_api_url()}/pipelines"
).json()
def update_pipeline(self, pipeline_id: str, props: Dict[str, Any]) -> Any:
return self._patch_api_request(
payload=props,
endpoint_url=f"{self.config.decodable_api_url()}/pipelines/{pipeline_id}",
).json()
def activate_pipeline(self, pipeline_id: str) -> Dict[str, Any]:
return self._post_api_request(
payload={},
endpoint_url=f"{self.config.decodable_api_url()}/pipelines/{pipeline_id}/activate",
).json()
def deactivate_pipeline(self, pipeline_id: str) -> Dict[str, Any]:
return self._post_api_request(
payload={},
endpoint_url=f"{self.config.decodable_api_url()}/pipelines/{pipeline_id}/deactivate",
).json()
def delete_pipeline(self, pipeline_id: str) -> None:
return self._delete_api_request(
endpoint_url=f"{self.config.decodable_api_url()}/pipelines/{pipeline_id}"
)
def get_preview_tokens(
self,
sql: str,
preview_start: StartPosition = StartPosition.LATEST,
input_streams: Optional[List[str]] = None,
) -> PreviewTokensResponse:
if input_streams is None:
input_streams = []
payload = {
"sql": sql,
"start_positions": {
stream: {"type": "TAG", "value": preview_start.value} for stream in input_streams
},
}
response = self._post_api_request(
payload=payload, endpoint_url=f"{self.config.decodable_api_url()}/preview/tokens"
)
return PreviewTokensResponse.from_dict(response.json())
def get_preview_dependencies(self, sql: str) -> Dict[str, Any]:
payload = {"sql": sql}
return self._post_api_request(
payload=payload, endpoint_url=f"{self.config.decodable_api_url()}/preview/dependencies"
).json()
def list_connections(self) -> ApiResponse:
response = self._get_api_request(
endpoint_url=f"{self.config.decodable_api_url()}/connections",
)
return self._parse_response(response.json())
def get_connection_id(self, name: str) -> Optional[str]:
connections = self.list_connections().items
conn_id = None
for conn in connections:
if conn["name"] == name:
conn_id = conn["id"]
break
return conn_id
def create_connection(
self,
name: str,
schema: List[SchemaField],
stream: Optional[str] = None,
connector: Connector = Connector.REST,
connection_type: ConnectionType = ConnectionType.SOURCE,
) -> Dict[str, Any]:
if not stream:
stream = name
payload = {
"name": name,
"connector": connector.value,
"type": connection_type.value,
"schema": [{"name": field.name, "type": repr(field.type)} for field in schema],
}
return self._post_api_request(
payload=payload,
endpoint_url=f"{self.config.decodable_api_url()}/connections?stream_name={stream}",
).json()
def activate_connection(self, conn_id: str) -> Dict[str, Any]:
return self._post_api_request(
payload={},
endpoint_url=f"{self.config.decodable_api_url()}/connections/{conn_id}/activate",
).json()
def deactivate_connection(self, conn_id: str) -> Dict[str, Any]:
return self._post_api_request(
payload={},
endpoint_url=f"{self.config.decodable_api_url()}/connections/{conn_id}/deactivate",
).json()
def delete_connection(self, conn_id: str):
self._delete_api_request(
endpoint_url=f"{self.config.decodable_api_url()}/connections/{conn_id}"
)
def send_events(self, id: str, events: List[Dict[str, Any]]) -> int:
payload = {"events": events}
response = self._post_api_request(
payload=payload,
endpoint_url=f"{self.config.decodable_api_url()}/connections/{id}/events",
).json()
return response["count"]
def get_account_info(self, account_name: str) -> AccountInfoResponse:
response = self._get_api_request(
endpoint_url=f"{self.config.decodable_api_url()}/accounts/{account_name}"
)
return AccountInfoResponse.from_dict(response.json())
def _parse_response(self, result: Any) -> ApiResponse:
return ApiResponse(items=result["items"], next_page_token=result["next_page_token"])
def _post_api_request(self, payload: Any, endpoint_url: str) -> requests.Response:
response = requests.post(
url=endpoint_url,
json=payload,
headers={
"accept": "application/json",
"content-type": "application/json",
"authorization": f"Bearer {self.config.access_token}",
},
)
if response.ok:
return response
else:
raise_api_exception(response.status_code, response.json())
def _patch_api_request(self, payload: Any, endpoint_url: str) -> requests.Response:
response = requests.patch(
url=endpoint_url,
json=payload,
headers={
"accept": "application/json",
"content-type": "application/json",
"authorization": f"Bearer {self.config.access_token}",
},
)
if response.ok:
return response
else:
raise_api_exception(response.status_code, response.json())
def _get_api_request(self, endpoint_url: str) -> requests.Response:
response = requests.get(
url=endpoint_url,
headers={
"accept": "application/json",
"authorization": f"Bearer {self.config.access_token}",
},
)
if response.ok:
return response
else:
raise_api_exception(response.status_code, response.json())
def _delete_api_request(self, endpoint_url: str) -> None:
response = requests.delete(
url=endpoint_url,
headers={
"accept": "application/json",
"authorization": f"Bearer {self.config.access_token}",
},
)
if not response.ok:
raise_api_exception(response.status_code, response.json())