Skip to content

Commit

Permalink
Merge pull request #122 from skyflowapi/SK-704/add-tokens-options-get
Browse files Browse the repository at this point in the history
SK-728 add tokens and encode options to the get interface.
  • Loading branch information
yaswanth-pula-skyflow authored Oct 19, 2023
2 parents 6bd0726 + 53d8100 commit 60de4ae
Show file tree
Hide file tree
Showing 8 changed files with 329 additions and 21 deletions.
8 changes: 5 additions & 3 deletions src/vault-api/Controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
IDeleteInput,
IDeleteOptions,
IGetInput,
IGetOptions,
IInsertOptions,
IUpdateInput,
IUpdateOptions,
Expand Down Expand Up @@ -151,20 +152,21 @@ class Controller {
});
}

get(getInput: IGetInput) {
get(getInput: IGetInput,options:IGetOptions={}) {
return new Promise((resolve, reject) => {
try {
validateInitConfig(this.#client.config)
printLog(logs.infoLogs.VALIDATE_GET_INPUT, MessageType.LOG);
validateGetInput(getInput);
validateGetInput(getInput,options);
printLog(parameterizedString(logs.infoLogs.EMIT_REQUEST,
TYPES.GET),
MessageType.LOG);
this.getToken().then((res) => {
fetchRecordsBySkyflowID(
getInput.records,
this.#client,
res
res,
options
).then(
(resolvedResult) => {
printLog(logs.infoLogs.GET_BY_SKYFLOWID_RESOLVED, MessageType.LOG);
Expand Down
5 changes: 3 additions & 2 deletions src/vault-api/Skyflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
IUpdateInput,
IUpdateOptions,
IGetInput,
IGetOptions,
IDeleteInput,
IDeleteOptions,
} from './utils/common';
Expand Down Expand Up @@ -77,10 +78,10 @@ class Skyflow {
return this.#Controller.getById(getByIdInput);
}

get(getInput: IGetInput) {
get(getInput: IGetInput,options?:IGetOptions) {
printLog(logs.infoLogs.GET_CALL_TRIGGERED,
MessageType.LOG);
return this.#Controller.get(getInput);
return this.#Controller.get(getInput,options);
}

invokeConnection(config: IConnectionConfig) {
Expand Down
31 changes: 24 additions & 7 deletions src/vault-api/core/Reveal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import Client from '../client';
import SkyflowError from '../libs/SkyflowError';
import {
ISkyflowIdRecord, IRevealRecord, IRevealResponseType, IUpdateRecord, IUpdateOptions,RedactionType
ISkyflowIdRecord, IRevealRecord, IRevealResponseType, IUpdateRecord, IUpdateOptions,RedactionType, IGetOptions
} from '../utils/common';
import 'core-js/modules/es.promise.all-settled';
interface IApiSuccessResponse {
Expand Down Expand Up @@ -56,6 +56,7 @@ const getSkyflowIdRecordsFromVault = (
skyflowIdRecord: ISkyflowIdRecord,
client: Client,
authToken: string,
options?: IGetOptions
) => {
let paramList: string = '';

Expand All @@ -64,11 +65,26 @@ const getSkyflowIdRecordsFromVault = (
paramList += `skyflow_ids=${skyflowId}&`;
});

skyflowIdRecord.columnValues?.forEach((column) => {
paramList += `column_name=${skyflowIdRecord.columnName}&column_values=${column}&`;
})
if(options && Object.prototype.hasOwnProperty.call(options, 'encodeURI') && options?.encodeURI === true) {
skyflowIdRecord.columnValues?.forEach((column) => {
var encode_column_value = encodeURIComponent(column)
paramList += `column_name=${skyflowIdRecord.columnName}&column_values=${encode_column_value}&`;
});
} else {
skyflowIdRecord.columnValues?.forEach((column) => {
paramList += `column_name=${skyflowIdRecord.columnName}&column_values=${column}&`;
});
}

const vaultEndPointurl: string = `${client.config.vaultURL}/v1/vaults/${client.config.vaultID}/${skyflowIdRecord.table}?${paramList}redaction=${skyflowIdRecord.redaction}`;
if(options && Object.prototype.hasOwnProperty.call(options,'tokens')){
paramList += `tokenization=${options.tokens}&`
}

if(skyflowIdRecord?.redaction){
paramList += `redaction=${skyflowIdRecord.redaction}`
}

const vaultEndPointurl: string = `${client.config.vaultURL}/v1/vaults/${client.config.vaultID}/${skyflowIdRecord.table}?${paramList}`;

return client.request({
requestMethod: 'GET',
Expand Down Expand Up @@ -158,12 +174,13 @@ export const fetchRecordsByTokenId = (
export const fetchRecordsBySkyflowID = async (
skyflowIdRecords: ISkyflowIdRecord[],
client: Client,
authToken: string
authToken: string,
options?: IGetOptions
) => new Promise((rootResolve, rootReject) => {
let vaultResponseSet: Promise<any>[];
vaultResponseSet = skyflowIdRecords.map(
(skyflowIdRecord) => new Promise((resolve, reject) => {
getSkyflowIdRecordsFromVault(skyflowIdRecord, client, authToken as string)
getSkyflowIdRecordsFromVault(skyflowIdRecord, client, authToken as string,options)
.then(
(resolvedResult: any) => {
const response: any[] = [];
Expand Down
8 changes: 6 additions & 2 deletions src/vault-api/utils/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export interface IDetokenizeInput {

export interface ISkyflowIdRecord {
ids?: string[];
redaction: RedactionType;
redaction?: RedactionType;
table: string;
columnName?: string;
columnValues?: string[];
Expand Down Expand Up @@ -128,6 +128,10 @@ export interface IUpdateOptions{
tokens: boolean
}

export interface IGetOptions{
tokens?: boolean
encodeURI?: boolean
}
export interface IDeleteRecord {
id: string;
table: string;
Expand All @@ -141,4 +145,4 @@ export interface IDeleteOptions {

}

export const SDK_METRICS_HEADER_KEY = "sky-metadata";
export const SDK_METRICS_HEADER_KEY = "sky-metadata";
19 changes: 17 additions & 2 deletions src/vault-api/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,22 @@ const SKYFLOW_ERROR_CODE = {
code: 400,
description: logs.errorLogs.INVALID_GET_BY_ID_INPUT,
},
INVALID_TOKENS_IN_GET: {
code: 400,
description: logs.errorLogs.INVALID_TOKENS_IN_GET,
},
TOKENS_GET_COLUMN_NOT_SUPPORTED: {
code: 400,
description: logs.errorLogs.TOKENS_GET_COLUMN_NOT_SUPPORTED,
},
REDACTION_WITH_TOKENS_NOT_SUPPORTED: {
code: 400,
description: logs.errorLogs.REDACTION_WITH_TOKENS_NOT_SUPPORTED,
},
INVALID_ENCODE_URI_IN_GET: {
code: 400,
description: logs.errorLogs.INVALID_ENCODE_URI_IN_GET,
},
MISSING_ID_IN_DELETE: {
code: 400,
description: logs.errorLogs.MISSING_ID_IN_DELETE,
Expand All @@ -149,13 +165,12 @@ const SKYFLOW_ERROR_CODE = {
},
INVLAID_DELETE_RECORDS_INPUT: {
code: 400,
description: logs.errorLogs.INVLAID_DELETE_RECORDS_INPUT
description: logs.errorLogs.INVLAID_DELETE_RECORDS_INPUT,
},
DETOKENIZE_INVALID_REDACTION_TYPE:{
code: 400,
description: logs.errorLogs.DETOKENIZE_INVALID_REDACTION_TYPE,
}

};

export default SKYFLOW_ERROR_CODE;
4 changes: 4 additions & 0 deletions src/vault-api/utils/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const logs = {
INVALID_VAULTURL_IN_INIT: 'Interface: init - Invalid client credentials. Expecting https://XYZ for vaultURL',
GET_BEARER_TOKEN_IS_REQUIRED: 'Interface: init - Invalid client credentials. getBearerToken is required.',
BEARER_TOKEN_REJECTED: 'Interface: init - GetBearerToken promise got rejected.',
INVALID_ENCODE_URI_IN_GET: 'Interface: get method - Invalid encodeURI type in get.',
INVALID_BEARER_TOKEN: 'Bearer token is invalid or expired.',
INVALID_VAULT_ID: 'Vault Id is invalid or cannot be found.',
EMPTY_VAULT_ID: 'VaultID is empty.',
Expand Down Expand Up @@ -128,6 +129,9 @@ const logs = {
INVALID_UPDATE_INPUT: 'Interface: update method - Invalid argument , object with records key is required.',
INVALID_RECORDS_UPDATE_INPUT: 'Interface: update method - Invalid records type, records should be an array of objects.',
INVALID_GET_BY_ID_INPUT: 'Interface: getById method - columnName or columnValues cannot be passed, use get method instead.',
INVALID_TOKENS_IN_GET: 'Interface: get method - Invalid tokens in options. tokens of type boolean is required.',
TOKENS_GET_COLUMN_NOT_SUPPORTED: 'Interface: get method - column_name or column_values cannot be used with tokens in options.',
REDACTION_WITH_TOKENS_NOT_SUPPORTED: 'Interface: get method - redaction cannot be used when tokens are true in options.',
INVALID_DELETE_INPUT: 'Interface: delete method - Invalid argument , object with records key is required.',
INVLAID_DELETE_RECORDS_INPUT: 'Interface: delete method - Invalid records type, records should be an array of objects.',
MISSING_ID_IN_DELETE: 'Interface: delete method - id key is required in records object at index %s1',
Expand Down
28 changes: 23 additions & 5 deletions src/vault-api/utils/validators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import SkyflowError from '../../libs/SkyflowError';
import { ISkyflow } from '../../Skyflow';
import {
IInsertRecordInput, IDetokenizeInput, RedactionType, IGetByIdInput, IConnectionConfig, RequestMethod, IUpdateInput, IGetInput, IDeleteInput, IDeleteOptions,
IInsertRecordInput, IDetokenizeInput, RedactionType, IGetByIdInput, IConnectionConfig, RequestMethod, IUpdateInput, IGetInput, IGetOptions, IDeleteInput, IDeleteOptions,
} from '../common';
import SKYFLOW_ERROR_CODE from '../constants';

Expand Down Expand Up @@ -93,7 +93,7 @@ export const validateGetByIdInput = (getByIdInput: IGetByIdInput) => {
});
};

export const validateGetInput = (getInput: IGetInput) => {
export const validateGetInput = (getInput: IGetInput,options?:IGetOptions) => {
if (!Object.prototype.hasOwnProperty.call(getInput, 'records')) {
throw new SkyflowError(SKYFLOW_ERROR_CODE.MISSING_RECORDS);
}
Expand All @@ -102,6 +102,14 @@ export const validateGetInput = (getInput: IGetInput) => {
throw new SkyflowError(SKYFLOW_ERROR_CODE.EMPTY_RECORDS);
}

if (Object.prototype.hasOwnProperty.call(options, 'tokens') && (typeof options?.tokens !== 'boolean')) {
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_TOKENS_IN_GET);
}

if (Object.prototype.hasOwnProperty.call(options, 'encodeURI') && (typeof options?.encodeURI !== 'boolean')) {
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_ENCODE_URI_IN_GET);
}

records.forEach((record) => {
if (Object.keys(record).length === 0) {
throw new SkyflowError(SKYFLOW_ERROR_CODE.EMPTY_RECORDS);
Expand All @@ -114,11 +122,14 @@ export const validateGetInput = (getInput: IGetInput) => {
});

const recordRedaction = record.redaction;
if (!recordRedaction) throw new SkyflowError(SKYFLOW_ERROR_CODE.MISSING_REDACTION);
if (!Object.values(RedactionType).includes(recordRedaction)) {
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_REDACTION_TYPE);
if(!(options && Object.prototype.hasOwnProperty.call(options, 'tokens') && options?.tokens === true)){
if (!recordRedaction) throw new SkyflowError(SKYFLOW_ERROR_CODE.MISSING_REDACTION);
if (!Object.values(RedactionType).includes(recordRedaction)) {
throw new SkyflowError(SKYFLOW_ERROR_CODE.INVALID_REDACTION_TYPE);
}
}


const recordTable = record.table;
if (!Object.prototype.hasOwnProperty.call(record, 'table')) { throw new SkyflowError(SKYFLOW_ERROR_CODE.MISSING_TABLE); }

Expand All @@ -143,6 +154,13 @@ export const validateGetInput = (getInput: IGetInput) => {
if (eachColumnValue === '') throw new SkyflowError(SKYFLOW_ERROR_CODE.EMPTY_COLUMN_VALUE);
});
}
if(options && Object.prototype.hasOwnProperty.call(options, 'tokens') && options?.tokens === true){
if(columnName || columnValues)
throw new SkyflowError(SKYFLOW_ERROR_CODE.TOKENS_GET_COLUMN_NOT_SUPPORTED)

if(recordRedaction)
throw new SkyflowError(SKYFLOW_ERROR_CODE.REDACTION_WITH_TOKENS_NOT_SUPPORTED)
}
});
};

Expand Down
Loading

0 comments on commit 60de4ae

Please sign in to comment.