Skip to content

Commit 73b99c3

Browse files
refactor(publisher): dedupe endpoint save logic between handleSave and handleSaveAndDeploy
1 parent adde311 commit 73b99c3

1 file changed

Lines changed: 103 additions & 166 deletions

File tree

  • portals/publisher/src/main/webapp/source/src/app/components/Apis/Details/Endpoints

‎portals/publisher/src/main/webapp/source/src/app/components/Apis/Details/Endpoints/Endpoints.jsx‎

Lines changed: 103 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -279,90 +279,92 @@ function Endpoints(props) {
279279
*
280280
* @param {boolean} isRedirect Used for dynamic endpoints to redirect to the runtime config page.
281281
*/
282-
const handleSave = (isRedirect) => {
282+
/**
283+
* Delete/ upload the production and sandbox sequence backends based on the current backend lists.
284+
*/
285+
const updateSequenceBackends = () => {
286+
if (productionBackendList?.length === 0 || (productionBackendList?.length > 0
287+
&& productionBackendList[0].content)) {
288+
api.deleteSequenceBackend(API_SECURITY_KEY_TYPE_PRODUCTION, api.id).then(() => {
289+
Alert.success('Production Sequence backend deleted successfully');
290+
})
291+
.catch(() => {
292+
Alert.error(intl.formatMessage({
293+
id: 'Apis.Details.Endpoints.Endpoints.delete.sequence.backend.error',
294+
defaultMessage: 'Error Deleting Production Sequence Backend',
295+
}));
296+
});
297+
}
283298

284-
const { endpointConfig, endpointImplementationType, serviceInfo } = apiObject;
285-
if (endpointConfig.endpoint_type === 'service') {
286-
endpointConfig.endpoint_type = 'http';
299+
if (sandBoxBackendList?.length === 0 || (sandBoxBackendList?.length > 0 && sandBoxBackendList[0].content)) {
300+
api.deleteSequenceBackend(API_SECURITY_KEY_TYPE_SANDBOX, api.id).then(() => {
301+
Alert.success('Sandbox Sequence backend deleted successfully');
302+
})
303+
.catch(() => {
304+
Alert.error(intl.formatMessage({
305+
id: 'Apis.Details.Endpoints.Endpoints.delete.sequence.backend.error',
306+
defaultMessage: 'Error Deleting Sandbox Sequence Backend',
307+
}));
308+
});
287309
}
288-
setUpdating(true);
289-
if (endpointConfig.endpoint_type === 'sequence_backend') {
290-
if (productionBackendList?.length === 0 || (productionBackendList?.length > 0
291-
&& productionBackendList[0].content)) {
292-
api.deleteSequenceBackend(API_SECURITY_KEY_TYPE_PRODUCTION, api.id).then(() => {
293-
Alert.success('Production Sequence backend deleted successfully');
310+
if (productionBackendList?.length > 0 && productionBackendList[0].content) {
311+
const productionBackend = productionBackendList[0];
312+
api.uploadCustomBackend(productionBackend.content, API_SECURITY_KEY_TYPE_PRODUCTION, api.id)
313+
.then(() => {
314+
Alert.success('Custom backend uploaded successfully');
294315
})
295-
.catch(() => {
296-
Alert.error(intl.formatMessage({
297-
id: 'Apis.Details.Endpoints.Endpoints.delete.sequence.backend.error',
298-
defaultMessage: 'Error Deleting Production Sequence Backend',
299-
}));
300-
});
301-
}
302-
303-
if (sandBoxBackendList?.length === 0 || (sandBoxBackendList?.length > 0 && sandBoxBackendList[0].content)) {
304-
api.deleteSequenceBackend(API_SECURITY_KEY_TYPE_SANDBOX, api.id).then(() => {
305-
Alert.success('Sandbox Sequence backend deleted successfully');
316+
.catch((error) => {
317+
const backendMessage = error?.response?.body?.description;
318+
Alert.error(
319+
backendMessage || intl.formatMessage({
320+
id: 'Apis.Details.Endpoints.Endpoints.upload.sequence.backend.error',
321+
defaultMessage: 'Error Uploading Production Sequence Backend',
322+
}),
323+
);
324+
});
325+
}
326+
if (sandBoxBackendList?.length > 0 && sandBoxBackendList[0].content) {
327+
const sandBackend = sandBoxBackendList[0];
328+
api.uploadCustomBackend(sandBackend.content, API_SECURITY_KEY_TYPE_SANDBOX, api.id)
329+
.then(() => {
330+
Alert.success('Custom backend uploaded successfully');
306331
})
307-
.catch(() => {
308-
Alert.error(intl.formatMessage({
309-
id: 'Apis.Details.Endpoints.Endpoints.delete.sequence.backend.error',
310-
defaultMessage: 'Error Deleting Sandbox Sequence Backend',
311-
}));
312-
});
313-
}
314-
if (productionBackendList?.length > 0 && productionBackendList[0].content) {
315-
const productionBackend = productionBackendList[0];
316-
api.uploadCustomBackend(productionBackend.content, API_SECURITY_KEY_TYPE_PRODUCTION, api.id)
317-
.then(() => {
318-
Alert.success('Custom backend uploaded successfully');
319-
})
320-
.catch((error) => {
321-
const backendMessage = error?.response?.body?.description;
322-
Alert.error(
323-
backendMessage || intl.formatMessage({
324-
id: 'Apis.Details.Endpoints.Endpoints.upload.sequence.backend.error',
325-
defaultMessage: 'Error Uploading Production Sequence Backend',
326-
}),
327-
);
328-
});
329-
}
330-
if (sandBoxBackendList?.length > 0 && sandBoxBackendList[0].content) {
331-
const sandBackend = sandBoxBackendList[0];
332-
api.uploadCustomBackend(sandBackend.content, API_SECURITY_KEY_TYPE_SANDBOX, api.id)
333-
.then(() => {
334-
Alert.success('Custom backend uploaded successfully');
335-
})
336-
.catch((error) => {
337-
const backendMessage = error?.response?.body?.description;
338-
Alert.error(
339-
backendMessage || intl.formatMessage({
340-
id: 'Apis.Details.Endpoints.Endpoints.upload.sequence.backend.error',
341-
defaultMessage: 'Error Uploading Sandbox Sequence Backend',
342-
}),
343-
);
344-
});
345-
}
332+
.catch((error) => {
333+
const backendMessage = error?.response?.body?.description;
334+
Alert.error(
335+
backendMessage || intl.formatMessage({
336+
id: 'Apis.Details.Endpoints.Endpoints.upload.sequence.backend.error',
337+
defaultMessage: 'Error Uploading Sandbox Sequence Backend',
338+
}),
339+
);
340+
});
346341
}
342+
};
343+
344+
/**
345+
* Update the swagger (for INLINE/ MOCKED_OAS implementations) or the API object, then invoke the completion
346+
* callback. Shared by handleSave and handleSaveAndDeploy, which only differ in the update payload and what
347+
* happens once the update settles.
348+
*
349+
* @param {string} endpointImplementationType The api implementation type (INLINE/ ENDPOINT/ MOCKED_OAS).
350+
* @param {object} updatePayload The payload to send via updateAPI for INLINE/ MOCKED_OAS implementations.
351+
* @param {Function} onComplete Callback invoked once the update settles.
352+
*/
353+
const persistEndpointConfig = (endpointImplementationType, updatePayload, onComplete) => {
347354
if (endpointImplementationType === ENDPOINT_IMPLEMENTATION_TYPE_INLINE
348355
|| endpointImplementationType === ENDPOINT_IMPLEMENTATION_TYPE_MOCKED_OAS) {
349356
api.updateSwagger(swagger).then((resp) => {
350357
setSwagger(resp.obj);
351358
}).then(() => {
352-
updateAPI({ endpointConfig, endpointImplementationType, serviceInfo })
359+
updateAPI(updatePayload)
353360
.catch((error) => {
354361
if (error.response) {
355362
Alert.error(error.response.body.description);
356363
} else {
357364
Alert.error('Error occurred while updating endpoint configurations');
358365
}
359366
});
360-
}).finally(() => {
361-
setUpdating(false);
362-
if (isRedirect) {
363-
history.push('/apis/' + api.id + '/policies');
364-
}
365-
});
367+
}).finally(onComplete);
366368
} else {
367369
const apiObjectCopy = cloneDeep(apiObject);
368370
if (apiObjectCopy.endpointConfig.endpoint_type === 'service') {
@@ -376,13 +378,34 @@ function Endpoints(props) {
376378
Alert.error('Error occurred while updating endpoint configurations');
377379
}
378380
})
379-
.finally(() => {
380-
setUpdating(false);
381-
if (isRedirect) {
382-
history.push('/apis/' + api.id + '/policies');
383-
}
384-
});
381+
.finally(onComplete);
382+
}
383+
};
384+
385+
/**
386+
* Method to update the api.
387+
*
388+
* @param {boolean} isRedirect Used for dynamic endpoints to redirect to the runtime config page.
389+
*/
390+
const handleSave = (isRedirect) => {
391+
const { endpointConfig, endpointImplementationType, serviceInfo } = apiObject;
392+
if (endpointConfig.endpoint_type === 'service') {
393+
endpointConfig.endpoint_type = 'http';
385394
}
395+
setUpdating(true);
396+
if (endpointConfig.endpoint_type === 'sequence_backend') {
397+
updateSequenceBackends();
398+
}
399+
persistEndpointConfig(
400+
endpointImplementationType,
401+
{ endpointConfig, endpointImplementationType, serviceInfo },
402+
() => {
403+
setUpdating(false);
404+
if (isRedirect) {
405+
history.push('/apis/' + api.id + '/policies');
406+
}
407+
},
408+
);
386409
};
387410

388411
const handleSaveAndDeploy = () => {
@@ -392,103 +415,17 @@ function Endpoints(props) {
392415
}
393416
setUpdating(true);
394417
if (endpointConfig.endpoint_type === 'sequence_backend') {
395-
if (productionBackendList?.length === 0
396-
|| (productionBackendList?.length > 0 && productionBackendList[0].content)) {
397-
api.deleteSequenceBackend(API_SECURITY_KEY_TYPE_PRODUCTION, api.id)
398-
.then(() => {
399-
Alert.success('Production Sequence backend deleted successfully');
400-
})
401-
.catch(() => {
402-
Alert.error(intl.formatMessage({
403-
id: 'Apis.Details.Endpoints.Endpoints.delete.sequence.backend.error',
404-
defaultMessage: 'Error Deleting Production Sequence Backend',
405-
}));
406-
});
407-
}
408-
409-
if (sandBoxBackendList?.length === 0
410-
|| (sandBoxBackendList?.length > 0 && sandBoxBackendList[0].content)) {
411-
api.deleteSequenceBackend(API_SECURITY_KEY_TYPE_SANDBOX, api.id)
412-
.then(() => {
413-
Alert.success('Sandbox Sequence backend deleted successfully');
414-
})
415-
.catch(() => {
416-
Alert.error(intl.formatMessage({
417-
id: 'Apis.Details.Endpoints.Endpoints.delete.sequence.backend.error',
418-
defaultMessage: 'Error Deleting Sandbox Sequence Backend',
419-
}));
420-
});
421-
}
422-
if (productionBackendList?.length > 0 && productionBackendList[0].content) {
423-
const productionBackend = productionBackendList[0];
424-
api.uploadCustomBackend(productionBackend.content, API_SECURITY_KEY_TYPE_PRODUCTION, api.id)
425-
.then(() => {
426-
Alert.success('Custom backend uploaded successfully');
427-
})
428-
.catch((error) => {
429-
const backendMessage = error?.response?.body?.description;
430-
Alert.error(
431-
backendMessage || intl.formatMessage({
432-
id: 'Apis.Details.Endpoints.Endpoints.upload.sequence.backend.error',
433-
defaultMessage: 'Error Uploading Production Sequence Backend',
434-
}),
435-
);
436-
});
437-
}
438-
if (sandBoxBackendList?.length > 0 && sandBoxBackendList[0].content) {
439-
const sandBackend = sandBoxBackendList[0];
440-
api.uploadCustomBackend(sandBackend.content, API_SECURITY_KEY_TYPE_SANDBOX, api.id)
441-
.then(() => {
442-
Alert.success('Custom backend uploaded successfully');
443-
})
444-
.catch((error) => {
445-
const backendMessage = error?.response?.body?.description;
446-
Alert.error(
447-
backendMessage || intl.formatMessage({
448-
id: 'Apis.Details.Endpoints.Endpoints.upload.sequence.backend.error',
449-
defaultMessage: 'Error Uploading Sandbox Sequence Backend',
450-
}),
451-
);
452-
});
453-
}
418+
updateSequenceBackends();
454419
}
455-
if (endpointImplementationType === ENDPOINT_IMPLEMENTATION_TYPE_INLINE
456-
|| endpointImplementationType === ENDPOINT_IMPLEMENTATION_TYPE_MOCKED_OAS) {
457-
api.updateSwagger(swagger).then((resp) => {
458-
setSwagger(resp.obj);
459-
}).then(() => {
460-
updateAPI({ endpointConfig, endpointImplementationType, endpointSecurity, serviceInfo })
461-
.catch((error) => {
462-
if (error.response) {
463-
Alert.error(error.response.body.description);
464-
} else {
465-
Alert.error('Error occurred while updating endpoint configurations');
466-
}
467-
});
468-
}).finally(() => history.push({
420+
persistEndpointConfig(
421+
endpointImplementationType,
422+
{ endpointConfig, endpointImplementationType, endpointSecurity, serviceInfo },
423+
() => history.push({
469424
pathname: api.isAPIProduct() ? `/api-products/${api.id}/deployments`
470425
: `/apis/${api.id}/deployments`,
471426
state: 'deploy',
472-
}));
473-
} else {
474-
const apiObjectCopy = cloneDeep(apiObject);
475-
if (apiObjectCopy.endpointConfig.endpoint_type === 'service') {
476-
apiObjectCopy.endpointConfig.endpoint_type = 'http';
477-
}
478-
updateAPI(apiObjectCopy)
479-
.catch((error) => {
480-
if (error.response) {
481-
Alert.error(error.response.body.description);
482-
} else {
483-
Alert.error('Error occurred while updating endpoint configurations');
484-
}
485-
})
486-
.finally(() => history.push({
487-
pathname: api.isAPIProduct() ? `/api-products/${api.id}/deployments`
488-
: `/apis/${api.id}/deployments`,
489-
state: 'deploy',
490-
}));
491-
}
427+
}),
428+
);
492429
};
493430

494431
/**

0 commit comments

Comments
 (0)