Skip to content

Commit

Permalink
SK-1317 Add continue on error in batch Insert
Browse files Browse the repository at this point in the history
- Added requestId in metadata of API response.
- Removed requestId from responses where not required.
- Fixed failing Unit Tests as a consequence of changes.
  • Loading branch information
skyflow-vivek committed Jan 11, 2024
1 parent 3bfb43d commit d3f1288
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 41 deletions.
4 changes: 2 additions & 2 deletions src/vault-api/Controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,8 @@ class Controller {
body: config.requestBody,
headers: { 'x-skyflow-authorization': res, 'content-type': ContentType.APPLICATIONORJSON,...toLowerKeys(config.requestHeader) },
});
invokeRequest.then((response) => {
rootResolve(response);
invokeRequest.then((response: any) => {
rootResolve(response.data);
}).catch((err) => {
rootReject({ errors: [err] });
});
Expand Down
5 changes: 3 additions & 2 deletions src/vault-api/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,9 @@ class Client {
headers: this.getHeaders(data,headerKeys)
}
).then((res)=> {
res.data['requestId'] = res.headers['x-request-id']
resolve(res.data)
// res.data['requestId'] = res.headers['x-request-id']
let requestId = res.headers['x-request-id']
resolve({data: res.data, metadata: {requestId}})
}).catch((err)=> {
this.failureResponse(err).catch((err)=>reject(err))
})
Expand Down
16 changes: 9 additions & 7 deletions src/vault-api/core/Collect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const constructInsertRecordResponse = (
if (options.continueOnError) {
const successObjects: any = [];
const failureObjects: any= [];
responseBody.responses
responseBody.data.responses
.forEach((response, index) => {
const status = response['Status']
const body = response['Body']
Expand All @@ -65,9 +65,11 @@ export const constructInsertRecordResponse = (
}
} else {
failureObjects.push({
code: status,
ddescription: `${body['error']} - requestId: ${responseBody.requestId}`,
request_index: index,
error: {
code: status,
description: `${body['error']} - requestId: ${responseBody.metadata.requestId}`,
request_index: index,
}
})
}
})
Expand All @@ -81,9 +83,9 @@ export const constructInsertRecordResponse = (
return finalResponse;
} else if (options.tokens) {
return {
records: responseBody.responses
records: responseBody.data.responses
.map((res, index) => {
const skyflowId = responseBody.responses[index].records[0].skyflow_id;
const skyflowId = res.records[0].skyflow_id;
const tokens = res.records[0].tokens;
return {
table: records[index].table,
Expand All @@ -97,7 +99,7 @@ export const constructInsertRecordResponse = (
};
}
return {
records: responseBody.responses.map((res, index) => ({
records: responseBody.data.responses.map((res, index) => ({
table: records[index].table,
skyflow_id: res.records[0].skyflow_id,
"request_index": index
Expand Down
2 changes: 1 addition & 1 deletion src/vault-api/core/Delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const deleteRecordsBySkyflowID = (
)
.then(
(response: any) => {
deleteResponse.push(response);
deleteResponse.push(response.data);
},
(cause: any) => {
deleteResponse.push(formatForPureJsFailure(cause, deleteRecord.id));
Expand Down
8 changes: 4 additions & 4 deletions src/vault-api/core/Reveal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ export const fetchRecordsByTokenId = (
const apiResponse: any = [];
getTokenRecordsFromVault(tokenRecord, client, authToken as string)
.then(
(response: IApiSuccessResponse) => {
const fieldsData = formatForPureJsSuccess(response);
(response: any) => {
const fieldsData = formatForPureJsSuccess(response.data);
apiResponse.push(...fieldsData);
},
(cause: any) => {
Expand Down Expand Up @@ -184,7 +184,7 @@ export const fetchRecordsBySkyflowID = async (
.then(
(resolvedResult: any) => {
const response: any[] = [];
const recordsData: any[] = resolvedResult.records;
const recordsData: any[] = resolvedResult.data.records;
recordsData.forEach((fieldData) => {
const id = fieldData.fields.skyflow_id;
const currentRecord = {
Expand Down Expand Up @@ -276,7 +276,7 @@ export const updateRecordsBySkyflowID = (
updateRecordInVault(updateRecord, options, client, authToken as string)
.then(
(response: any) => {
updateResponse.push(formatUpdateSuccessResponse(response));
updateResponse.push(formatUpdateSuccessResponse(response.data));
},
(cause: any) => {
updateResponse.push(formatUpdateFailureResponse(cause,updateRecord.id));
Expand Down
5 changes: 4 additions & 1 deletion test/vault-api/Client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ describe("Client Class",()=>{
data: data,
headers: headers,
});
expect(response).toEqual({ message: "Success", requestId: "22r5-dfbf-3543" });
expect(response).toEqual({
data: {message: "Success"},
metadata: {requestId: "22r5-dfbf-3543"}
});
});

test("should return an error if the request to client fails", async () => {
Expand Down
52 changes: 28 additions & 24 deletions test/vault-api/Skyflow.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ describe('skyflow insert', () => {
__esModule: true,
isTokenValid:jest.fn(()=>true),
}));
const clientReq = jest.fn(() => Promise.resolve(insertResponse));
const clientReq = jest.fn(() => Promise.resolve({data:insertResponse}));
const mockClient = {
config: skyflowConfig,
request: clientReq,
Expand Down Expand Up @@ -202,7 +202,7 @@ describe('skyflow insert', () => {
test('insert success without tokens', () => {


const clientReq = jest.fn(() => Promise.resolve(insertResponseWithoutTokens));
const clientReq = jest.fn(() => Promise.resolve({data:insertResponseWithoutTokens}));
const mockClient = {
config: skyflowConfig,
request: clientReq,
Expand Down Expand Up @@ -265,7 +265,7 @@ describe('skyflow insert', () => {
__esModule: true,
isTokenValid:jest.fn(()=>true),
}));
const clientReq = jest.fn(() => Promise.resolve(insertResponse));
const clientReq = jest.fn(() => Promise.resolve({data:insertResponse}));
const mockClient = {
config: skyflowConfig,
request: clientReq,
Expand Down Expand Up @@ -356,7 +356,9 @@ describe('skyflow insert', () => {
__esModule: true,
isTokenValid:jest.fn(() => true),
}));
const clientReq = jest.fn(() => Promise.resolve(insertResponseCOEWithTokens));
const clientReq = jest.fn(() => Promise.resolve({
data: insertResponseCOEWithTokens, metadata: {requestId: 123}
}));
const mockClient = {
config: skyflowConfig,
request: clientReq,
Expand Down Expand Up @@ -397,7 +399,9 @@ describe('skyflow insert', () => {
__esModule: true,
isTokenValid:jest.fn(() => true),
}));
const clientReq = jest.fn(() => Promise.resolve(insertResponseCOEWithoutTokens));
const clientReq = jest.fn(() => Promise.resolve({
data: insertResponseCOEWithoutTokens, metadata: {requestId: 123}
}));
const mockClient = {
config: skyflowConfig,
request: clientReq,
Expand Down Expand Up @@ -481,7 +485,7 @@ describe('skyflow detokenize', () => {
__esModule: true,
isTokenValid:jest.fn(()=>true),
}));
const clientReq = jest.fn(() => Promise.resolve(detokenizeRes));
const clientReq = jest.fn(() => Promise.resolve({data:detokenizeRes}));
const mockClient = {
config: skyflowConfig,
request: clientReq,
Expand Down Expand Up @@ -787,7 +791,7 @@ describe('skyflow getById', () => {
__esModule: true,
isTokenValid:jest.fn(()=>true),
}));
const clientReq = jest.fn(() => Promise.resolve(getByIdRes));
const clientReq = jest.fn(() => Promise.resolve({data:getByIdRes}));
const mockClient = {
config: skyflowConfig,
request: clientReq,
Expand Down Expand Up @@ -938,7 +942,7 @@ describe('skyflow get method', () => {
__esModule: true,
isTokenValid:jest.fn(()=>true),
}));
const clientReq = jest.fn(() => Promise.resolve(getByIdRes));
const clientReq = jest.fn(() => Promise.resolve({data:getByIdRes}));
const mockClient = {
config: skyflowConfig,
request: clientReq,
Expand Down Expand Up @@ -1196,7 +1200,7 @@ describe('skyflow invoke connection', () => {
__esModule: true,
isTokenValid:jest.fn(()=>true),
}));
const clientReq = jest.fn(() => Promise.resolve(invokeConnectionRes));
const clientReq = jest.fn(() => Promise.resolve({data:invokeConnectionRes}));
const mockClient = {
config: skyflowConfig,
request: clientReq,
Expand Down Expand Up @@ -1410,7 +1414,7 @@ describe("Update method",()=>{
__esModule: true,
isTokenValid:jest.fn(()=>true),
}));
const clientReq = jest.fn(() => Promise.resolve(successUpdateRequestResponse));
const clientReq = jest.fn(() => Promise.resolve({data:successUpdateRequestResponse}));
const mockClient = {
config: skyflowConfig,
request: clientReq,
Expand Down Expand Up @@ -1443,7 +1447,7 @@ describe("Update method",()=>{
__esModule: true,
isTokenValid:jest.fn(()=>true),
}));
const clientReq = jest.fn(() => Promise.resolve(successUpdateRequestWithoutTokensResponse));
const clientReq = jest.fn(() => Promise.resolve({data:successUpdateRequestWithoutTokensResponse}));
const mockClient = {
config: skyflowConfig,
request: clientReq,
Expand Down Expand Up @@ -1480,7 +1484,7 @@ describe("Update method",()=>{
const clientReq = jest.fn().mockImplementation((args) => {
const check = args.url.includes('test_update_id')
if(check)
return Promise.resolve(successUpdateRequestResponse);
return Promise.resolve({data:successUpdateRequestResponse});
else
return Promise.reject(errorUpdateRequestResponse);
});
Expand Down Expand Up @@ -1588,7 +1592,7 @@ describe('skyflow detokenize with redaction', () => {
let reqArg;
const clientReq = jest.fn((arg) => {
reqArg = arg;
return Promise.resolve(detokenizeRes)
return Promise.resolve({data:detokenizeRes})
});

const mockClient = {
Expand Down Expand Up @@ -1636,7 +1640,7 @@ describe('skyflow detokenize with redaction', () => {
let reqArg;
const clientReq = jest.fn((arg) => {
reqArg = arg;
return Promise.resolve(detokenizeRes)
return Promise.resolve({data:detokenizeRes})
});

const mockClient = {
Expand Down Expand Up @@ -1684,7 +1688,7 @@ describe('skyflow detokenize with redaction', () => {
let reqArg;
const clientReq = jest.fn((arg) => {
reqArg = arg;
return Promise.resolve(detokenizeRes)
return Promise.resolve({data:detokenizeRes})
});

const mockClient = {
Expand Down Expand Up @@ -1732,7 +1736,7 @@ describe('skyflow detokenize with redaction', () => {
let reqArg;
const clientReq = jest.fn((arg) => {
reqArg = arg;
return Promise.resolve(detokenizeRes)
return Promise.resolve({data:detokenizeRes})
});

const mockClient = {
Expand Down Expand Up @@ -1780,7 +1784,7 @@ describe('skyflow detokenize with redaction', () => {
let reqArg;
const clientReq = jest.fn((arg) => {
reqArg = arg;
return Promise.resolve(detokenizeRes)
return Promise.resolve({data:detokenizeRes})
});

const mockClient = {
Expand Down Expand Up @@ -1897,7 +1901,7 @@ describe('get method with options', () => {
let reqArg;
const clientReq = jest.fn((arg) => {
reqArg = arg;
return Promise.resolve(getByIdRes)
return Promise.resolve({data:getByIdRes})
});

const mockClient = {
Expand Down Expand Up @@ -1934,7 +1938,7 @@ describe('get method with options', () => {
let reqArg;
const clientReq = jest.fn((arg) => {
reqArg = arg;
return Promise.resolve(getByIdRes)
return Promise.resolve({data:getByIdRes})
});

const mockClient = {
Expand Down Expand Up @@ -1971,7 +1975,7 @@ describe('get method with options', () => {
let reqArg;
const clientReq = jest.fn((arg) => {
reqArg = arg;
return Promise.resolve(getByIdRes)
return Promise.resolve({data:getByIdRes})
});

const mockClient = {
Expand Down Expand Up @@ -2063,7 +2067,7 @@ describe('get method with options', () => {
let reqArg;
const clientReq = jest.fn((arg) => {
reqArg = arg;
return Promise.resolve(getByIdRes);
return Promise.resolve({data:getByIdRes});
});

const mockClient = {
Expand Down Expand Up @@ -2096,7 +2100,7 @@ describe('get method with options', () => {
let reqArg;
const clientReq = jest.fn((arg) => {
reqArg = arg;
return Promise.resolve(getByIdRes);
return Promise.resolve({data:getByIdRes});
});

const mockClient = {
Expand Down Expand Up @@ -2222,7 +2226,7 @@ describe('skyflow delete method', () => {
isTokenValid: jest.fn(() => true),
}));

const clientReq = jest.fn(() => Promise.resolve(successDeleteRequestResponse));
const clientReq = jest.fn(() => Promise.resolve({data:successDeleteRequestResponse}));
const mockClient = {
config: skyflowConfig,
request: clientReq,
Expand Down Expand Up @@ -2301,7 +2305,7 @@ describe('skyflow delete method', () => {
const clientReq = jest.fn((args) => {
const check = args.url.includes('test_delete_id')
if (check) {
return Promise.resolve(successDeleteRequestResponse);
return Promise.resolve({data:successDeleteRequestResponse});
} else {
return Promise.reject(errorDeleteRequestResponse);
}
Expand Down

0 comments on commit d3f1288

Please sign in to comment.