diff --git a/bower.json b/bower.json index 301702a..00d52f3 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "angular-google-gapi", - "version": "1.0.0-beta.1", + "version": "1.0.0-beta.2", "homepage": "https://maximepvrt.github.io/angular-google-gapi", "authors": [ "maximepvrt " diff --git a/dist/angular-google-gapi.js b/dist/angular-google-gapi.js index c2343a6..e19d69a 100644 --- a/dist/angular-google-gapi.js +++ b/dist/angular-google-gapi.js @@ -1,6 +1,6 @@ /** * An AngularJS module for use all Google Apis and your Google Cloud Endpoints - * @version 1.0.0-beta.1 + * @version 1.0.0-beta.2 * @link https://github.com/maximepvrt/angular-google-gapi */ @@ -94,34 +94,35 @@ } //exponentialBackoff - function retryExecute(actionPromise, args) { - var queryResults = $q.defer(); - var iter = 0; - retry(actionPromise, iter); - function retry(actionPromise, iter) { - actionPromise.apply(this, args).then(function(body) { - queryResults.resolve(body); - }).catch(function(error){ - if((error.code == 403 && error.message.toLowerCase().indexOf('limit exceeded')>-1) || error.code == 503){ - var base = 2; - var ms = 1000; - var randomMilliseconds = Math.floor((Math.random() * 1000) + 1); - if(iter < 5){ - setTimeout(function(){ - retry(actionPromise, ++iter); - }, (ms * Math.pow(base, iter)) + randomMilliseconds); - } - else{ - queryResults.reject(error); - } - } - else{ - queryResults.reject(error); - } - }); - } - return queryResults.promise; - } + function retryExecute(actionPromise, args) { + var queryResults = $q.defer(); + var iter = 0; + retry(actionPromise, iter); + function retry(actionPromise, iter) { + actionPromise.apply(this, args).then(function(body) { + queryResults.resolve(body); + }).catch(function(error){ + if((error.code == 403 && error.message.toLowerCase().indexOf('limit exceeded')>-1) || error.code == 503){ + var base = 2; + var ms = 1000; + var randomMilliseconds = Math.floor((Math.random() * 1000) + 1); + if(iter < 5){ + setTimeout(function(){ + retry(actionPromise, ++iter); + }, (ms * Math.pow(base, iter)) + randomMilliseconds); + } + else{ + queryResults.reject(error); + } + } + else{ + queryResults.reject(error); + } + }); + } + return queryResults.promise; + } + return { @@ -273,7 +274,7 @@ var deferred = $q.defer(); signin(false, function() { getUser().then(function (user) { - deferred.resolve(); + deferred.resolve(user); }, function () { deferred.reject(); }); @@ -328,6 +329,7 @@ }]); })(); + (function() { 'use strict'; angular.module('angular-google-gapi').factory('GClient', ['$document', '$q', '$window', diff --git a/dist/angular-google-gapi.min.js b/dist/angular-google-gapi.min.js index 748d774..9fbca10 100644 --- a/dist/angular-google-gapi.min.js +++ b/dist/angular-google-gapi.min.js @@ -1,6 +1,6 @@ /** * An AngularJS module for use all Google Apis and your Google Cloud Endpoints - * @version 1.0.0-beta.1 + * @version 1.0.0-beta.2 * @link https://github.com/maximepvrt/angular-google-gapi */ -!function(){"use strict";angular.module("angular-google-gapi",[])}(),function(){"use strict";angular.module("angular-google-gapi").factory("GApi",["$q","GClient","GData","$window",function(e,n,t,o){function r(e,n,t,o,r){var i={};i.api=e,i.apiLoad=!1,i.method=n,i.params=t,i.auth=o,i.deferred=r,f.push(i)}function i(t,r,i){var a=e.defer();return n.get().then(function(){o.gapi.client.load(t,r,void 0,i).then(function(e){var n={api:t,version:r,url:i};e&&e.hasOwnProperty("error")?(console.log(r),a.reject(n)):(a.resolve(n),l.push(t),u(t))})}),a.promise}function u(e){for(var n=e,o=0;o-1&&f.splice(o--,1))}}function a(e,n,t){for(var r=n.split("."),e=o.gapi.client[e],i=0;i-1?c(n,t,o,u):r(n,t,o,i,u),u.promise}var l=[],f=[];return{executeCallbacks:function(){u()},load:i,createRequest:a,execute:function(e,n,t){return 3==arguments.length?s(e,n,t,!1):2==arguments.length?s(e,n,null,!1):void 0},executeAuth:function(e,n,t){return 3==arguments.length?s(e,n,t,!0):2==arguments.length?s(e,n,null,!0):void 0}}}])}(),function(){"use strict";angular.module("angular-google-gapi").factory("GAuth",["$rootScope","$q","GClient","GApi","GData","$interval","$window","$location",function(e,n,t,o,r,i,u,a){function c(){var e=n.defer();return 0==p?t.get().then(function(){u.gapi.client.load("oauth2","v2",function(){p=!0,e.resolve()})}):e.resolve(),e.promise}function s(e,n){c().then(function(){var t={client_id:g,scope:v,immediate:!1,authuser:-1,response_type:h};e&&(t.user_id=r.getUserId(),t.immediate=!0),void 0!=d&&(t.hd=d),u.gapi.auth.authorize(t,n)})}function l(){function e(n){if("https://accounts.google.com"===n.origin){var r=JSON.parse(n.data);u.removeEventListener("message",e),r=t(r.a[0],"code"),void 0==r?o.reject():o.resolve(r)}}function t(e,n){n=n.replace(/[[]/,"[").replace(/[]]/,"]");var t=n+"=([^&#]*)",o=new RegExp(t),r=o.exec(e);return null==r?void 0:r[1]}var o=n.defer(),r=a.protocol()+"//"+a.hostname();(""!=a.port()||443!=a.port()&&"https"==a.protocol())&&(r=r+":"+a.port());u.open("https://accounts.google.com/o/oauth2/auth?scope="+encodeURI(v)+"&redirect_uri=postmessage&response_type=code&client_id="+g+"&access_type=offline&approval_prompt=force&origin="+r,null,"width=800, height=600");return u.addEventListener("message",e),o.promise}function f(){var e=n.defer();return u.gapi.client.oauth2.userinfo.get().execute(function(n){n.code?e.reject():(r.isLogin(!0),o.executeCallbacks(),n.name&&0!==n.name.length||(n.name=n.email),r.getUser(n),e.resolve(n))}),e.promise}var g,p=!1,d=void 0,v="https://www.googleapis.com/auth/userinfo.email",h="token id_token";return{setClient:function(e){g=e},setDomain:function(e){d=e},setScope:function(e){v=e},checkAuth:function(){var e=n.defer();return s(!0,function(){f().then(function(n){e.resolve(n)},function(){e.reject()})}),e.promise},login:function(){var e=n.defer();return s(!1,function(){f().then(function(n){e.resolve()},function(){e.reject()})}),e.promise},setToken:function(e){var t=n.defer();return c().then(function(){u.gapi.auth.setToken(e),f().then(function(){t.resolve()},function(){t.reject()})}),t.promise},getToken:function(){var e=n.defer();return c().then(function(){e.resolve(u.gapi.auth.getToken())}),e.promise},logout:function(){var e=n.defer();return c().then(function(){u.gapi.auth.setToken(null),r.isLogin(!1),r.getUser(null),e.resolve()}),e.promise},offline:function(){var e=n.defer();return l().then(function(n){e.resolve(n)},function(){e.reject()}),e.promise}}}])}(),function(){"use strict";angular.module("angular-google-gapi").factory("GClient",["$document","$q","$window",function(e,n,t){function o(o){var r=n.defer();t._gapiOnLoad=function(){r.resolve()};var i=e[0].createElement("script");return i.onerror=function(e){$timeout(function(){r.reject(e)})},i.src=o,e[0].body.appendChild(i),r.promise}var r=!1,i=!1,u="https://apis.google.com/js/client.js?onload=_gapiOnLoad",a=null,c=[];return{get:function(){var e=n.defer();return r?e.resolve():i?c.push(e):(i=!0,o(u).then(function(){t.gapi.client.setApiKey(a),r=!0,i=!1,e.resolve();for(var n=0;n-1&&g.splice(o--,1))}}function a(e,n,t){for(var r=n.split("."),e=o.gapi.client[e],i=0;i-1?c(n,t,o,u):r(n,t,o,i,u),u.promise}function l(n,t){function o(e,n){e.apply(this,t).then(function(e){r.resolve(e)})["catch"](function(t){if(403==t.code&&t.message.toLowerCase().indexOf("limit exceeded")>-1||503==t.code){var i=2,u=1e3,a=Math.floor(1e3*Math.random()+1);5>n?setTimeout(function(){o(e,++n)},u*Math.pow(i,n)+a):r.reject(t)}else r.reject(t)})}var r=e.defer(),i=0;return o(n,i),r.promise}var f=[],g=[];return{executeCallbacks:function(){u()},load:i,createRequest:a,execute:function(e,n,t){return 3==arguments.length?s(e,n,t,!1):2==arguments.length?s(e,n,null,!1):void 0},executeAuth:function(e,n,t){return 3==arguments.length?l(s,arguments):2==arguments.length?l(s,arguments):void 0}}}])}(),function(){"use strict";angular.module("angular-google-gapi").factory("GAuth",["$rootScope","$q","GClient","GApi","GData","$interval","$window","$location",function(e,n,t,o,r,i,u,a){function c(){var e=n.defer();return 0==p?t.get().then(function(){u.gapi.client.load("oauth2","v2",function(){p=!0,e.resolve()})}):e.resolve(),e.promise}function s(e,n){c().then(function(){var t={client_id:g,scope:h,immediate:!1,authuser:-1,response_type:v};e&&(t.user_id=r.getUserId(),t.immediate=!0),void 0!=d&&(t.hd=d),u.gapi.auth.authorize(t,n)})}function l(){function e(n){if("https://accounts.google.com"===n.origin){var r=JSON.parse(n.data);u.removeEventListener("message",e),r=t(r.a[0],"code"),void 0==r?o.reject():o.resolve(r)}}function t(e,n){n=n.replace(/[[]/,"[").replace(/[]]/,"]");var t=n+"=([^&#]*)",o=new RegExp(t),r=o.exec(e);return null==r?void 0:r[1]}var o=n.defer(),r=a.protocol()+"//"+a.hostname();(""!=a.port()||443!=a.port()&&"https"==a.protocol())&&(r=r+":"+a.port());u.open("https://accounts.google.com/o/oauth2/auth?scope="+encodeURI(h)+"&redirect_uri=postmessage&response_type=code&client_id="+g+"&access_type=offline&approval_prompt=force&origin="+r,null,"width=800, height=600");return u.addEventListener("message",e),o.promise}function f(){var e=n.defer();return u.gapi.client.oauth2.userinfo.get().execute(function(n){n.code?e.reject():(r.isLogin(!0),o.executeCallbacks(),n.name&&0!==n.name.length||(n.name=n.email),r.getUser(n),e.resolve(n))}),e.promise}var g,p=!1,d=void 0,h="https://www.googleapis.com/auth/userinfo.email",v="token id_token";return{setClient:function(e){g=e},setDomain:function(e){d=e},setScope:function(e){h=e},checkAuth:function(){var e=n.defer();return s(!0,function(){f().then(function(n){e.resolve(n)},function(){e.reject()})}),e.promise},login:function(){var e=n.defer();return s(!1,function(){f().then(function(n){e.resolve(n)},function(){e.reject()})}),e.promise},setToken:function(e){var t=n.defer();return c().then(function(){u.gapi.auth.setToken(e),f().then(function(){t.resolve()},function(){t.reject()})}),t.promise},getToken:function(){var e=n.defer();return c().then(function(){e.resolve(u.gapi.auth.getToken())}),e.promise},logout:function(){var e=n.defer();return c().then(function(){u.gapi.auth.setToken(null),r.isLogin(!1),r.getUser(null),e.resolve()}),e.promise},offline:function(){var e=n.defer();return l().then(function(n){e.resolve(n)},function(){e.reject()}),e.promise}}}])}(),function(){"use strict";angular.module("angular-google-gapi").factory("GClient",["$document","$q","$window",function(e,n,t){function o(o){var r=n.defer();t._gapiOnLoad=function(){r.resolve()};var i=e[0].createElement("script");return i.onerror=function(e){$timeout(function(){r.reject(e)})},i.src=o,e[0].body.appendChild(i),r.promise}var r=!1,i=!1,u="https://apis.google.com/js/client.js?onload=_gapiOnLoad",a=null,c=[];return{get:function(){var e=n.defer();return r?e.resolve():i?c.push(e):(i=!0,o(u).then(function(){t.gapi.client.setApiKey(a),r=!0,i=!1,e.resolve();for(var n=0;n-1) || error.code == 503){ + var base = 2; + var ms = 1000; + var randomMilliseconds = Math.floor((Math.random() * 1000) + 1); + if(iter < 5){ + setTimeout(function(){ + retry(actionPromise, ++iter); + }, (ms * Math.pow(base, iter)) + randomMilliseconds); + } + else{ + queryResults.reject(error); + } + } + else{ + queryResults.reject(error); + } + }); + } + return queryResults.promise; + } + + return { executeCallbacks : function() { @@ -101,9 +132,9 @@ executeAuth: function(api, method, params){ if(arguments.length == 3) - return execute(api, method, params, true); + return retryExecute(execute, arguments); //return execute(api, method, params, true) if(arguments.length == 2) - return execute(api, method, null, true); + return retryExecute(execute, arguments); //return execute(api, method, null, true) }, } }]);