2 import {Component, IComponent} from "../models/components/component";
3 import {ICsarComponent} from "../models/csar-component";
4 import {IAppConfigurtaion, IApi} from "../models/app-config";
5 import {IFileDownload} from "../models/file-download";
6 import {Resource} from "../models/components/resource";
7 import {ComponentFactory} from "../utils/component-factory";
9 interface IOnboardingService {
10 getOnboardingComponents():ng.IPromise<Array<IComponent>>;
11 getComponentFromCsarUuid(csarUuid:string):ng.IPromise<Component>;
12 downloadOnboardingCsar(packageId:string):ng.IPromise<IFileDownload>;
15 export class OnboardingService implements IOnboardingService {
17 static '$inject' = ['$http', '$q', 'sdcConfig', 'ComponentFactory'];
20 constructor(private $http:ng.IHttpService,
21 private $q:ng.IQService,
22 private sdcConfig:IAppConfigurtaion,
23 private ComponentFactory:ComponentFactory) {
24 this.api = sdcConfig.api;
27 getOnboardingComponents = ():ng.IPromise<Array<IComponent>> => {
28 let defer = this.$q.defer<Array<IComponent>>();
29 this.$http.get(this.api.GET_onboarding)
30 .then((response:any) => {
31 let onboardingComponents:Array<ICsarComponent> = response.data.results;
32 let componentsList:Array<IComponent> = new Array();
34 onboardingComponents.forEach((obc:ICsarComponent) => {
35 let component:Component = this.ComponentFactory.createFromCsarComponent(obc);
36 componentsList.push(component);
39 defer.resolve(componentsList);
41 defer.reject(response);
47 downloadOnboardingCsar = (packageId:string):ng.IPromise<IFileDownload> => {
48 let defer = this.$q.defer();
50 url: this.api.GET_onboarding + "/" + packageId,
54 .then((response:any) => {
55 defer.resolve(response.data);
63 getComponentFromCsarUuid = (csarUuid:string):ng.IPromise<Component> => {
64 let defer = this.$q.defer<Component>();
65 this.$http.get(this.api.root + this.api.GET_component_from_csar_uuid.replace(':csar_uuid', csarUuid))
66 .then((response:any) => {
67 let component:Resource;
68 // If the status is 400, this means that the component not found.
69 // I do not want to return error from server, because a popup will appear in client with the error.
70 // So returning success (200) with status 400.
71 if (response.data.status !== 400) {
72 component = new Resource(null, this.$q, <Resource>response.data);
74 defer.resolve(component);
76 defer.reject(response.data);