4 * Copyright 2016 RIFT.IO Inc
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
21 import alt
from '../alt'
22 import catalogUtils
from '../libraries/utils'
23 import CatalogPackageManagerActions
from '../actions/CatalogPackageManagerActions'
24 import Utils
from 'utils/utils.js';
26 let API_SERVER
= catalogUtils
.getSearchParams(window
.location
).api_server
;
27 const FILE_SERVER
= window
.location
.hostname
=== 'localhost'
28 ? API_SERVER
: window
.location
.protocol
+ '//' + window
.location
.hostname
;
31 const getAuthorization
= () => 'Basic ' + window
.sessionStorage
.getItem("auth");
33 const getStateApiPath
= (operation
, id
) =>
34 FILE_SERVER
+ ':8008/mano/' + operation
+ '/' + id
+ '/state';
36 const getComposerApiPath
= (api
) =>
37 window
.location
.origin
+ '/composer/api/' + api
+ '?api_server=' + catalogUtils
.getSearchParams(window
.location
).api_server
;
43 message
: "Completely successfully"
55 message
: "In progress"
58 function ajaxFetch(url
, operation
, resolve
, reject
, method
= 'GET', input
, urlOverride
) {
59 let credentials
= 'same-origin';
60 let body
= input
? JSON
.stringify(input
) : null;
61 let headers
= new Headers();
62 headers
.append('Authorization', getAuthorization());
63 headers
.append('Accept', 'application/json');
65 headers
.append('Content-Type', 'application/json');
68 fetch(url
, {method
, credentials
, headers
, body
})
69 .then(checkStatusGetJson
)
73 function checkStatusGetJson(response
) {
74 if (response
.status
>= 200 && response
.status
< 300) {
75 return response
.json();
77 var error
= new Error(response
.statusText
)
78 error
.status
= response
.status
;
79 error
.statusText
= response
.statusText
;
84 function handleSuccess (data
) {
85 if (typeof data
== 'string') {
86 data
= JSON
.parse(data
);
95 function handleError (data
) {
96 if (typeof data
== 'string') {
97 data
= JSON
.parse(data
);
107 const CatalogPackageManagerSource
= {
109 requestCatalogPackageDownload: function () {
111 remote: function (state
, download
, format
, grammar
, schema
) {
112 return new Promise((resolve
, reject
) => {
113 // we need an initial status for UI (server does not send)
114 const setStatusBeforeResolve
= (response
) => {
115 response
.data
.status
= 'download-requested';
119 "package-type": download
['catalogItems'][0]['uiState']['type'].toUpperCase(),
120 "package-id": download
.ids
,
121 "export-format": format
&& format
.toUpperCase() || 'YAML',
122 "export-grammar": grammar
&& grammar
.toUpperCase() || 'OSM',
123 "export-schema": schema
&& schema
.toUpperCase() || "RIFT"
125 const path
= getComposerApiPath('package-export');
126 ajaxFetch(path
, download
, setStatusBeforeResolve
, reject
, 'POST', data
, true);
129 success
: CatalogPackageManagerActions
.downloadCatalogPackageStatusUpdated
,
130 error
: CatalogPackageManagerActions
.downloadCatalogPackageError
134 requestCatalogPackageDownloadStatus: function() {
136 remote: function(state
, download
) {
137 const transactionId
= download
.transactionId
;
138 return new Promise(function(resolve
, reject
) {
139 const path
= getStateApiPath('export', transactionId
);
140 ajaxFetch(path
, download
, resolve
, reject
);
143 success
: CatalogPackageManagerActions
.downloadCatalogPackageStatusUpdated
,
144 error
: CatalogPackageManagerActions
.downloadCatalogPackageError
148 requestCatalogPackageCopy: function () {
150 remote: function (state
, operationInfo
) {
151 return new Promise((resolve
, reject
) => {
152 // we need an initial status for UI (server does not send)
153 const successHandler
= (response
) => {
154 const status
= response
.data
.output
.status
;
155 const state
= status
=== "COMPLETED" ? SUCCESS
: status
=== "FAILED" ? FAILED
: PENDING
;
156 state
.progress
= 25; // put something
157 let operation
= Object
.assign({}, operationInfo
, state
);
158 operation
.transactionId
= response
.data
.output
['transaction-id'];
161 const failHandler
= (response
) => {
162 let operation
= Object
.assign({}, this, FAILED
);
166 "package-type": operationInfo
.args
.packageType
,
167 "package-id": operationInfo
.args
.id
,
168 "package-name": operationInfo
.args
.name
170 const path
= getComposerApiPath('package-copy');
171 ajaxFetch(path
, operationInfo
, successHandler
, failHandler
, 'POST', data
, true);
174 success
: CatalogPackageManagerActions
.updateStatus
,
175 error
: CatalogPackageManagerActions
.updateStatus
179 requestCatalogPackageCopyStatus: function() {
181 remote: function(state
, operation
) {
182 return new Promise(function(resolve
, reject
) {
183 const successHandler
= (response
) => {
184 const status
= response
.data
.status
;
185 const state
= status
=== "COMPLETED" ? SUCCESS
: status
=== "FAILED" ? FAILED
: PENDING
;
186 state
.progress
= state
.pending
? operation
.progress
+ ((100 - operation
.progress
) / 2) : 100;
187 let newOp
= Object
.assign({}, operation
, state
);
190 const failHandler
= (response
) => {
191 reject(Object
.assign({}, operation
, FAILED
));
193 const path
= getComposerApiPath('package-copy/jobs/' + operation
.transactionId
);
194 ajaxFetch(path
, operation
, successHandler
, failHandler
);
197 success
: CatalogPackageManagerActions
.updateStatus
,
198 error
: CatalogPackageManagerActions
.updateStatus
202 requestCatalogPackageUploadStatus: function () {
204 remote: function (state
, upload
) {
205 const transactionId
= upload
.transactionId
;
206 return new Promise(function (resolve
, reject
) {
207 const action
= upload
.riftAction
=== 'onboard' ? 'import' : 'update';
208 const path
= getComposerApiPath('package-'+action
+'/jobs/' + transactionId
);
209 ajaxFetch(path
, upload
, resolve
, reject
);
212 success
: CatalogPackageManagerActions
.uploadCatalogPackageStatusUpdated
,
213 error
: CatalogPackageManagerActions
.uploadCatalogPackageError
219 export default CatalogPackageManagerSource
;