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