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 const getAuthorization
= () => 'Basic ' + window
.sessionStorage
.getItem("auth");
28 const getStateApiPath
= (operation
, id
) =>
29 catalogUtils
.getSearchParams(window
.location
).upload_server
+ ':4567/api/' + operation
+ '/' + id
+ '/state';
31 const getComposerApiPath
= (api
) =>
32 window
.location
.origin
+ '/composer/api/' + api
+ '?api_server=' + catalogUtils
.getSearchParams(window
.location
).api_server
;
38 message
: "Completely successfully"
50 message
: "In progress"
53 function ajaxFetch(url
, operation
, resolve
, reject
, method
= 'GET', input
, urlOverride
) {
54 let credentials
= 'same-origin';
55 let body
= input
? JSON
.stringify(input
) : null;
56 let headers
= new Headers();
57 headers
.append('Authorization', getAuthorization());
58 headers
.append('Accept', 'application/json');
60 headers
.append('Content-Type', 'application/json');
63 fetch(url
, {method
, credentials
, headers
, body
})
64 .then(checkStatusGetJson
)
68 function checkStatusGetJson(response
) {
69 if (response
.status
>= 200 && response
.status
< 300) {
70 return response
.json();
72 var error
= new Error(response
.statusText
)
73 error
.status
= response
.status
;
74 error
.statusText
= response
.statusText
;
79 function handleSuccess (data
) {
80 if (typeof data
== 'string') {
81 data
= JSON
.parse(data
);
90 function handleError (data
) {
91 if (typeof data
== 'string') {
92 data
= JSON
.parse(data
);
102 const CatalogPackageManagerSource
= {
104 requestCatalogPackageDownload: function () {
106 remote: function (state
, download
, format
, grammar
, schema
) {
107 return new Promise((resolve
, reject
) => {
108 // we need an initial status for UI (server does not send)
109 const setStatusBeforeResolve
= (response
) => {
110 response
.data
.status
= 'download-requested';
114 "package-type": download
['catalogItems'][0]['uiState']['type'].toUpperCase(),
115 "package-id": download
.ids
,
116 "export-format": format
&& format
.toUpperCase() || 'YAML',
117 "export-grammar": grammar
&& grammar
.toUpperCase() || 'OSM',
118 "export-schema": schema
&& schema
.toUpperCase() || "RIFT"
120 const path
= getComposerApiPath('package-export');
121 ajaxFetch(path
, download
, setStatusBeforeResolve
, reject
, 'POST', data
, true);
124 success
: CatalogPackageManagerActions
.downloadCatalogPackageStatusUpdated
,
125 error
: CatalogPackageManagerActions
.downloadCatalogPackageError
129 requestCatalogPackageDownloadStatus: function() {
131 remote: function(state
, download
) {
132 const transactionId
= download
.transactionId
;
133 return new Promise(function(resolve
, reject
) {
134 const path
= getStateApiPath('export', transactionId
);
135 ajaxFetch(path
, download
, resolve
, reject
);
138 success
: CatalogPackageManagerActions
.downloadCatalogPackageStatusUpdated
,
139 error
: CatalogPackageManagerActions
.downloadCatalogPackageError
143 requestCatalogPackageCopy: function () {
145 remote: function (state
, operationInfo
) {
146 return new Promise((resolve
, reject
) => {
147 // we need an initial status for UI (server does not send)
148 const successHandler
= (response
) => {
149 const status
= response
.data
.output
.status
;
150 const state
= status
=== "COMPLETED" ? SUCCESS
: status
=== "FAILED" ? FAILED
: PENDING
;
151 state
.progress
= 25; // put something
152 let operation
= Object
.assign({}, operationInfo
, state
);
153 operation
.transactionId
= response
.data
.output
['transaction-id'];
156 const failHandler
= (response
) => {
157 let operation
= Object
.assign({}, this, FAILED
);
161 "package-type": operationInfo
.args
.packageType
,
162 "package-id": operationInfo
.args
.id
,
163 "package-name": operationInfo
.args
.name
165 const path
= getComposerApiPath('package-copy');
166 ajaxFetch(path
, operationInfo
, successHandler
, failHandler
, 'POST', data
, true);
169 success
: CatalogPackageManagerActions
.updateStatus
,
170 error
: CatalogPackageManagerActions
.updateStatus
174 requestCatalogPackageCopyStatus: function() {
176 remote: function(state
, operation
) {
177 return new Promise(function(resolve
, reject
) {
178 const successHandler
= (response
) => {
179 const status
= response
.data
.status
;
180 const state
= status
=== "COMPLETED" ? SUCCESS
: status
=== "FAILED" ? FAILED
: PENDING
;
181 state
.progress
= state
.pending
? operation
.progress
+ ((100 - operation
.progress
) / 2) : 100;
182 let newOp
= Object
.assign({}, operation
, state
);
185 const failHandler
= (response
) => {
186 reject(Object
.assign({}, operation
, FAILED
));
188 const path
= getComposerApiPath('package-manager/jobs/' + operation
.transactionId
);
189 ajaxFetch(path
, operation
, successHandler
, failHandler
);
192 success
: CatalogPackageManagerActions
.updateStatus
,
193 error
: CatalogPackageManagerActions
.updateStatus
197 requestCatalogPackageUploadStatus: function () {
199 remote: function (state
, upload
) {
200 const transactionId
= upload
.transactionId
;
201 return new Promise(function (resolve
, reject
) {
202 const action
= upload
.riftAction
=== 'onboard' ? 'upload' : 'update';
203 const path
= getStateApiPath(action
, transactionId
);
204 ajaxFetch(path
, upload
, resolve
, reject
);
207 success
: CatalogPackageManagerActions
.uploadCatalogPackageStatusUpdated
,
208 error
: CatalogPackageManagerActions
.uploadCatalogPackageError
214 export default CatalogPackageManagerSource
;