Implement self.get and self.post from Gerrit JS API https://gerrit-review.googlesource.com/Documentation/js-api.html#self_get https://gerrit-review.googlesource.com/Documentation/js-api.html#self_post Feature: Issue 5960 Change-Id: I6778f40ba1d6d8b2b31ebb51ffd39a6fab265424
diff --git a/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-js-api-interface.html b/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-js-api-interface.html index 5c0535b..3cda49b 100644 --- a/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-js-api-interface.html +++ b/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-js-api-interface.html
@@ -15,10 +15,10 @@ --> <link rel="import" href="../../../bower_components/polymer/polymer.html"> <link rel="import" href="../../core/gr-reporting/gr-reporting.html"> +<link rel="import" href="../../shared/gr-rest-api-interface/gr-rest-api-interface.html"> <link rel="import" href="../gr-rest-api-interface/gr-rest-api-interface.html"> <dom-module id="gr-js-api-interface"> - <template></template> <script src="gr-change-actions-js-api.js"></script> <script src="gr-change-reply-js-api.js"></script> <script src="gr-js-api-interface.js"></script>
diff --git a/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-js-api-interface_test.html b/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-js-api-interface_test.html index c3013bd..40810b1 100644 --- a/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-js-api-interface_test.html +++ b/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-js-api-interface_test.html
@@ -36,6 +36,8 @@ let plugin; let errorStub; let sandbox; + let getResponseObjectStub; + let sendStub; const throwErrFn = function() { throw Error('Unfortunately, this handler has stopped'); @@ -43,10 +45,16 @@ setup(() => { sandbox = sinon.sandbox.create(); + getResponseObjectStub = sandbox.stub().returns(Promise.resolve()); + sendStub = sandbox.stub().returns(Promise.resolve()); stub('gr-rest-api-interface', { getAccount() { return Promise.resolve({name: 'Judy Hopps'}); }, + getResponseObject: getResponseObjectStub, + send(...args) { + return sendStub(...args); + }, }); element = fixture('basic'); errorStub = sandbox.stub(console, 'error'); @@ -67,6 +75,27 @@ 'http://test.com/plugins/testplugin/static/test.js'); }); + test('get', done => { + const response = {foo: 'foo'}; + getResponseObjectStub.returns(Promise.resolve(response)); + plugin.get('/url', r => { + assert.isTrue(sendStub.calledWith('GET', '/url')); + assert.strictEqual(r, response); + done(); + }); + }); + + test('post', done => { + const payload = {foo: 'foo'}; + const response = {bar: 'bar'}; + getResponseObjectStub.returns(Promise.resolve(response)); + plugin.post('/url', payload, r => { + assert.isTrue(sendStub.calledWith('POST', '/url', payload)); + assert.strictEqual(r, response); + done(); + }); + }); + test('history event', done => { plugin.on(element.EventType.HISTORY, throwErrFn); plugin.on(element.EventType.HISTORY, path => {
diff --git a/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-public-js-api.js b/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-public-js-api.js index a84eedd..0811935 100644 --- a/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-public-js-api.js +++ b/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-public-js-api.js
@@ -24,6 +24,14 @@ GWT_PLUGIN_STUB[name] = warnNotSupported.bind(null, name); } + let _restAPI; + const getRestAPI = () => { + if (!_restAPI) { + _restAPI = document.createElement('gr-rest-api-interface'); + } + return _restAPI; + }; + const API_VERSION = '0.1'; // GWT JSNI uses $wnd to refer to window. @@ -77,6 +85,20 @@ return this._url.origin + '/plugins/' + this._name + (opt_path || '/'); }; + Plugin.prototype._send = function(method, url, callback, opt_payload) { + return getRestAPI().send(method, url, opt_payload) + .then(getRestAPI().getResponseObject) + .then(callback); + }; + + Plugin.prototype.get = function(url, callback) { + return this._send('GET', url, callback); + }, + + Plugin.prototype.post = function(url, payload, callback) { + return this._send('POST', url, callback, payload); + }, + Plugin.prototype.changeActions = function() { return new GrChangeActionsInterface(Plugin._sharedAPIElement.getElement( Plugin._sharedAPIElement.Element.CHANGE_ACTIONS));