1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
/*******************************************************************************
* Copyright (c) 2017 ZTE Corporation.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and the Apache License 2.0 which both accompany this distribution,
* and are available at http://www.eclipse.org/legal/epl-v10.html
* and http://www.apache.org/licenses/LICENSE-2.0
*
* Contributors:
* ZTE - initial API and implementation and/or initial documentation
*******************************************************************************/
import { Injectable } from '@angular/core';
import { Http, RequestOptionsArgs } from '@angular/http';
import { Observable } from 'rxjs/Rx';
import { isNullOrUndefined } from 'util';
import { SwaggerMethod } from '../model/swagger';
import { SwaggerResponse } from '../model/swagger';
import { Swagger, SwaggerSchemaObject } from '../model/swagger';
import { RestConfig } from '../model/rest-config';
import { HttpService } from '../util/http.service';
import { BroadcastService } from './broadcast.service';
import { NoticeService } from './notice.service';
@Injectable()
export class RestService {
private restConfigs: RestConfig[] = [];
// private runtimeURL = '/openoapi/catalog/v1/sys/config';
private runtimeURL = '/api/tenant';
// private msbPublishServiceURL = '/api/msdiscover/v1/publishservicelist';
private msbPublishServiceURL = '/api/mockarray';
constructor(private broadcastService: BroadcastService, private http: Http, private noticeService: NoticeService) {
this.broadcastService.planModel$.subscribe(planModel => {
planModel.configs.restConfigs.forEach(element => {
this.addRestConfig(element);
});
});
this.initSwaggerInfoByMSB();
}
public addRestConfig(config: RestConfig) {
const idSet = new Set<string>();
this.restConfigs.forEach(rc => {
idSet.add(rc.id);
});
if(idSet.has(config.id)) {
return false;
} else {
this.restConfigs.push(config);
return true;
}
}
public newRestConfig(): RestConfig {
const idSet = new Set<string>();
this.restConfigs.forEach(rc => {
idSet.add(rc.id);
});
let index = 0;
for(; index <= idSet.size; index++) {
if(!idSet.has(index + '')) {
break;
}
}
const restConfig = new RestConfig(index.toString(), 'new Config', '', '', '');
return restConfig;
}
// public initSwaggerInfo(restConfig: RestConfig) {
// if (restConfig.dynamic && restConfig.definition) {
// this.getDynamicSwaggerInfo(restConfig.definition).subscribe(response => restConfig.swagger = new Swagger(response));
// } else {
// restConfig.swagger = new Swagger(restConfig.swagger);
// }
// }
public getRestConfigs() {
return this.restConfigs;
}
public getDynamicSwaggerInfo(url: string): Observable<any> {
const options: any = {
headers: {
Accept: 'application/json',
},
};
return this.http.get(url, options);
}
public getSwaggerInfo(id: string): Swagger {
const restConfig = this.restConfigs.find(tmp => tmp.id === id);
return restConfig === undefined ? undefined : restConfig.swagger;
}
public getResponseParameters(swagger: Swagger, interfaceUrl: string, operation: string): SwaggerResponse[] {
const path = swagger.paths[interfaceUrl];
const method: SwaggerMethod = path[operation];
let responses: SwaggerResponse[] = [];
for (const key of Object.keys(method.responses)) {
if (key.startsWith('20') && method.responses[key].schema && method.responses[key].schema.$ref) {
let response: SwaggerResponse = method.responses[key];
responses.push(response);
}
}
return responses;
}
public getDefinition(swagger: Swagger, position: string): SwaggerSchemaObject {
const definitionName = position.substring('#/definitions/'.length);
return swagger.definitions[definitionName];
}
private initSwaggerInfoByMSB(): void {
const options: any = {
headers: {
Accept: 'application/json',
}
};
let restConfigs = this.restConfigs;
this.http.get(this.runtimeURL).subscribe(runtimeResponse => {
const tenant = runtimeResponse.json().tenant;
console.log('Current namespace is:' + tenant);
this.http.get(this.msbPublishServiceURL, { params: { namespace: tenant } }).subscribe(serviceResponse => {
if (!Array.isArray(serviceResponse.json())) {
return;
}
const services = serviceResponse.json();
const protocel = location.protocol.slice(0, location.protocol.length - 1);
const swaggerObservableArray: Observable<any>[] = [];
services.forEach(serviceInfo => {
if ('REST' === serviceInfo.protocol && protocel === serviceInfo.publish_protocol) {
// this service don't have sawgger file.
if ('/activiti-rest' !== serviceInfo.publish_url) {
const id = serviceInfo.serviceName + '.' + serviceInfo.version;
this.addRestConfig(new RestConfig(id, serviceInfo.serviceName, serviceInfo.version, serviceInfo.publish_url, ''));
let swaggerUrl = '';
if (undefined !== serviceInfo.swagger_url && '' !== serviceInfo.swagger_url) {
swaggerUrl = serviceInfo.publish_url + '/' + serviceInfo.swagger_url;
} else {
// default swagger url is: '/swagger.json'
swaggerUrl = serviceInfo.publish_url + '/swagger.json';
}
swaggerObservableArray.push(this.http.get(swaggerUrl, options).timeout(5000).catch((error): Observable<any> => {
console.log('Request swagger from:"' + swaggerUrl + '" faild!');
return Observable.of(null);
}));
}
}
});
Observable.forkJoin(swaggerObservableArray).subscribe(
responses => {
let deleteArray: number[] = [];
responses.forEach((response, index) => {
// mark http get failed request index or set the swagger into restConfigs
if (null === response) {
deleteArray.push(index);
} else {
try {
const swagger = response.json();
restConfigs[index].swagger = new Swagger(swagger);
} catch (e) {
deleteArray.push(index);
console.warn('Do not support this sawgger file format:' + response.text());
}
}
});
console.log('Get all swagger file finish.');
// delete failed request from all restConfigs array
deleteArray.reverse();
deleteArray.forEach(deleteIndex => {
restConfigs.splice(deleteIndex, 1);
});
this.broadcastService.broadcast(this.broadcastService.updateModelRestConfig, restConfigs);
console.log('Load all swagger finished.');
}
);
});
});
}
}
|