UNPKG

4.49 kBJavaScriptView Raw
1/**
2 * Copyright 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 url = require('url');
22
23module.exports = function API(options, apiName, basePath, documentation) {
24 this.options = options;
25 this.apiName = apiName;
26 this.basePath = basePath;
27 this.documentation = documentation;
28
29 this.getApiDoc = (type) => {
30 let opOptions = {};
31 const documentationUri = documentation[type];
32 if (documentationUri === undefined) {
33 return Promise.reject('Documentation not available');
34 }
35 const docUrl = url.parse(documentationUri);
36 const apiUrl = url.parse(this.basePath);
37 opOptions = extend(opOptions, this.options);
38 opOptions.uri = `${apiUrl.protocol}//${apiUrl.host}${docUrl.pathname}`;
39 return new Promise(((resolve, reject) => {
40 request.get(opOptions, (error, response, body) => {
41 if (error !== null) {
42 reject(error);
43 } else if (response.statusCode !== 200) {
44 reject(`Unable to retrieve API documentation (${response.statusCode})`);
45 } else {
46 resolve(body);
47 }
48 });
49 }));
50 };
51
52 this.invoke = (resource, method, content) => {
53 let opOptions = {};
54 opOptions = extend(opOptions, this.options);
55 opOptions.uri = `${basePath}/${resource}`;
56 opOptions.method = method;
57 if (content !== null) {
58 opOptions.body = content;
59 }
60
61 opOptions.json = true;
62 return new Promise(((resolve, reject) => {
63 request(opOptions, (error, response) => {
64 if (error) {
65 reject(error);
66 } else {
67 resolve(response);
68 }
69 });
70 }));
71 };
72
73 this.start = () => {
74 let opOptions = {};
75 opOptions = extend(opOptions, this.options);
76 opOptions.uri += '?status=started';
77 opOptions.method = 'PUT';
78 delete opOptions.body;
79 return new Promise(((resolve, reject) => {
80 request(opOptions, (error, response) => {
81 if (error) {
82 reject(error);
83 } else if (response.statusCode !== 200) {
84 reject(response.statusCode);
85 } else {
86 resolve();
87 }
88 });
89 }));
90 };
91
92 this.stop = () => {
93 let opOptions = {};
94 opOptions = extend(opOptions, this.options);
95 opOptions.uri += '?status=stopped';
96 opOptions.method = 'PUT';
97 delete opOptions.body;
98 return new Promise(((resolve, reject) => {
99 request(opOptions, (error, response) => {
100 if (error) {
101 reject(error);
102 } else if (response.statusCode !== 200) {
103 reject(response.statusCode);
104 } else {
105 resolve();
106 }
107 });
108 }));
109 };
110
111 this.update = (aarFile) => {
112 let opOptions = {};
113 opOptions = extend(opOptions, this.options);
114 return this.stop().then(() => new Promise(((resolve, reject) => {
115 opOptions.method = 'PUT';
116 opOptions.uri += '?status=started';
117 opOptions.body = aarFile;
118 opOptions.headers = {
119 'Content-Type': 'application/zip',
120 };
121 request(opOptions, (error, response, body) => {
122 if (error) {
123 reject(error);
124 } else if (response.statusCode !== 200) {
125 reject(new Error(`Unable to update API (${response.statusCode})`));
126 } else {
127 const json = JSON.parse(body);
128 this.basePath = json.apiUrl;
129 this.documentation = json.documentation;
130 resolve();
131 }
132 });
133 })));
134 };
135
136 this.delete = () => {
137 let opOptions = {};
138 opOptions = extend(opOptions, this.options);
139 opOptions.method = 'DELETE';
140 return new Promise(((resolve, reject) => {
141 request(opOptions, (error, response) => {
142 if (error) {
143 reject(error);
144 } else if (response.statusCode !== 200) {
145 reject(`Unable to delete API (${response.statusCode})`);
146 } else {
147 resolve();
148 }
149 });
150 }));
151 };
152};