2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
8 * Unless otherwise specified, all software contained herein is licensed
9 * under the Apache License, Version 2.0 (the "License");
10 * you may not use this software except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * Unless otherwise specified, all documentation contained herein is licensed
22 * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23 * you may not use this documentation except in compliance with the License.
24 * You may obtain a copy of the License at
26 * https://creativecommons.org/licenses/by/4.0/
28 * Unless required by applicable law or agreed to in writing, documentation
29 * distributed under the License is distributed on an "AS IS" BASIS,
30 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31 * See the License for the specific language governing permissions and
32 * limitations under the License.
34 * ============LICENSE_END============================================
39 * Created by doritrieur on 12/3/15.
44 class WidgetsService {
45 constructor($q, $log, $http, conf, uuid, utilsService) {
51 this.utilsService = utilsService;
55 let deferred = this.$q.defer();
56 this.$log.info('WidgetsService::getUserWidgets');
59 url: this.conf.api.widgets,
62 'X-Widgets-Type': 'all',
63 'X-ECOMP-RequestID':this.uuid.generate()
66 // If response comes back as a redirected HTML page which IS NOT a success
67 if (this.utilsService.isValidJSON(res) == false) {
68 var msg = 'WidgetsService::getUserWidgets Failed';
72 // this.$log.info('WidgetsService::getUserWidgets Succeeded');
73 deferred.resolve(res.data);
77 deferred.reject(status);
79 return deferred.promise;
83 let deferred = this.$q.defer();
84 this.$log.info('WidgetsService::getManagedWidgets');
87 url: this.conf.api.widgets,
90 'X-Widgets-Type': 'managed',
91 'X-ECOMP-RequestID':this.uuid.generate()
94 // If response comes back as a redirected HTML page which IS NOT a success
95 if (this.utilsService.isValidJSON(res)== false) {
96 deferred.reject('WidgetsService::getManagedWidgets Failed');
97 } else if(Object.keys(res).length == 0 && this.utilsService.isValidJSON(res)) {
98 deferred.resolve(null);
100 this.$log.info('WidgetsService::getManagedWidgets Succeeded');
101 deferred.resolve(res.data);
105 deferred.reject(status);
107 return deferred.promise;
110 createWidget(widgetData) {
111 let deferred = this.$q.defer();
112 this.$log.info('WidgetsService::createWidget');
115 url: this.conf.api.widgets,
119 'X-ECOMP-RequestID':this.uuid.generate()
122 // If response comes back as a redirected HTML page which IS NOT a success
123 if (this.utilsService.isValidJSON(res)== false) {
124 deferred.reject('WidgetsService::createWidget Failed');
126 this.$log.info('WidgetsService::createWidget Succeeded');
127 deferred.resolve(res.data);
131 deferred.reject(errRes);
133 return deferred.promise;
136 updateWidget(widgetId, widgetData) {
137 let deferred = this.$q.defer();
138 this.$log.info('WidgetsService::updateWidget');
139 let url = this.conf.api.widgets + '/' + widgetId;
146 'X-ECOMP-RequestID':this.uuid.generate()
149 // If response comes back as a redirected HTML page which IS NOT a success
150 if (this.utilsService.isValidJSON(res)== false) {
151 deferred.reject('WidgetsService::updateWidget Failed');
153 this.$log.info('WidgetsService::updateWidget Succeeded');
154 deferred.resolve(res.data);
158 deferred.reject(errRes);
160 return deferred.promise;
163 deleteWidget(widgetId) {
164 let deferred = this.$q.defer();
165 this.$log.info('WidgetsService::deleteWidget');
166 let url = this.conf.api.widgets + '/' + widgetId;
172 'X-ECOMP-RequestID':this.uuid.generate()
175 // If response comes back as a redirected HTML page which IS NOT a success
176 if (this.utilsService.isValidJSON(res)== false) {
177 deferred.reject('WidgetsService::deleteWidget Failed');
179 this.$log.info('WidgetsService::deleteWidget Succeeded');
180 deferred.resolve(res.data);
184 deferred.reject(status);
186 return deferred.promise;
189 checkIfWidgetUrlExists(urlToValidate) {
190 let deferred = this.$q.defer();
191 this.$log.info('WidgetsService::checkIfWidgetUrlExists:' + urlToValidate);
197 url: this.conf.api.widgetsValidation,
201 'X-ECOMP-RequestID':this.uuid.generate()
204 //if exists return true
205 deferred.resolve(true);
208 if (this.utilsService.isValidJSON(res)== false) {
209 deferred.reject('WidgetsService::checkIfWidgetUrlExists Failed');
211 if (response.data.status === 404) {
212 deferred.resolve(false);
214 deferred.reject(response.data);
218 return deferred.promise;
222 WidgetsService.$inject = ['$q', '$log', '$http', 'conf','uuid4', 'utilsService'];
223 angular.module('ecompApp').service('widgetsService', WidgetsService)