Clamp react ui to use url pathname dynamically instead of hardcoded prefix for callin...
[clamp.git] / ui-react / src / api / LoopActionService.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  *
22  */
23
24 export default class LoopActionService{
25
26         static performAction(cl_name, uiAction) {
27                 console.info("LoopActionService perform action: " + uiAction + " closedloopName=" + cl_name);
28                 const svcAction = uiAction.toLowerCase();
29                 return fetch(window.location.pathname + "restservices/clds/v2/loop/" + svcAction + "/" + cl_name, {
30                                 method: 'PUT',
31                                 credentials: 'same-origin'
32                         })
33                 .then(function (response) {
34                         if (response.ok) {
35                                 return response.json();
36                         } else {
37                                 return Promise.reject("Perform action failed with code:" + response.status);
38                         }
39                 })
40                 .then(function (data) {
41                         console.info("Action Successful: " + uiAction);
42                         return data;
43                 })
44                 .catch(function(error) {
45                         console.info("Action Failure: " + uiAction);
46                         return Promise.reject(error);
47                 });
48         }
49
50
51         static refreshStatus(cl_name) {
52                 console.info("Refresh the status for closedloopName=" + cl_name);
53
54                 return fetch(window.location.pathname + "restservices/clds/v2/loop/getstatus/" + cl_name, {
55                         method: 'GET',
56                         credentials: 'same-origin'
57                 })
58                 .then(function (response) {
59                         if (response.ok) {
60                                 return response.json();
61                         } else {
62                                 return Promise.reject("Refresh status failed with code:" + response.status);
63                         }
64                 })
65                 .then(function (data) {
66                         console.info ("Refresh status Successful");
67                         return data;
68                 })
69                 .catch(function(error) {
70                         console.info ("Refresh status failed:", error);
71                         return Promise.reject(error);
72                 });
73         }
74 }