Skip to content

Commit dcb5ddc

Browse files
SK-1874: Support for the combination of tokens and redaction type in the Detokenize API. (#156)
* SK-1874: Detokenize API support
1 parent e923868 commit dcb5ddc

File tree

6 files changed

+44
-19
lines changed

6 files changed

+44
-19
lines changed

samples/vault_api/detokenize_records.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,20 @@ def perform_detokenization():
5252
)
5353

5454
# Step 4: Prepare Detokenization Data
55-
detokenize_data = ['token1', 'token2', 'token3'] # Tokens to be detokenized
56-
redaction_type = RedactionType.REDACTED
55+
detokenize_data = [
56+
{
57+
'token': '<TOKEN1>', # Token to be detokenized
58+
'redaction': RedactionType.REDACTED
59+
},
60+
{
61+
'token': '<TOKEN2>', # Token to be detokenized
62+
'redaction': RedactionType.MASKED
63+
}
64+
]
5765

5866
# Create Detokenize Request
5967
detokenize_request = DetokenizeRequest(
60-
tokens=detokenize_data,
61-
redaction_type=redaction_type,
68+
data=detokenize_data,
6269
continue_on_error=True # Continue processing on errors
6370
)
6471

skyflow/utils/_skyflow_messages.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class Error(Enum):
101101
INVOKE_CONNECTION_FAILED = f"{error_prefix} Invoke Connection operation failed."
102102

103103
INVALID_IDS_TYPE = f"{error_prefix} Validation error. 'ids' has a value of type {{}}. Specify 'ids' as list."
104-
INVALID_REDACTION_TYPE = f"{error_prefix} Validation error. 'redaction' has a value of type {{}}. Specify 'redaction' as type Skyflow.Redaction."
104+
INVALID_REDACTION_TYPE = f"{error_prefix} Validation error. 'redaction' has a value of type {{}}. Specify 'redaction' as type Skyflow.RedactionType."
105105
INVALID_COLUMN_NAME = f"{error_prefix} Validation error. 'column' has a value of type {{}}. Specify 'column' as a string."
106106
INVALID_COLUMN_VALUE = f"{error_prefix} Validation error. columnValues key has a value of type {{}}. Specify columnValues key as list."
107107
INVALID_FIELDS_VALUE = f"{error_prefix} Validation error. fields key has a value of type{{}}. Specify fields key as list."
@@ -117,8 +117,10 @@ class Error(Enum):
117117
UPDATE_FIELD_KEY_ERROR = f"{error_prefix} Validation error. Fields are empty in an update payload. Specify at least one field."
118118
INVALID_FIELDS_TYPE = f"{error_prefix} Validation error. The 'data' key has a value of type {{}}. Specify 'data' as a dictionary."
119119
IDS_KEY_ERROR = f"{error_prefix} Validation error. 'ids' key is missing from the payload. Specify an 'ids' key."
120-
INVALID_TOKENS_LIST_VALUE = f"{error_prefix} Validation error. The 'tokens' key has a value of type {{}}. Specify 'tokens' as a list."
120+
INVALID_TOKENS_LIST_VALUE = f"{error_prefix} Validation error. The 'data' field is invalid. Specify 'data' as a list of dictionaries containing 'token' and 'redaction'."
121+
INVALID_DATA_FOR_DETOKENIZE = f"{error_prefix}"
121122
EMPTY_TOKENS_LIST_VALUE = f"{error_prefix} Validation error. Tokens are empty in detokenize payload. Specify at lease one token"
123+
INVALID_TOKEN_TYPE = f"{ERROR}: [{error_prefix}] Invalid {{}} request. Tokens should be of type string."
122124

123125
INVALID_TOKENIZE_PARAMETERS = f"{error_prefix} Validation error. The 'values' key has a value of type {{}}. Specify 'tokenize_parameters' as a list."
124126
EMPTY_TOKENIZE_PARAMETERS = f"{error_prefix} Validation error. Tokenize values are empty in tokenize payload. Specify at least one parameter."

skyflow/utils/validations/_validations.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -502,19 +502,28 @@ def validate_update_request(logger, request):
502502
invalid_input_error_code)
503503

