Jdlrobson has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/182134

Change subject: WIP: Introduce ForeignApi
......................................................................

WIP: Introduce ForeignApi

Change-Id: I370b301c8913c09790197a3c21fdfb4a5984b8fb
---
M includes/Resources.php
A javascripts/modules/ForeignApi.js
M javascripts/modules/uploads/PhotoApi.js
M javascripts/modules/wikigrok/WikiDataApi.js
4 files changed, 88 insertions(+), 59 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/MobileFrontend 
refs/changes/34/182134/1

diff --git a/includes/Resources.php b/includes/Resources.php
index 05e0cf6..4ee2889 100644
--- a/includes/Resources.php
+++ b/includes/Resources.php
@@ -192,6 +192,15 @@
                'position' => 'bottom',
        ),
 
+       'mobile.foreignapi' => $wgMFResourceFileModuleBoilerplate + array(
+               'dependencies' => array(
+                       'mobile.startup',
+               ),
+               'scripts' => array(
+                       'javascripts/modules/ForeignApi.js',
+               ),
+       ),
+
        'mobile.redlinks' => $wgMFResourceFileModuleBoilerplate + array(
                'dependencies' => array(
                        'mobile.head',
@@ -370,6 +379,7 @@
                        'mobile.templates',
                        'mobile.editor.api',
                        'mobile.contentOverlays',
+                       'mobile.foreignapi',
                ),
                'scripts' => array(
                        'javascripts/modules/uploads/PhotoApi.js',
@@ -965,6 +975,7 @@
        'mobile.wikigrok.api' => $wgMFResourceFileModuleBoilerplate + array(
                'dependencies' => array(
                        'mobile.startup',
+                       'mobile.foreignapi',
                ),
                'scripts' => array(
                        'javascripts/modules/wikigrok/WikiDataApi.js',
diff --git a/javascripts/modules/ForeignApi.js 
b/javascripts/modules/ForeignApi.js
new file mode 100644
index 0000000..dcb51db
--- /dev/null
+++ b/javascripts/modules/ForeignApi.js
@@ -0,0 +1,73 @@
+( function ( M, $ ) {
+       var api = M.require( 'api' ),
+               Api = api.Api,
+               ForeignApi;
+       /**
+        * Gets claims and labels from the WikiData API
+        * @class WikiDataApi
+        * @extends Api
+        */
+       ForeignApi = Api.extend( {
+               useJsonp: true,
+               /**
+                * Get a central auth token from the current host for use on 
the foreign api.
+                * @return {jQuery.Deferred}
+                */
+               getCentralAuthToken: function () {
+                       var data = {
+                               action: 'centralauthtoken'
+                       };
+                       return api.get( data ).then( function ( resp ) {
+                               return resp.centralauthtoken.centralauthtoken;
+                       } );
+               },
+               /**
+                * Get a token from a foreign API
+                * @param {String} type of token you want to retrieve
+                * @param {String} centralAuthToken to help get it
+                * @return {jQuery.Deferred}
+                */
+               getToken: function ( type, centralAuthToken ) {
+                       var data = {
+                               action: 'query',
+                               meta: 'tokens',
+                               origin: this.getOrigin(),
+                               centralauthtoken: centralAuthToken,
+                               type: type
+                       };
+                       return this.get( data ).then( function ( resp ) {
+                               return resp.query.tokens[type + 'token'];
+                       } );
+               },
+               /**
+                * Post with support for central auth tokens
+                */
+               post: function ( data ) {
+                       var self = this,
+                               d = $.Deferred();
+
+                       // first let's sort out the token
+                       self.getCentralAuthToken().done( function ( 
centralAuthTokenOne ) {
+                               self.getToken( 'csrf', centralAuthTokenOne 
).done( function ( editToken ) {
+                                       self.getCentralAuthToken().done( 
function ( centralAuthTokenTwo ) {
+                                               data.format = 'json';
+                                               data.centralauthtoken = 
centralAuthTokenTwo;
+                                               data.token = editToken;
+                                               data.origin = self.getOrigin();
+                                               $.post( self.apiUrl, data 
).done( function ( resp ) {
+                                                       d.resolve( resp );
+                                               } );
+                                       } );
+                               } );
+                       } );
+                       return d;
+               },
+               /** @inheritdoc */
+               initialize: function ( options ) {
+                       Api.prototype.initialize.apply( this, arguments );
+               }
+       } );
+
+       M.define( 'modules/ForeignApi', ForeignApi );
+
+}( mw.mobileFrontend, jQuery ) );
diff --git a/javascripts/modules/uploads/PhotoApi.js 
b/javascripts/modules/uploads/PhotoApi.js
index 436b26a..9cc68a6 100644
--- a/javascripts/modules/uploads/PhotoApi.js
+++ b/javascripts/modules/uploads/PhotoApi.js
@@ -1,5 +1,5 @@
 ( function ( M, $ ) {
-       var Api = M.require( 'api' ).Api,
+       var ForeignApi = M.require( 'ForeignApi' ),
                user = M.require( 'user' ),
                endpoint = mw.config.get( 'wgMFPhotoUploadEndpoint' ),
                PhotoApi;
@@ -103,7 +103,7 @@
         * @class PhotoApi
         * @extends Api
         */
-       PhotoApi = Api.extend( {
+       PhotoApi = ForeignApi.extend( {
                useCentralAuthToken: mw.config.get( 'wgMFUseCentralAuthToken' ),
 
                /**
diff --git a/javascripts/modules/wikigrok/WikiDataApi.js 
b/javascripts/modules/wikigrok/WikiDataApi.js
index 1870293..5c10190 100644
--- a/javascripts/modules/wikigrok/WikiDataApi.js
+++ b/javascripts/modules/wikigrok/WikiDataApi.js
@@ -1,6 +1,5 @@
 ( function ( M, $ ) {
-       var api = M.require( 'api' ),
-               Api = api.Api,
+       var ForeignApi = M.require( 'modules/ForeignApi' ),
                config = mw.config.get( 'wgWikiBasePropertyConfig' ),
                WikiDataApi;
        /**
@@ -8,64 +7,10 @@
         * @class WikiDataApi
         * @extends Api
         */
-       WikiDataApi = Api.extend( {
+       WikiDataApi = ForeignApi.extend( {
                propertyIdInstanceOf: config.instanceOf,
                apiUrl: mw.config.get( 'wgMFWikiDataEndpoint' ),
-               useJsonp: true,
                language: mw.config.get( 'wgUserLanguage' ),
-               /**
-                * Get a central auth token from the current host for use on 
the foreign api.
-                * @return {jQuery.Deferred}
-                */
-               getCentralAuthToken: function () {
-                       var data = {
-                               action: 'centralauthtoken'
-                       };
-                       return api.get( data ).then( function ( resp ) {
-                               return resp.centralauthtoken.centralauthtoken;
-                       } );
-               },
-               /**
-                * Get a token from a foreign API
-                * @param {String} type of token you want to retrieve
-                * @param {String} centralAuthToken to help get it
-                * @return {jQuery.Deferred}
-                */
-               getToken: function ( type, centralAuthToken ) {
-                       var data = {
-                               action: 'query',
-                               meta: 'tokens',
-                               origin: this.getOrigin(),
-                               centralauthtoken: centralAuthToken,
-                               type: type
-                       };
-                       return this.get( data ).then( function ( resp ) {
-                               return resp.query.tokens[type + 'token'];
-                       } );
-               },
-               /**
-                * Post with support for central auth tokens
-                */
-               post: function ( data ) {
-                       var self = this,
-                               d = $.Deferred();
-
-                       // first let's sort out the token
-                       self.getCentralAuthToken().done( function ( 
centralAuthTokenOne ) {
-                               self.getToken( 'csrf', centralAuthTokenOne 
).done( function ( editToken ) {
-                                       self.getCentralAuthToken().done( 
function ( centralAuthTokenTwo ) {
-                                               data.format = 'json';
-                                               data.centralauthtoken = 
centralAuthTokenTwo;
-                                               data.token = editToken;
-                                               data.origin = self.getOrigin();
-                                               $.post( self.apiUrl, data 
).done( function ( resp ) {
-                                                       d.resolve( resp );
-                                               } );
-                                       } );
-                               } );
-                       } );
-                       return d;
-               },
                /** @inheritdoc */
                initialize: function ( options ) {
                        this.subjectId = options.itemId;

-- 
To view, visit https://gerrit.wikimedia.org/r/182134
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I370b301c8913c09790197a3c21fdfb4a5984b8fb
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/MobileFrontend
Gerrit-Branch: master
Gerrit-Owner: Jdlrobson <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to