An AngularJS service for using RESTful Webservices with an build-in auto-mechanism for detecting access tokens.
0.0.1.2
$ bower install ambersive-rest
You first have to declare the 'ambersive.rest' module dependency inside your app module (perhaps inside your app main module).
angular.module('app', ['ambersive.rest']);
angular.module('app').controller('DemoController', function($scope, RestSrv) {
// GET
RestSrv.call({
'method': 'GET',
'url': 'httP://api.example.com/users/1'
},function(result){
// result = object {'status':STATUSCODE (integer),'data':OBJECCT}
});
// POST
RestSrv.call({
'method': 'POST',
'url': 'httP://api.example.com/users/1',
'data':OBJECT/ARRAY
},function(result){
// result = object {'status':STATUSCODE (integer),'data':OBJECCT}
});
// PUT
RestSrv.call({
'method': 'PUT',
'url': 'httP://api.example.com/users/1',
'data':OBJECT/ARRAY
},function(result){
// result = object {'status':STATUSCODE (integer),'data':OBJECCT}
});
// DELETE
RestSrv.call({
'method': 'DELETE',
'url': 'httP://api.example.com/users/1'
},function(result){
// result = object {'status':STATUSCODE (integer),'data':OBJECCT}
});
});
You an configurate few things like storageName, tokenName etc.
This value determine the name of the access token.
RestSrv.config.auth.tokenName = 'NAME'; // Default value: accessToken
This value determine the tokenType of the access token.
RestSrv.config.auth.tokenType = 'NAME'; // Default value: 'Bearer '
This value determine the name of the cookie, in which the access token will be safed.
RestSrv.config.auth.storageName = 'NAME'; // Default value: accessToken
RestSrv.config.auth.storageValidDays ='' // Default value: 7
This service provides an easy way to determine an error handling for 401 and 403 status codes. If you want to set a error handler insert the following code.
RestSrv.config.errorHandling.on401 = function(callbackFN){
// Insert here your code
// Callback method fires the normal response mechanism
});
RestSrv.config.errorHandling.on403 = function(callbackFN){
// Insert here your code
// Callback method fires the normal response mechanism
});
MIT