I am attempting to convert an Ajax call with WSSE
authentication into an AngularJS factory.
The request method is Post
.
The purpose of this conversion is to access the Adobe Analytics Rest API, retrieve data in JSON format, and then visualize it using d3.js.
I am unfamiliar with the properties that can be utilized in an AngularJS $http post
call, making me unsure about the correct approach for implementing WSSE auth
, dataType
, callback
, etc.
Below is the original ajax code obtained from a public github repository:
(function($) {
window.MarketingCloud = {
env: {},
wsse: new Wsse(),
/** Make the api request */
/* callback should follow standard jQuery request format:
* function callback(data)
*/
makeRequest: function (username, secret, method, params, endpoint, callback)
{
var headers = MarketingCloud.wsse.generateAuth(username, secret);
var url = 'https://'+endpoint+'/admin/1.4/rest/?method='+method;
$.ajax(url, {
type:'POST',
data: params,
complete: callback,
dataType: "text",
headers: {
'X-WSSE': headers['X-WSSE']
}
});
}
};
})(jQuery);
This is how the current code is being used with pure JS:
MarketingCloud.makeRequest(username, secret, method, params, endpoint, function(response) {
data = JSON.parse(response.responseText);
});
I aim to transform this into a factory and controller respectively.
Here is what I have implemented for the factory so far:
app.factory('mainFactory', ['$http', function($http) {
var wsse = new Wsse();
return function(username, secret, method, params, endpoint) {
return $http({
method: 'POST',
url: 'https://' + endpoint + '/admin/1.4/rest/?method=' + method,
data: params,
headers: {
'X-WSSE': wsse.generateAuth(username, secret)['X-WSSE']
},
dataType: 'text',
});
};
}]);
And here is my implementation for the controller:
app.controller('mainController', ['$scope', 'mainFactory', function($scope, mainFactory) {
mainFactory.success(function(data) {
$scope.data = data;
});
}]);
Currently facing an error stating
mainFactory.success is not a function
, which suggests that the factory isn't functioning correctly yet.