Santhosh has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/373178 )

Change subject: Remove hardcoded cxserver /translate API url
......................................................................

Remove hardcoded cxserver /translate API url

Use the sitemapper module for doing cxserver request.

We need move these methods from mw.cx.ui.TranslationView to a better
place as next step.

Change-Id: Idf2bd169f2bd9b890472c8615fb0a775469e60b4
---
M modules/ui/mw.cx.ui.TranslationView.js
1 file changed, 76 insertions(+), 6 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ContentTranslation 
refs/changes/78/373178/1

diff --git a/modules/ui/mw.cx.ui.TranslationView.js 
b/modules/ui/mw.cx.ui.TranslationView.js
index 59e04c0..eebe74f 100644
--- a/modules/ui/mw.cx.ui.TranslationView.js
+++ b/modules/ui/mw.cx.ui.TranslationView.js
@@ -1,4 +1,5 @@
 'use strict';
+
 /**
  * TranslationView
  *
@@ -366,14 +367,83 @@
 
 };
 
+/**
+ * Translate and adapt the given source section
+ * @param {string} source Source html content
+ * @return {jQuery.Promise}
+ */
 mw.cx.ui.TranslationView.prototype.translate = function ( source ) {
-       return $.ajax( {
-               method: 'POST',
-               contentType: 'application/x-www-form-urlencoded',
-               // TODO hardcoded
-               url: 'http://localhost:8080/v1/translate/en/es/Apertium',
-               data: { html: source }
+       var translateURL;
+
+       translateURL = this.config.siteMapper.getCXServerUrl( 
'/translate/$from/$to', {
+               $from: this.config.sourceLanguage,
+               $to: this.config.targetLanguage
+               // $provider: this.provider // TOOD: Pass the MT provider, else 
default will be used
        } );
+
+       return this.getCXServerToken().then( function ( token ) {
+               return $.ajax( {
+                       type: 'post',
+                       url: translateURL,
+                       headers: {
+                               Authorization: token
+                       },
+                       data: { html: source }
+               } );
+       } );
+};
+
+mw.cx.ui.TranslationView.static.cxserverToken = {
+       expires: undefined,
+       jwt: undefined,
+       promise: undefined
+};
+
+/**
+ * Fetch token for authentication with cxserver.
+ *
+ * @return {jQuery.Promise}
+ */
+mw.cx.ui.TranslationView.prototype.getCXServerToken = function () {
+       var now = Math.floor( Date.now() / 1000 ),
+               cxserverToken = mw.cx.ui.TranslationView.static.cxserverToken;
+
+       // If request in progress, wait for it
+       if ( cxserverToken.promise ) {
+               return cxserverToken.promise;
+       }
+
+       // Return cached token if fresh and not expiring soon.
+       // And hope that client clock is at correct time.
+       if (
+               cxserverToken.expires !== undefined &&
+               cxserverToken.expires + 5 < now
+       ) {
+               return $.Deferred().resolve( cxserverToken.jwt );
+       }
+
+       // (Re-)fetch cxserver token
+       cxserverToken.promise = ( new mw.Api() )
+               .postWithToken( 'csrf', {
+                       action: 'cxtoken'
+               } )
+               .always( function () {
+                       cxserverToken.promise = undefined;
+               } )
+               .then( function ( response ) {
+                       cxserverToken.jwt = response.jwt;
+                       cxserverToken.expires = response.exp;
+
+                       return response.jwt;
+               },
+               // Not all MT services require token, so let the caller try
+               // with empty token to see if it fails.
+               function () {
+                       return $.Deferred().resolve( '' );
+               }
+               );
+
+       return cxserverToken.promise;
 };
 
 mw.cx.ui.TranslationView.prototype.showCategories = function () {

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Idf2bd169f2bd9b890472c8615fb0a775469e60b4
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/ContentTranslation
Gerrit-Branch: master
Gerrit-Owner: Santhosh <santhosh.thottin...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to