UI initial implementation.
[validation.git] / ui / src / main / webapp / resources / js / App.Services.js
1 /*
2  * Copyright (c) 2019 AT&T Intellectual Property. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 var AECBlueprintValidationUIApp = angular
18         .module('BlueprintValidationUIManagement');
19
20 AECBlueprintValidationUIApp.factory('restAPISvc', [
21         '$http',
22         'appContext',
23         function($http, appContext) {
24             var svc = [];
25             svc.getRestAPI = function(path, cb) {
26                 return $http({
27                     method : 'GET',
28                     url : appContext + path,
29                     headers : {
30                         'Content-Type' : "application/json",
31                         'Accept' : "application/json"
32                     }
33                 }).then(
34                         function(response) {
35                             if (response.status == 200) {
36                                 cb(response.data);
37                             } else {
38                                 /* eslint-disable no-console */
39                                 console.log("Get REST API error: "
40                                         + response.statusText);
41                                 /* eslint-enable no-console */
42                                 cb(null);
43                             }
44                         },
45                         function(error) {
46                             /* eslint-disable no-console */
47                             console.log("Get REST API error: "
48                                     + error.statusText);
49                             /* eslint-enable no-console */
50                             cb(null);
51                         });
52             };
53             svc.postRestAPI = function(path, json, cb) {
54                 return $http({
55                     method : 'POST',
56                     url : appContext + path,
57                     headers : {
58                         'Content-Type' : "application/json",
59                         'Accept' : "application/json"
60                     },
61                     data : json
62                 }).then(
63                         function(response) {
64                             if (response.status == 200
65                                     || response.status == 201) {
66                                 cb(response.data);
67                             } else {
68                                 /* eslint-disable no-console */
69                                 console.log("Post REST API error: "
70                                         + response.statusText);
71                                 /* eslint-enable no-console */
72                                 cb(null);
73                             }
74                         },
75                         function(error) {
76                             /* eslint-disable no-console */
77                             console.log("Post REST API error: "
78                                     + error.statusText);
79                             /* eslint-enable no-console */
80                             cb(null);
81                         });
82             };
83             svc.deleteRestAPI = function(path, json) {
84                 return $http({
85                     method : 'DELETE',
86                     url : appContext + path,
87                     headers : {
88                         'Content-Type' : "application/json",
89                         'Accept' : "application/json"
90                     },
91                     data : json
92                 }).then(
93                         function(response) {
94                             if (response.status !== 200) {
95                                 /* eslint-disable no-console */
96                                 console.log("Delete REST API error: "
97                                         + response.statusText);
98                                 /* eslint-enable no-console */
99                             }
100                         },
101                         function(error) {
102                             /* eslint-disable no-console */
103                             console.log("Delete REST API error: "
104                                     + error.statusText);
105                             /* eslint-enable no-console */
106                         });
107             };
108             return svc;
109         } ]);