Advertisement
vitareinforce

Angular HTTP Service

Jun 18th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function() {
  2.  
  3.     angular.module("app")
  4.     .factory("httpService", httpService);
  5.  
  6.     function httpService($http, $q) {
  7.         var service = {
  8.             get: get,
  9.             post: post,
  10.             put: put,
  11.             patch: patch,
  12.             update: update,
  13.             del: del,
  14.         }
  15.  
  16.         return service;
  17.  
  18.         function get(link) {
  19.           var request = $http({
  20.             url: link,
  21.             method: "GET",
  22.             headers: {
  23.               "Content-type": "application/json"
  24.             }
  25.           });
  26.           return(request.then(handleSuccess,handleError));
  27.         }
  28.  
  29.         function post(link, jsonData) {
  30.           var request = $http({
  31.             url: link,
  32.             method: "POST",
  33.             headers: {
  34.               "Content-type": "application/json"
  35.             },
  36.             data: jsonData
  37.           });
  38.           return(request.then(handleSuccess,handleError));
  39.         }
  40.  
  41.         function put(link, jsonData) {
  42.           var request = $http({
  43.             url: link,
  44.             method: "PUT",
  45.             headers: {
  46.               "Content-type": "application/json"
  47.             },
  48.             data: jsonData
  49.           });
  50.           return(request.then(handleSuccess,handleError));
  51.         }
  52.  
  53.         function patch(link, jsonData) {
  54.           var request = $http({
  55.             url: link,
  56.             method: "PATCH",
  57.             headers: {
  58.               "Content-type": "application/json"
  59.             },
  60.             data: jsonData
  61.           });
  62.           return(request.then(handleSuccess,handleError));
  63.         }
  64.  
  65.         function update(link, jsonData) {
  66.           var request = $http({
  67.             url: link,
  68.             method: "UPDATE",
  69.             headers: {
  70.               "Content-type": "application/json"
  71.             },
  72.             data: jsonData
  73.           });
  74.           return(request.then(handleSuccess,handleError));
  75.         }
  76.  
  77.         function del(link, jsonData) {
  78.           var request = $http({
  79.             url: link,
  80.             method: "DELETE",
  81.             headers: {
  82.               "Content-type": "application/json"
  83.             },
  84.             data: jsonData
  85.           });
  86.           return(request.then(handleSuccess,handleError));
  87.         }
  88.  
  89.         function handleSuccess(response) {
  90.           return(response.data);
  91.         }
  92.  
  93.         function handleError(response) {
  94.           if(!angular.isObject(response.data) || !response.data.message) {
  95.             return $q.reject("Unknown Error");
  96.           }
  97.           return $q.reject(response.message);
  98.         }
  99.  
  100.     }
  101.  
  102. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement