Could you please review the below AngularJS service. What it does is centralize all (relevant) HTTP action calls.
Do you see any chances for improvement?:
"use strict";
angular.module("services.api")
.factory("apiService", [
"$http",
"$q",
"coreUtilityService",
"constantsServiceNames",
"constantsApiRequestFor",
function (
$http,
$q,
coreUtilityService,
constantsServiceNames,
constantsApiRequestFor
) {
// === START SERVICE === //
var self = {};
self.Name = constantsServiceNames.ApiService;
self.ExecuteRequest = function(verb, fullUri, data) {
var defer = $q.defer();
verb = verb.toLowerCase();
//start with the uri
var httpArgs = [fullUri];
if (verb.match(/post|put/)) {
httpArgs.push(data);
}
$http[verb].apply(null, httpArgs)
.success(function (response) {
defer.resolve(response);
})
.error(function (response, status) {
defer.reject("HTTP Error: " + status);
});
return defer.promise;
}
/*
Do a request to any RESTful receiver. Meant for external use only, use DoViewRequest
for internal requests.
*/
self.DoRequest = function (verb, uri, data) {
var fullUrl = coreUtilityService.GetBaseUrl(constantsApiRequestFor.WebAPI, uri);
return self.ExecuteRequest(verb, fullUrl, data);
};
/*
This will target an MVC View of the client application. The url will be different.
*/
self.DoViewRequest = function (verb, uri) {
var fullUrl = coreUtilityService.GetBaseUrl(constantsApiRequestFor.View, uri);
return self.ExecuteRequest(verb, fullUrl, null);
};
// === PUBLIC === //
return {
Get: function (uri) {
return self.DoRequest("get", uri);
},
Post: function (uri, data) {
return self.DoRequest("post", uri, data);
},
Put: function (uri, data) {
return self.DoRequest("put", uri, data);
},
Delete: function (uri) {
return self.DoRequest("delete", uri);
},
View: function (uri) {
return self.DoViewRequest("get", uri);
}
}
// === END SERVICE === //
}]);