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 $ from 'jquery'
22 import alt
from '../alt'
23 import utils
from '../libraries/utils'
24 import CatalogPackageManagerActions
from '../actions/CatalogPackageManagerActions'
25 let Utils
= require('utils/utils.js');
26 function getApiServerOrigin() {
27 return utils
.getSearchParams(window
.location
).upload_server
+ ':4567';
30 function ajaxRequest(path
, catalogPackage
, resolve
, reject
, method
= 'GET') {
32 url
: getApiServerOrigin() + path
,
34 beforeSend
: Utils
.addAuthorizationStub
,
36 success: function(data
) {
37 if (typeof data
== 'string') {
38 data
= JSON
.parse(data
);
45 error: function(error
) {
46 if (typeof error
== 'string') {
47 error
= JSON
.parse(error
);
54 }).fail(function(xhr
){
55 //Authentication and the handling of fail states should be wrapped up into a connection class.
56 Utils
.checkAuthentication(xhr
.status
);
60 const CatalogPackageManagerSource
= {
62 requestCatalogPackageDownload: function () {
64 remote: function (state
, download
, format
, grammar
) {
65 return new Promise((resolve
, reject
) => {
66 // the server does not add a status in the payload
67 // so we add one so that the success handler will
68 // be able to follow the flow of this download
69 const setStatusBeforeResolve
= (response
= {}) => {
70 response
.data
.status
= 'download-requested';
73 // RIFT-13485 requires to send type (nsd/vnfd) as a path element.
74 // Backend no longer supports mixed multi-package download.
75 // Probably does not even support multi-package download of same type.
76 // Hence, pick the type from the first element.
77 const path
= '/api/export/' + download
['catalogItems'][0]['uiState']['type'] + '?schema=' + format
+ '&grammar=' + grammar
+ '&format=yaml&ids=' + download
.ids
;
78 ajaxRequest(path
, download
, setStatusBeforeResolve
, reject
);
81 success
: CatalogPackageManagerActions
.downloadCatalogPackageStatusUpdated
,
82 error
: CatalogPackageManagerActions
.downloadCatalogPackageError
86 requestCatalogPackageDownloadStatus: function() {
88 remote: function(state
, download
) {
89 const transactionId
= download
.transactionId
;
90 return new Promise(function(resolve
, reject
) {
91 const path
= '/api/export/' + transactionId
+ '/state';
92 ajaxRequest(path
, download
, resolve
, reject
);
95 success
: CatalogPackageManagerActions
.downloadCatalogPackageStatusUpdated
,
96 error
: CatalogPackageManagerActions
.downloadCatalogPackageError
100 requestCatalogPackageUploadStatus: function () {
102 remote: function (state
, upload
) {
103 const transactionId
= upload
.transactionId
;
104 return new Promise(function (resolve
, reject
) {
105 const action
= upload
.riftAction
=== 'onboard' ? 'upload' : 'update';
106 const path
= '/api/' + action
+ '/' + transactionId
+ '/state';
107 ajaxRequest(path
, upload
, resolve
, reject
);
110 success
: CatalogPackageManagerActions
.uploadCatalogPackageStatusUpdated
,
111 error
: CatalogPackageManagerActions
.uploadCatalogPackageError
117 export default CatalogPackageManagerSource
;