Skip to content
Draft
Show file tree
Hide file tree
Changes from 10 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
45 changes: 37 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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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,25 @@ export default class ActionBinder {
this.filesData.assetId = assetId;
}

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

async checkandUpdatePageConfigEndpoint() {
await this.networkUtils.checkandUpdatePageConfigEndpoint(
(newEndpoint) => {
// send analytics event before updating the endpoint, where the pageConfig call is made.
Comment thread
vipu0303 marked this conversation as resolved.
Outdated
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 +740,8 @@ export default class ActionBinder {
if (b === this.block) {
this.loadTransitionScreen();
}
if (!this.pageConfigPromise) {
this.pageConfigPromise = this.checkandUpdatePageConfigEndpoint();
}
}
}
28 changes: 28 additions & 0 deletions unitylibs/utils/NetworkUtils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { unityConfig, getApiCallOptions } from '../scripts/utils.js';

export default class NetworkUtils {
handleAbortedRequest(url, options) {
if (!(options?.signal?.aborted)) return;
Expand Down Expand Up @@ -151,4 +153,30 @@ export default class NetworkUtils {
throw error;
}
}

async checkandUpdatePageConfigEndpoint(updateConfigCallback, onFailure) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
async checkandUpdatePageConfigEndpoint(updateConfigCallback, onFailure) {
async updateUnityEndpoint(updateConfigCallback, onFailure) {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

updated the name to fetchPageConfig.
The method now accepts unityEndpointSuccessCb and unityEndpointfailureCb and also returns the response body which make it future proof in case more info is being fetched from service in response.

try {
const TIMEOUT_MS = 5000;
const getOpts = await getApiCallOptions('GET', unityConfig.apiKey, {}, {});
Comment thread
vipu0303 marked this conversation as resolved.
Outdated
const pageConfigUrl = `${unityConfig.apiEndPoint}/pageConfig`;
Comment thread
vipu0303 marked this conversation as resolved.
Outdated
const pageConfigResponse = await this.fetchWithTimeout(pageConfigUrl, getOpts, TIMEOUT_MS);
if (pageConfigResponse.ok) {
const locationHeader = pageConfigResponse.headers.get('location');
if (locationHeader) {
const newEndpoint = `${locationHeader}/api/v1`;
if (typeof updateConfigCallback === 'function') updateConfigCallback(newEndpoint);
return;
}
console.warn('No location header found, keeping existing API endpoint');
Comment thread
vipu0303 marked this conversation as resolved.
Outdated
if (typeof onFailure === 'function') onFailure({ type: 'no-location-header', status: pageConfigResponse.status });
return;
}
console.error('pageConfig call failed with status:', pageConfigResponse.status);
Comment thread
vipu0303 marked this conversation as resolved.
Outdated
if (typeof onFailure === 'function') onFailure({ type: 'non-ok-status', status: pageConfigResponse.status });
return;
} catch (error) {
console.error('pageConfig call failed with error:', error);
Comment thread
vipu0303 marked this conversation as resolved.
Outdated
if (typeof onFailure === 'function') onFailure({ type: `Network-error - ${error.message}` });
}
}
}
Loading