mediawiki.api: Refactor getToken and postWithToken methods
authorTimo Tijhof <krinklemail@gmail.com>
Wed, 5 Feb 2014 20:54:20 +0000 (12:54 -0800)
committerKrinkle <krinklemail@gmail.com>
Wed, 2 Apr 2014 23:22:35 +0000 (23:22 +0000)
commit9641767d06dac34144a723561cb7004a9978bade
treeb62ca987bbb1a62e21137da4262393d1c2399bbc
parent49abc169d7446a7e5cbf63ba2148409c13357703
mediawiki.api: Refactor getToken and postWithToken methods

* getToken now keeps promise objects around so that it can:
  1) Re-use already retrieved tokens instead of always making
     a new request (this was previously done locally inside
     one of the callers, namely #postWithToken).
  2) By storing promises instead of the tokens, we can re-use
     it asynchronously. While it is unlikely that one module
     calls getToken twice in a short period amount of time,
     it is quite likely that two separate modules (both using
     mediawiki.api) request similar tokens from the API at the
     same time (e.g. at page load).

  This cache has to be additionally keyed by API endpoint since
  mw.Api is a generic class that can be (and in practice, is)
  instantied for foreign wiki as well.

* Initialise the cache with pre-resolved promises for values
  from user.tokens from the local wiki (embedded in the page
  output, no point in requesting these again from the API).

* postWithToken no longer has its own token cache, it now
  unconditionally uses getToken.

* postWithToken already took care of clearing the token cache when
  the server responds with a 'badtoken' error. However, it then
  changed the request to delete the 'token' query and try again
  without any token. That seems rather silly. Change this to
  instead have getToken fetch a fresh one and re-submit with that.

* Add unit tests.

Bug: 34733
Change-Id: I5c25ae5ea4bf3336899bc60fd94ec3b0948050e1
resources/Resources.php
resources/mediawiki.api/mediawiki.api.js
tests/qunit/suites/resources/mediawiki.api/mediawiki.api.test.js