Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 40 additions & 8 deletions unitylibs/core/workflow/workflow-acrobat/action-binder.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ export default class ActionBinder {
upload_warn_delete_asset: -603,
validation_warn_validate_files: -604,
warn_fetch_experiment: -605,
warn_page_config_call_failed: -606,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check if the dashboard query needs modification to start recording this. Actual modification to be done after the PR merge

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, it may need dashboard modifications.

};

static NEW_TO_OLD_ERROR_KEY_MAP = {
Expand Down Expand Up @@ -215,13 +216,17 @@ export default class ActionBinder {
});
}

getAcrobatApiConfig() {
unityConfig.acrobatEndpoint = {
createAsset: `${unityConfig.apiEndPoint}/asset`,
finalizeAsset: `${unityConfig.apiEndPoint}/asset/finalize`,
getMetadata: `${unityConfig.apiEndPoint}/asset/metadata`,
getAcrobatApiConfig(newAPIEndpoint) {
const apiEndPoint = newAPIEndpoint || unityConfig.apiEndPoint;
return {
acrobatEndpoint: {
createAsset: `${apiEndPoint}/asset`,
finalizeAsset: `${apiEndPoint}/asset/finalize`,
getMetadata: `${apiEndPoint}/asset/metadata`,
connector: `${apiEndPoint}/asset/connector`,
log: `${apiEndPoint}/log`,
},
};
return unityConfig;
}

getAdditionalHeaders() {
Expand Down Expand Up @@ -281,6 +286,7 @@ export default class ActionBinder {
subCode: ActionBinder.ERROR_MAP[errorMetaData.subCode] || errorMetaData.subCode || status,
desc: errorMetaData.desc || message || info || undefined,
},
logEndPoint: this.acrobatApiConfig?.acrobatEndpoint?.log,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

slytherin (dc) side changes corresponding to this are in PR:
adobecom/dc#1295

sendToSplunk,
},
},
Expand All @@ -289,7 +295,7 @@ export default class ActionBinder {

async dispatchAnalyticsEvent(eventName, data = null) {
const sendToSplunk = this.workflowCfg.targetCfg.sendSplunkAnalytics;
const detail = { event: eventName, ...(data && { data }), sendToSplunk };
const detail = { event: eventName, ...(data && { data }), logEndPoint: this.acrobatApiConfig?.acrobatEndpoint?.log, sendToSplunk };
this.block.dispatchEvent(new CustomEvent(unityConfig.trackAnalyticsEvent, { detail }));
}

Expand Down Expand Up @@ -426,7 +432,7 @@ export default class ActionBinder {
const postOpts = await getApiCallOptions('POST', unityConfig.apiKey, this.getAdditionalHeaders() || {}, { body: JSON.stringify(cOpts) });
this.promiseStack.push(
this.networkUtils.fetchFromServiceWithRetry(
this.acrobatApiConfig.connectorApiEndPoint,
this.acrobatApiConfig.acrobatEndpoint.connector,
postOpts,
),
);
Expand Down Expand Up @@ -511,6 +517,7 @@ export default class ActionBinder {
const { isValid, validFiles } = await this.validateFiles(sanitizedFiles);
if (!isValid) return;
await this.initUploadHandler();
await this.ensureOptimalEndpoint();
if (files.length === 1 || (validFiles.length === 1 && !verbsWithoutFallback.includes(this.workflowCfg.enabledFeatures[0]))) {
await this.handleSingleFileUpload(validFiles);
} else {
Expand Down Expand Up @@ -682,6 +689,28 @@ export default class ActionBinder {
this.filesData.assetId = assetId;
}

async ensureOptimalEndpoint() {
if (this.pageConfigPromise) {
await this.pageConfigPromise;
}
}

async fetchPageConfig() {
const getOpts = await getApiCallOptions('GET', unityConfig.apiKey, {}, {});
const pageConfigUrl = `${unityConfig.apiEndPoint}/pageConfig`;
await this.networkUtils.fetchPageConfig(
pageConfigUrl,
getOpts,
(newEndpoint) => {
this.dispatchAnalyticsEvent('pageConfigUpdated', { newEndpoint });
this.acrobatApiConfig = this.getAcrobatApiConfig(newEndpoint);
},
(failure) => {
this.dispatchErrorToast('warn_page_config_call_failed', null, null, true, true, { code: 'warn_page_config_call_failed', subCode: failure?.status, desc: failure?.type });
},
);
}

async initActionListeners(b = this.block, actMap = this.actionMap) {
for (const [key, value] of Object.entries(actMap)) {
const el = b.querySelector(key);
Expand Down Expand Up @@ -714,5 +743,8 @@ export default class ActionBinder {
if (b === this.block) {
this.loadTransitionScreen();
}
if (!this.pageConfigPromise) {
this.pageConfigPromise = this.fetchPageConfig();
}
}
}
23 changes: 23 additions & 0 deletions unitylibs/utils/NetworkUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,27 @@ export default class NetworkUtils {
throw error;
}
}

async fetchPageConfig(pageConfigUrl, options, unityEndpointSuccessCb, unityEndpointFailureCb) {
try {
const TIMEOUT_MS = 5000;
const pageConfigResponse = await this.fetchWithTimeout(pageConfigUrl, options, TIMEOUT_MS);
if (pageConfigResponse.ok) {
const responseJson = await this.getResponseJson(pageConfigResponse, {});
const locationHeader = pageConfigResponse.headers.get('location');
if (locationHeader) {
const newEndpoint = `${locationHeader}/api/v1`;
unityEndpointSuccessCb(newEndpoint);
return responseJson;
}
unityEndpointFailureCb({ type: 'no-location-header', status: pageConfigResponse.status });
return responseJson;
}
unityEndpointFailureCb({ type: 'non-ok-status', status: pageConfigResponse.status });
return await this.getResponseJson(pageConfigResponse, {});
} catch (error) {
unityEndpointFailureCb({ type: `Network-error - ${error.message}` });
return null;
}
}
}
Loading