gr-request/gr-ajax cleanup (gr-diff-comment)

Bug: Issue 3988
Change-Id: I0c100a0b77a256c1eb34f94e8fff0515add5dea1
diff --git a/polygerrit-ui/app/elements/diff/gr-diff-comment/gr-diff-comment.js b/polygerrit-ui/app/elements/diff/gr-diff-comment/gr-diff-comment.js
index 81449b8..b39295a 100644
--- a/polygerrit-ui/app/elements/diff/gr-diff-comment/gr-diff-comment.js
+++ b/polygerrit-ui/app/elements/diff/gr-diff-comment/gr-diff-comment.js
@@ -72,22 +72,33 @@
     save: function() {
       this.comment.message = this._editDraft;
       this.disabled = true;
-      var endpoint = this._restEndpoint(this.comment.id);
-      this._send('PUT', endpoint).then(function(req) {
+      this._xhrPromise = this._saveDraft(this.comment).then(function(response) {
         this.disabled = false;
-        var comment = req.response;
-        comment.__draft = true;
-        // Maintain the ephemeral draft ID for identification by other
-        // elements.
-        if (this.comment.__draftID) {
-          comment.__draftID = this.comment.__draftID;
+        if (!response.ok) {
+          alert('Your draft couldn’t be saved. Check the console and contact ' +
+              'the PolyGerrit team for assistance.');
+          return response.text().then(function(text) {
+            console.error(text);
+          });
         }
-        this.comment = comment;
-        this.editing = false;
+        return this.$.restAPI.getResponseObject(response).then(function(obj) {
+          var comment = obj;
+          comment.__draft = true;
+          // Maintain the ephemeral draft ID for identification by other
+          // elements.
+          if (this.comment.__draftID) {
+            comment.__draftID = this.comment.__draftID;
+          }
+          this.comment = comment;
+          this.editing = false;
+
+          return obj;
+        }.bind(this));
       }.bind(this)).catch(function(err) {
         alert('Your draft couldn’t be saved. Check the console and contact ' +
             'the PolyGerrit team for assistance.');
         this.disabled = false;
+        console.error(err.message);
       }.bind(this));
     },
 
@@ -179,18 +190,27 @@
         throw Error('Cannot discard a non-draft comment.');
       }
       this.disabled = true;
-      var commentID = this.comment.id;
-      if (!commentID) {
+      if (!this.comment.id) {
         this.fire('comment-discard');
         return;
       }
-      this._send('DELETE', this._restEndpoint(commentID)).then(function(req) {
+
+      this._xhrPromise =
+          this._deleteDraft(this.comment).then(function(response) {
+        this.disabled = false;
+        if (!response.ok) {
+          alert('Your draft couldn’t be deleted. Check the console and ' +
+              'contact the PolyGerrit team for assistance.');
+          return response.text().then(function(text) {
+            console.error(text);
+          });
+        }
         this.fire('comment-discard');
       }.bind(this)).catch(function(err) {
-        alert('Your draft couldn’t be deleted. Check the console and ' +
-            'contact the PolyGerrit team for assistance.');
+        alert('Your draft couldn’t be deleted. Check the console and contact ' +
+            'the PolyGerrit team for assistance.');
         this.disabled = false;
-      }.bind(this));
+      }.bind(this));;
     },
 
     _preventDefaultAndBlur: function(e) {
@@ -198,26 +218,13 @@
       Polymer.dom(e).rootTarget.blur();
     },
 
-    _send: function(method, url) {
-      var xhr = document.createElement('gr-request');
-      var opts = {
-        method: method,
-        url: url,
-      };
-      if (method == 'PUT' || method == 'POST') {
-        opts.body = this.comment;
-      }
-      this._xhrPromise = xhr.send(opts);
-      return this._xhrPromise;
+    _saveDraft: function(draft) {
+      return this.$.restAPI.saveDiffDraft(this.changeNum, this.patchNum, draft);
     },
 
-    _restEndpoint: function(id) {
-      var path = '/changes/' + this.changeNum + '/revisions/' +
-          this.patchNum + '/drafts';
-      if (id) {
-        path += '/' + id;
-      }
-      return path;
+    _deleteDraft: function(draft) {
+      return this.$.restAPI.deleteDiffDraft(this.changeNum, this.patchNum,
+          draft);
     },
   });
 })();