Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(SDK-2101): add setrequestmetadata public method #944

Merged
merged 4 commits into from
Dec 11, 2023
Merged
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
2 changes: 1 addition & 1 deletion src/0_queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ goog.provide('task_queue');
/**
* @returns {function(function(function()))}
*/
var task_queue = function() {
task_queue = function() {
var queue = [];
var next = function() {
if (queue.length) {
Expand Down
1 change: 1 addition & 0 deletions src/1_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -1320,3 +1320,4 @@ utils.removeTrailingDotZeros = function(versionNumber) {
}
return versionNumber;
};

11 changes: 5 additions & 6 deletions src/2_storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@ var BRANCH_KEY_PREFIX = 'BRANCH_WEBSDK_KEY';

/** @typedef {undefined|{get:function(string, boolean=), set:function(string, (string|boolean), boolean=),
* remove:function(string), clear:function(), isEnabled:function()}} */
var storage;

/**
* @class BranchStorage
* @constructor
*/
var BranchStorage = function(storageMethods) {
storage.BranchStorage = function(storageMethods) {
for (var i = 0; i < storageMethods.length; i++) {
var storageMethod = this[storageMethods[i]];
storageMethod = (typeof storageMethod === 'function') ? storageMethod() : storageMethod;
Expand Down Expand Up @@ -150,12 +149,12 @@ var webStorage = function(perm) {
};

/** @type {storage} */
BranchStorage.prototype['local'] = function() {
storage.BranchStorage.prototype['local'] = function() {
return webStorage(true);
};

/** @type {storage} */
BranchStorage.prototype['session'] = function() {
storage.BranchStorage.prototype['session'] = function() {
return webStorage(false);
};

Expand Down Expand Up @@ -214,12 +213,12 @@ var cookies = function() {
};
};

BranchStorage.prototype['cookie'] = function() {
storage.BranchStorage.prototype['cookie'] = function() {
return cookies();
};

/** @type {storage} */
BranchStorage.prototype['pojo'] = {
storage.BranchStorage.prototype['pojo'] = {
getAll: function() {
return this._store;
},
Expand Down
5 changes: 4 additions & 1 deletion src/3_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* @class Server
* @constructor
*/
var Server = function() { };
Server = function() { };

Server.prototype._jsonp_callback_index = 0;

Expand Down Expand Up @@ -131,6 +131,9 @@
// to .setBranchViewData() call so the logic above won't work
utils.merge(d, data);
}
if (data.hasOwnProperty("branch_requestMetadata") && data["branch_requestMetadata"] && !(resource.endpoint === '/v1/pageview' || resource.endpoint === '/v1/dismiss')) {
d['metadata'] = safejson.stringify(data["branch_requestMetadata"]);
}

if (resource.method === 'POST') {
try {
Expand Down
36 changes: 35 additions & 1 deletion src/6_branch.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ Branch = function() {

var storageMethods = [ 'session', 'cookie', 'pojo' ];

this._storage = /** @type {storage} */ (new BranchStorage(storageMethods));
this._storage = /** @type {storage} */ (new storage.BranchStorage(storageMethods)); // jshint ignore:line

this._server = new Server();

Expand All @@ -139,6 +139,7 @@ Branch = function() {
this._listeners = [ ];

this.sdk = sdk + config.version;
this.requestMetadata = {};

this.init_state = init_states.NO_INIT;
this.init_state_fail_code = init_state_fail_codes.NO_FAILURE;
Expand Down Expand Up @@ -203,7 +204,17 @@ Branch.prototype._api = function(resource, obj, callback) {
if (utils.userPreferences.trackingDisabled) {
obj['tracking_disabled'] = utils.userPreferences.trackingDisabled;
}
if (this.requestMetadata) {
for (var metadata_key in this.requestMetadata) {
if (this.requestMetadata.hasOwnProperty(metadata_key)) {
if (!obj["branch_requestMetadata"]) {
obj["branch_requestMetadata"] = {};
}
obj["branch_requestMetadata"][metadata_key] = this.requestMetadata[metadata_key];
}
}

}
return this._server.request(resource, obj, this._storage, function(err, data) {
callback(err, data);
});
Expand Down Expand Up @@ -1926,3 +1937,26 @@ Branch.prototype['setAPIResponseCallback'] = wrap(callback_params.NO_CALLBACK, f
Branch.prototype.referringLink = function(withExtendedJourneysAssist) {
return this._referringLink(withExtendedJourneysAssist);
};

/***
* @function Branch.setRequestMetaData
* @param {String} key - Request metadata key
* @param {String} value - Request metadata value
* Sets request metadata that gets passed along with all the API calls except v1/pageview
*/
Branch.prototype.setRequestMetaData = function(key, value) {
try {
if ((typeof(key) === 'undefined' || key === null || key.length === 0) || (typeof value === "undefined")) {
return;
}

if (this.requestMetadata.hasOwnProperty(key) && value === null) {
delete this.requestMetadata[key];
}

this.requestMetadata = utils.addPropertyIfNotNull(this.requestMetadata, key, value);
}
catch (e) {
console.error("An error occured while setting request metadata", e);
}
};
58 changes: 53 additions & 5 deletions test/6_branch_new.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,66 @@
var sinon = require('sinon');

goog.require('Branch');
goog.require('utils');

describe('Branch - new', function() {
var branch;
beforeEach(function() {
branch = sinon.createStubInstance(Branch);
});
var branch_instance = new Branch();
var assert = testUtils.unplanned();
afterEach(function() {
sinon.restore();
});
describe('referringLink', function() {
it('test method exists', function() {
sinon.assert.match(typeof branch.referringLink, "function");
sinon.assert.match(typeof branch_instance.referringLink, "function");
});
});
describe('setRequestMetaData', function() {
var addPropertyIfNotNullSpy;
beforeEach(function() {
addPropertyIfNotNullSpy = sinon.spy(utils, 'addPropertyIfNotNull');
});
it('test method exists', function() {
sinon.assert.match(typeof branch_instance.setRequestMetaData, "function");
});
it('should set metadata for a valid key and value', function() {
var key = 'validKey';
var value = 'validValue';
var requestMetadata = {
};
var result = branch_instance.setRequestMetaData.call({ requestMetadata: requestMetadata }, key, value);
assert.strictEqual(result, undefined);
sinon.assert.calledOnce(addPropertyIfNotNullSpy);
assert.deepEqual(requestMetadata, { "validKey": "validValue" });
});

it('should delete metadata for a key when value is null', function() {
var requestMetadata = { "keyToDelete": "value" };
branch_instance.setRequestMetaData.call({ requestMetadata: requestMetadata }, "keyToDelete", null);
assert.deepEqual(requestMetadata, {});
});

it('should not modify metadata for an invalid key or undefined value', function() {
var invalidKey = null;
var undefinedValue;
var requestMetadata = { "key": "value" };

var result1 = branch_instance.setRequestMetaData.call({ requestMetadata: requestMetadata }, invalidKey, 'validValue');
var result2 = branch_instance.setRequestMetaData.call({ requestMetadata: requestMetadata }, 'validKey', undefinedValue);
assert.strictEqual(result1, undefined);
assert.strictEqual(result2, undefined);
sinon.assert.notCalled(addPropertyIfNotNullSpy);
assert.deepEqual(requestMetadata, { "key": "value" });
});
});
/*
describe('setRequestMetaData - exceptions', function() {
it('should throw exception', function() {
var logSpy = sinon.spy(console, 'log');
sinon.stub(utils, 'addPropertyIfNotNull').throws(new Error("fake error"));
branch_instance.setRequestMetaData.call({ requestMetadata: {} }, "validKey", 'validValue');
assert(logSpy.calledWith("An error occured while setting request metadata"));
});

});
*/
});
2 changes: 1 addition & 1 deletion test/branch-deps.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ goog.addDependency('../../../../test/1_utils.js', [], ['utils']);
goog.addDependency('../../../../test/2_storage.js', [], ['storage']);
goog.addDependency('../../../../test/3_api.js', [], ['Server', 'config', 'resources', 'safejson', 'storage', 'utils']);
goog.addDependency('../../../../test/6_branch.js', [], ['Branch', 'banner_html', 'banner_utils', 'config', 'goog.json', 'resources', 'safejson', 'session', 'storage', 'utils']);
goog.addDependency('../../../../test/6_branch_new.js', [], ['Branch']);
goog.addDependency('../../../../test/6_branch_new.js', [], ['Branch', 'utils']);
goog.addDependency('../../../../test/7_integration.js', [], ['config', 'goog.json']);
goog.addDependency('../../../../test/blob-banner.js', [], []);
goog.addDependency('../../../../test/blob-interstitial.js', [], []);
Expand Down