UNPKG

5.88 kBJavaScriptView Raw
1/**
2 * Copyright 2015, 2016 IBM Corp. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17'use strict';
18
19const request = require('request');
20const extend = require('extend');
21const Service = require('./service.js');
22const Api = require('./api.js');
23const url = require('url');
24
25const defaultOptions = {
26 strictSSL: true,
27};
28
29module.exports = function ZosConnect(options) {
30 if (options === null || options === undefined) {
31 throw new Error('An options object is required');
32 }
33
34 if (options.uri === undefined && options.url === undefined) {
35 throw new Error('Required uri or url not specified');
36 }
37
38 this.options = extend(defaultOptions, options);
39
40 if (options.uri === undefined) {
41 let uri = options.url.protocol;
42 uri += '//';
43 uri += options.url.host;
44 this.options.uri = uri;
45 }
46
47 this.getServices = () => {
48 let opOptions = {};
49 opOptions = extend(opOptions, this.options);
50 opOptions.uri += '/zosConnect/services';
51
52 return new Promise(((resolve, reject) => {
53 request.get(opOptions, (error, response, body) => {
54 if (error) {
55 reject(error);
56 } else if (response.statusCode !== 200) {
57 reject(new Error(`Failed to get list of services (${response.statusCode})`));
58 } else {
59 const json = JSON.parse(body);
60 const services = [];
61 for (const service of json.zosConnectServices) {
62 services.push(service.ServiceName);
63 }
64
65 resolve(services);
66 }
67 });
68 }));
69 };
70
71 this.getService = (serviceName) => {
72 let opOptions = {};
73 opOptions = extend(opOptions, this.options);
74 opOptions.uri += `/zosConnect/services/${serviceName}`;
75
76 return new Promise(((resolve, reject) => {
77 request.get(opOptions, (error, response, body) => {
78 if (error) {
79 reject(error);
80 } else if (response.statusCode !== 200) {
81 reject(new Error(`Unable to get service (${response.statusCode})`));
82 } else {
83 const serviceData = JSON.parse(body);
84 const invokeUrl = url.parse(serviceData.zosConnect.serviceInvokeURL);
85 resolve(new Service(opOptions, serviceName,
86 this.options.uri + invokeUrl.pathname + invokeUrl.search));
87 }
88 });
89 }));
90 };
91
92 this.getApis = () => {
93 let opOptions = {};
94 opOptions = extend(opOptions, this.options);
95 opOptions.uri += '/zosConnect/apis';
96 return new Promise(((resolve, reject) => {
97 request.get(opOptions, (error, response, body) => {
98 if (error) {
99 reject(error);
100 } else if (response.statusCode !== 200) {
101 reject(new Error(`Unable to get list of APIs (${response.statusCode})`));
102 } else {
103 const json = JSON.parse(body);
104 const apis = [];
105 for (const api of json.apis) {
106 apis.push(api.name);
107 }
108 resolve(apis);
109 }
110 });
111 }));
112 };
113
114 this.getApi = (apiName) => {
115 let opOptions = {};
116 opOptions = extend(opOptions, this.options);
117 opOptions.uri += `/zosConnect/apis/${apiName}`;
118 return new Promise(((resolve, reject) => {
119 request.get(opOptions, (error, response, body) => {
120 if (error) {
121 reject(error);
122 } else if (response.statusCode !== 200) {
123 reject(new Error(`Unable to get API information (${response.statusCode})`));
124 } else {
125 const json = JSON.parse(body);
126 const apiUrl = url.parse(json.apiUrl);
127 resolve(new Api(opOptions, apiName, this.options.uri + apiUrl.pathname,
128 json.documentation));
129 }
130 });
131 }));
132 };
133
134 this.createApi = (aarFile) => {
135 let opOptions = {};
136 opOptions = extend(opOptions, this.options);
137 opOptions.uri += '/zosConnect/apis';
138 opOptions.method = 'POST';
139 opOptions.body = aarFile;
140 opOptions.headers = {
141 'Content-Type': 'application/zip',
142 };
143 return new Promise(((resolve, reject) => {
144 request(opOptions, (error, response, body) => {
145 if (error) {
146 reject(error);
147 } else if (response.statusCode !== 201) {
148 reject(new Error(`Unable to create API (${response.statusCode})`));
149 } else {
150 const json = JSON.parse(body);
151 const apiUrl = url.parse(json.apiUrl);
152 resolve(new Api(opOptions, json.name, this.options.uri + apiUrl.pathname,
153 json.documentation));
154 }
155 });
156 }));
157 };
158
159 this.createService = (sarFile) => {
160 let opOptions = {};
161 opOptions = extend(opOptions, this.options);
162 opOptions.uri += '/zosConnect/services';
163 opOptions.method = 'POST';
164 opOptions.body = sarFile;
165 opOptions.headers = {
166 'Content-Type': 'application/zip',
167 };
168 return new Promise(((resolve, reject) => {
169 request(opOptions, (error, response, body) => {
170 if (error) {
171 reject(error);
172 } else if (response.statusCode !== 201) {
173 reject(new Error(`Unable to create Service (${response.statusCode})`));
174 } else {
175 const serviceData = JSON.parse(body);
176 const invokeUrl = url.parse(serviceData.zosConnect.serviceInvokeURL);
177 resolve(new Service(opOptions, serviceData.ServiceName,
178 this.options.uri + invokeUrl.pathname + invokeUrl.search));
179 }
180 });
181 }));
182 };
183};