504504
def validate_detokenize_request(logger, request):
505-
if not isinstance(request.redaction_type, RedactionType):
506-
raise SkyflowError(SkyflowMessages.Error.INVALID_REDACTION_TYPE.value.format(type(request.redaction_type)), invalid_input_error_code)
507-
508505
if not isinstance(request.continue_on_error, bool):
509506
raise SkyflowError(SkyflowMessages.Error.INVALID_CONTINUE_ON_ERROR_TYPE.value, invalid_input_error_code)
510507

511-
if not len(request.tokens):
508+
if not isinstance(request.data, list):
509+
raise SkyflowError(SkyflowMessages.Error.INVALID_TOKENS_LIST_VALUE.value(type(request.data)), invalid_input_error_code)
510+
511+
if not len(request.data):
512512
log_error_log(SkyflowMessages.ErrorLogs.TOKENS_REQUIRED.value.format("DETOKENIZE"), logger = logger)
513513
log_error_log(SkyflowMessages.ErrorLogs.EMPTY_TOKENS.value.format("DETOKENIZE"), logger = logger)
514514
raise SkyflowError(SkyflowMessages.Error.EMPTY_TOKENS_LIST_VALUE.value, invalid_input_error_code)
515515

516-
if not isinstance(request.tokens, list):
517-
raise SkyflowError(SkyflowMessages.Error.INVALID_TOKENS_LIST_VALUE.value(type(request.tokens)), invalid_input_error_code)
516+
for item in request.data:
517+
if 'token' not in item or 'redaction' not in item:
518+
raise SkyflowError(SkyflowMessages.Error.INVALID_TOKENS_LIST_VALUE.value(type(request.data)), invalid_input_error_code)
519+
token = item.get('token')
520+
redaction = item.get('redaction')
521+
522+
if not isinstance(token, str) or not token:
523+
raise SkyflowError(SkyflowMessages.Error.INVALID_TOKEN_TYPE.value.format("DETOKENIZE"), invalid_input_error_code)
524+
525+
if not isinstance(redaction, RedactionType) or not redaction:
526+
raise SkyflowError(SkyflowMessages.Error.INVALID_REDACTION_TYPE.value.format(type(redaction)), invalid_input_error_code)
518527

519528
def validate_tokenize_request(logger, request):
520529
parameters = request.values

skyflow/vault/controller/_vault.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,8 @@ def detokenize(self, request: DetokenizeRequest):
230230
log_info(SkyflowMessages.Info.DETOKENIZE_REQUEST_RESOLVED.value, self.__vault_client.get_logger())
231231
self.__initialize()
232232
tokens_list = [
233-
V1DetokenizeRecordRequest(token=token, redaction=request.redaction_type.value)
234-
for token in request.tokens
233+
V1DetokenizeRecordRequest(token=item.get('token'), redaction=item.get('redaction').value)
234+
for item in request.data
235235
]
236236
payload = V1DetokenizePayload(detokenization_parameters=tokens_list, continue_on_error=request.continue_on_error)
237237
tokens_api = self.__vault_client.get_tokens_api()
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from skyflow.utils.enums.redaction_type import RedactionType
22

33
class DetokenizeRequest:
4-
def __init__(self, tokens, redaction_type = RedactionType.PLAIN_TEXT, continue_on_error = False):
5-
self.tokens = tokens
6-
self.redaction_type = redaction_type
4+
def __init__(self, data, continue_on_error = False):
5+
self.data = data
76
self.continue_on_error = continue_on_error

tests/vault/controller/test__vault.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,8 +455,16 @@ def test_query_successful(self, mock_parse_response, mock_validate):
455455
@patch("skyflow.vault.controller._vault.parse_detokenize_response")
456456
def test_detokenize_successful(self, mock_parse_response, mock_validate):
457457
request = DetokenizeRequest(
458-
tokens=["token1", "token2"],
459-
redaction_type=RedactionType.PLAIN_TEXT,
458+
data=[
459+
{
460+
'token': 'token1',
461+
'redaction': RedactionType.PLAIN_TEXT
462+
},
463+
{
464+
'token': 'token2',
465+
'redaction': RedactionType.PLAIN_TEXT
466+
}
467+
],
460468
continue_on_error=False
461469
)
462470

0 commit comments

Comments
 (0)