Created
December 14, 2013 20:39
-
-
Save neilff/7964654 to your computer and use it in GitHub Desktop.
angular-http-service
This file contains 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
define( | |
[ 'app', 'services' ], | |
function ( app, services ) { | |
return angular.module('drivecam.services').service('RequestService', | |
['SessionService', '$http', '$resource', '$rootScope', '$cookies', | |
function(SessionService, $http, $resource, $rootScope, $cookies) { | |
/** | |
* @param config {object} - Object which contains all the configuration settings for $http | |
* @return {object} - $http object which success and error methods | |
*/ | |
var makeApiRequest = function(config) { | |
var headers = { | |
'Content-Type': 'application/json; charset=utf-8', | |
'X-Session-Id': (SessionService.checkUserSession()) ? SessionService.getSessionId() : '', | |
'X-Group-Id': (SessionService.checkUserSession()) ? SessionService.getSessionObject().group_id : '', | |
'X-User-Id': (SessionService.checkUserSession()) ? SessionService.getSessionObject().user_id : '' | |
}; | |
// If method isn't defined, use POST | |
if (config.method == undefined) { | |
config.method = 'POST'; | |
} | |
// If data being returned isn't an array, set to false in request | |
if (config.isArray == undefined) { | |
config.isArray = true; | |
} | |
if ( typeof(config.cache) == 'undefined' ) { | |
config.cache = false; | |
} | |
return $http({ | |
url: '/api/' + config.url, | |
headers: headers, | |
method: config.method, | |
data: angular.toJson(config.data), | |
isArray: true, | |
cache: config.cache | |
}); | |
} | |
return { | |
makeApiRequest: makeApiRequest | |
} | |
}]); | |
} | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment