Created
October 11, 2013 14:48
-
-
Save tangorri/6935987 to your computer and use it in GitHub Desktop.
I try to wrap my http access
but query (via authenticatedQuery) does not return a promise
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Wrapper for http services | |
angular.module('App').factory('Http', function($q, $http) { | |
var defaultHeaders = {}; | |
var token = null; | |
var BID = null; | |
function query(config){ | |
if (bid == null || token == null) { | |
token = getToken(). | |
then( function success(value) { | |
bid = getBID() | |
.then( function success(value) { | |
var ret = authenticatedQuery(config); | |
return authenticatedQuery(config); | |
}) | |
}) | |
} else { | |
return authenticatedQuery(config); | |
} | |
} | |
function authenticatedQuery(config) { | |
var configToPass = config; | |
if (configToPass.headers == undefined) configToPass.headers = {}; | |
for(var prop in defaultHeaders) { | |
configToPass.headers[prop] = defaultHeaders[prop]; | |
} | |
return $http(configToPass); | |
} | |
function getToken() { | |
var token = $q.defer(); | |
$http({ | |
method: 'POST', | |
url: AUTHEN_BASE_URL, | |
data: AUTHEN_PARAMS, | |
headers: {'Content-Type': 'application/x-www-form-urlencoded'} | |
}). | |
then( | |
function success(response) { | |
console.log('token success get ' + response.data.body); | |
defaultHeaders = { | |
'X-EZ-USER': '[email protected]', | |
'X-EZ-TOKEN': response.data.body | |
}; | |
auth = true; | |
token.resolve(response.data.body); | |
}, | |
function error(reason) { | |
console.error('error while loading token: '); | |
} | |
); | |
return token.promise; | |
} | |
function getBID() { | |
var BID = $q.defer(); | |
query({ | |
method: 'GET', | |
url: AUTHEN_BID_URL, | |
withCredentials: true | |
}).then( | |
function success(data, status, headers, config) { | |
// all in data (ez internal format) | |
console.log('BID get : ' + data.data.body); | |
BID.resolve(data.data.body); | |
}, | |
function error(data, status, headers, config) { | |
console.error('error whie get BID : ') | |
} | |
) | |
return BID.promise; | |
} | |
return { | |
query : function(config) { | |
return query(config); | |
} | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment