Repository navigation
Expand file tree
/
Copy pathcustom_header_example.py
More file actions
67 lines (51 loc) · 1.71 KB
/
Copy pathcustom_header_example.py
File metadata and controls
67 lines (51 loc) · 1.71 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
import uuid
from skyflow.error import SkyflowError
from skyflow import Env
from skyflow import Skyflow, LogLevel
from skyflow.vault.data import (
InsertRequest,
InsertRequestRecord,
InsertOptions,
UpsertOptions,
CustomHeaderKey,
)
def make_request_id():
request_id = str(uuid.uuid4())
print('request id =>', request_id)
return request_id
def add_custom_headers(context):
context.add_header(CustomHeaderKey.REQUEST_ID_HEADER, make_request_id())
def perform_insert_with_custom_headers():
try:
credentials = {
'token': '<BEARER_TOKEN>',
}
vault_config = {
'vault_id': '<VAULT_ID>',
'cluster_id': '<CLUSTER_ID>',
'env': Env.DEV,
'credentials': credentials,
}
skyflow_client = (
Skyflow.builder()
.add_vault_config(vault_config)
.set_log_level(LogLevel.DEBUG)
.build()
)
insert_request = InsertRequest(
table_name='<TABLE_NAME>',
upsert=UpsertOptions(unique_columns=['<UPSERT_COLUMN_NAME>']),
records=[InsertRequestRecord(data={'<YOUR_COLUMN_NAME>': '<YOUR_VALUE>'})],
)
options = InsertOptions(interceptor=add_custom_headers)
response = skyflow_client.vault(vault_config.get('vault_id')).insert(insert_request, options)
print('Records: ', response.records)
except SkyflowError as error:
print('Skyflow Specific Error: ', {
'code': error.http_code,
'message': error.message,
'details': error.details,
})
except Exception as error:
print('Unexpected Error:', error)
perform_insert_with_custom_headers()