-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #72 from skyflowapi/release/23.1.2.1
SK-243 added update and get interfaces
- Loading branch information
Showing
13 changed files
with
723 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
''' | ||
Copyright (c) 2022 Skyflow, Inc. | ||
''' | ||
from skyflow.errors import SkyflowError | ||
from skyflow.service_account import generate_bearer_token, is_expired | ||
from skyflow.vault import Client, Configuration, RedactionType | ||
|
||
|
||
# cache token for reuse | ||
bearerToken = '' | ||
|
||
|
||
def token_provider(): | ||
global bearerToken | ||
if is_expired(bearerToken): | ||
bearerToken, _ = generate_bearer_token('<YOUR_CREDENTIALS_FILE_PATH>') | ||
return bearerToken | ||
|
||
|
||
try: | ||
config = Configuration( | ||
'<YOUR_VAULT_ID>', '<YOUR_VAULT_URL>', token_provider) | ||
client = Client(config) | ||
|
||
data = {"records": [ | ||
{ | ||
"ids": ["<SKYFLOW_ID1>", "<SKYFLOW_ID2>", "<SKYFLOW_ID3>"], | ||
"table": "<TABLE_NAME>", | ||
"redaction": RedactionType.PLAIN_TEXT | ||
}, | ||
#To get records using unique column name and values. | ||
{ | ||
"redaction" : "<REDACTION_TYPE>", | ||
"table": "<TABLE_NAME>", | ||
"columnName": "<UNIQUE_COLUMN_NAME>", | ||
"columnValues": "[<COLUMN_VALUE_1>,<COLUMN_VALUE_2>]", | ||
} | ||
]} | ||
|
||
response = client.get(data) | ||
print('Response:', response) | ||
except SkyflowError as e: | ||
print('Error Occurred:', e) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
''' | ||
Copyright (c) 2022 Skyflow, Inc. | ||
''' | ||
from skyflow.errors import SkyflowError | ||
from skyflow.service_account import generate_bearer_token, is_expired | ||
from skyflow.vault import Client, UpdateOptions, Configuration | ||
|
||
# cache token for reuse | ||
bearerToken = '' | ||
|
||
def token_provider(): | ||
global bearerToken | ||
if is_expired(bearerToken): | ||
bearerToken, _ = generate_bearer_token('<YOUR_CREDENTIALS_FILE_PATH>') | ||
return bearerToken | ||
|
||
|
||
try: | ||
config = Configuration( | ||
'<YOUR_VAULT_ID>', '<YOUR_VAULT_URL>', token_provider) | ||
client = Client(config) | ||
|
||
options = UpdateOptions(True) | ||
|
||
data = { | ||
"records": [ | ||
{ | ||
"id": "<SKYFLOW_ID>", | ||
"table": "<TABLE_NAME>", | ||
"fields": { | ||
"<FIELDNAME>": "<VALUE>" | ||
} | ||
} | ||
] | ||
} | ||
response = client.update(data, options=options) | ||
print('Response:', response) | ||
except SkyflowError as e: | ||
print('Error Occurred:', e) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
''' | ||
Copyright (c) 2022 Skyflow, Inc. | ||
''' | ||
from skyflow.errors._skyflow_errors import SkyflowError, SkyflowErrorCodes, SkyflowErrorMessages | ||
import asyncio | ||
from aiohttp import ClientSession | ||
from ._config import RedactionType | ||
from skyflow._utils import InterfaceName | ||
from ._get_by_id import get | ||
|
||
interface = InterfaceName.GET.value | ||
|
||
def getGetRequestBody(data): | ||
ids = None | ||
if "ids" in data: | ||
ids = data["ids"] | ||
if not isinstance(ids, list): | ||
idsType = str(type(ids)) | ||
raise SkyflowError(SkyflowErrorCodes.INVALID_INPUT, | ||
SkyflowErrorMessages.INVALID_IDS_TYPE.value % (idsType), interface=interface) | ||
for id in ids: | ||
if not isinstance(id, str): | ||
idType = str(type(id)) | ||
raise SkyflowError(SkyflowErrorCodes.INVALID_INPUT, SkyflowErrorMessages.INVALID_ID_TYPE.value % ( | ||
idType), interface=interface) | ||
try: | ||
table = data["table"] | ||
except KeyError: | ||
raise SkyflowError(SkyflowErrorCodes.INVALID_INPUT, | ||
SkyflowErrorMessages.TABLE_KEY_ERROR, interface=interface) | ||
if not isinstance(table, str): | ||
tableType = str(type(table)) | ||
raise SkyflowError(SkyflowErrorCodes.INVALID_INPUT, SkyflowErrorMessages.INVALID_TABLE_TYPE.value % ( | ||
tableType), interface=interface) | ||
try: | ||
redaction = data["redaction"] | ||
except KeyError: | ||
raise SkyflowError(SkyflowErrorCodes.INVALID_INPUT, | ||
SkyflowErrorMessages.REDACTION_KEY_ERROR, interface=interface) | ||
if not isinstance(redaction, RedactionType): | ||
redactionType = str(type(redaction)) | ||
raise SkyflowError(SkyflowErrorCodes.INVALID_INPUT, SkyflowErrorMessages.INVALID_REDACTION_TYPE.value % ( | ||
redactionType), interface=interface) | ||
|
||
columnName = None | ||
if "columnName" in data: | ||
columnName = data["columnName"] | ||
if not isinstance(columnName, str): | ||
columnNameType = str(type(columnName)) | ||
raise SkyflowError(SkyflowErrorCodes.INVALID_INPUT, SkyflowErrorMessages.INVALID_COLUMN_NAME.value % ( | ||
columnNameType), interface=interface) | ||
|
||
columnValues = None | ||
if columnName is not None and "columnValues" in data: | ||
columnValues = data["columnValues"] | ||
if not isinstance(columnValues, list): | ||
columnValuesType= str(type(columnValues)) | ||
raise SkyflowError(SkyflowErrorCodes.INVALID_INPUT, SkyflowErrorMessages.INVALID_COLUMN_VALUE.value % ( | ||
columnValuesType), interface=interface) | ||
|
||
if(ids is None and (columnName is None or columnValues is None)): | ||
raise SkyflowError(SkyflowErrorCodes.INVALID_INPUT, | ||
SkyflowErrorMessages.UNIQUE_COLUMN_OR_IDS_KEY_ERROR.value, interface= interface) | ||
return ids, table, redaction.value, columnName, columnValues | ||
|
||
|
||
async def sendGetRequests(data, url, token): | ||
tasks = [] | ||
try: | ||
records = data["records"] | ||
except KeyError: | ||
raise SkyflowError(SkyflowErrorCodes.INVALID_INPUT, | ||
SkyflowErrorMessages.RECORDS_KEY_ERROR, interface=interface) | ||
if not isinstance(records, list): | ||
recordsType = str(type(records)) | ||
raise SkyflowError(SkyflowErrorCodes.INVALID_INPUT, SkyflowErrorMessages.INVALID_RECORDS_TYPE.value % ( | ||
recordsType), interface=interface) | ||
|
||
validatedRecords = [] | ||
for record in records: | ||
ids, table, redaction, columnName, columnValues = getGetRequestBody(record) | ||
validatedRecords.append((ids, table, redaction, columnName, columnValues)) | ||
async with ClientSession() as session: | ||
for record in validatedRecords: | ||
headers = { | ||
"Authorization": "Bearer " + token | ||
} | ||
params = {"redaction": redaction} | ||
if ids is not None: | ||
params["skyflow_ids"] = ids | ||
if columnName is not None: | ||
params["column_name"] = columnName | ||
params["column_values"] = columnValues | ||
task = asyncio.ensure_future( | ||
get(url, headers, params, session, record[1])) | ||
tasks.append(task) | ||
await asyncio.gather(*tasks) | ||
await session.close() | ||
return tasks |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.