1 | const request = require('request')
|
2 | const UnauthorizedError = require('./errors/UnauthorizedError')
|
3 | const NotFoundError = require('./errors/NotFoundError')
|
4 |
|
5 | class DcHttpClient {
|
6 | constructor (userSettings, logger) {
|
7 | this.userSettings = userSettings
|
8 | this.logger = logger
|
9 | this.dcAddress = process.env.SGCLOUD_DC_ADDRESS || 'https://developer-connector.shopgate.cloud'
|
10 | }
|
11 |
|
12 | |
13 |
|
14 |
|
15 |
|
16 |
|
17 | login (username, password, cb) {
|
18 | const opts = {
|
19 | method: 'POST',
|
20 | url: `${this.dcAddress}/login`,
|
21 | json: true,
|
22 | body: {username, password},
|
23 | timeout: 2000
|
24 | }
|
25 |
|
26 | request(opts, (err, res, body) => {
|
27 | if (err) return cb(err)
|
28 | if (res.statusCode !== 200) return cb(new Error(body && body.message ? body.message : 'Login failed'))
|
29 |
|
30 | this.userSettings.setToken(body.accessToken)
|
31 | cb()
|
32 | })
|
33 | }
|
34 |
|
35 | |
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | getInfos (infoType, appId, deviceId, cb) {
|
42 | const opts = {
|
43 | method: 'GET',
|
44 | url: `${this.dcAddress}/applications/${appId}/${infoType}/${deviceId}`,
|
45 | json: true,
|
46 | timeout: 2000,
|
47 | headers: {authorization: 'Bearer ' + this.userSettings.getToken()}
|
48 | }
|
49 |
|
50 | request(opts, (err, res, body) => {
|
51 | if (err) return cb(err)
|
52 | if (res.statusCode !== 200) return cb(new Error(body && body.message ? body.message : `could not get ${infoType}`))
|
53 |
|
54 | if (res.headers['x-jwt']) this.userSettings.setToken(res.headers['x-jwt'])
|
55 | cb(null, body)
|
56 | })
|
57 | }
|
58 |
|
59 | |
60 |
|
61 |
|
62 |
|
63 | downloadPipelines (applicationId, trusted) {
|
64 | return new Promise((resolve, reject) => {
|
65 | const pipelineRoute = trusted ? 'trustedPipelines' : 'pipelines'
|
66 |
|
67 | const opts = {
|
68 | method: 'GET',
|
69 | url: `${this.dcAddress}/applications/${applicationId}/${pipelineRoute}`,
|
70 | timeout: 15000,
|
71 | json: true,
|
72 | headers: {authorization: 'Bearer ' + this.userSettings.getToken()}
|
73 | }
|
74 |
|
75 | request(opts, (err, res, body) => {
|
76 | if (err) return reject(err)
|
77 | if (res.statusCode === 404) return reject(new Error(`The application with id '${applicationId}' was not found`))
|
78 | if (res.statusCode !== 200) return reject(new Error(body && body.message ? body.message : 'Pipeline update failed'))
|
79 |
|
80 | if (res.headers['x-jwt']) this.userSettings.setToken(res.headers['x-jwt'])
|
81 | resolve(body.pipelines)
|
82 | })
|
83 | })
|
84 | }
|
85 |
|
86 | |
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 | uploadPipeline (pipeline, applicationId, trusted) {
|
93 | return new Promise((resolve, reject) => {
|
94 | const pipelineRoute = trusted ? 'trustedPipelines' : 'pipelines'
|
95 |
|
96 | const opts = {
|
97 | method: 'PUT',
|
98 | url: `${this.dcAddress}/applications/${applicationId}/${pipelineRoute}/${pipeline.pipeline.id}`,
|
99 | json: true,
|
100 | body: pipeline,
|
101 | timeout: 15000,
|
102 | headers: {authorization: 'Bearer ' + this.userSettings.getToken()}
|
103 | }
|
104 |
|
105 | request(opts, (err, res, body) => {
|
106 | if (body) this.logger.debug(body)
|
107 | if (err) return reject(err)
|
108 | if (res.statusCode !== 204) {
|
109 | const err = new Error(body && body.message ? body.message : 'Pipeline update failed')
|
110 | if (body && body.code) err.code = body.code
|
111 | return reject(err)
|
112 | }
|
113 |
|
114 | if (res.headers['x-jwt']) this.userSettings.setToken(res.headers['x-jwt'])
|
115 | resolve()
|
116 | })
|
117 | })
|
118 | }
|
119 |
|
120 | |
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 | uploadMultiplePipelines (pipelines, applicationId, trusted) {
|
127 | if (pipelines.length === 0) return Promise.resolve()
|
128 | const promises = []
|
129 | pipelines.forEach((pipeline) => {
|
130 | promises.push(this.uploadPipeline(pipeline, applicationId, trusted))
|
131 | })
|
132 | return Promise.all(promises)
|
133 | }
|
134 |
|
135 | |
136 |
|
137 |
|
138 |
|
139 |
|
140 | removePipeline (pipelineId, applicationId, trusted) {
|
141 | return new Promise((resolve, reject) => {
|
142 | const pipelineRoute = trusted ? 'trustedPipelines' : 'pipelines'
|
143 |
|
144 | const opts = {
|
145 | method: 'DELETE',
|
146 | url: `${this.dcAddress}/applications/${applicationId}/${pipelineRoute}/${pipelineId}`,
|
147 | json: true,
|
148 | timeout: 15000,
|
149 | headers: {authorization: 'Bearer ' + this.userSettings.getToken()}
|
150 | }
|
151 |
|
152 | request(opts, (err, res, body) => {
|
153 | if (body) this.logger.debug(body)
|
154 | if (err) return reject(err)
|
155 | if (res.statusCode !== 204) return reject(new Error(body && body.message ? body.message : 'Pipeline removal failed'))
|
156 |
|
157 | if (res.headers['x-jwt']) this.userSettings.setToken(res.headers['x-jwt'])
|
158 | resolve()
|
159 | })
|
160 | })
|
161 | }
|
162 |
|
163 | |
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 | generateExtensionConfig (config, applicationId, cb) {
|
170 | return new Promise((resolve, reject) => {
|
171 | const opts = {
|
172 | method: 'POST',
|
173 | url: `${this.dcAddress}/applications/${applicationId}/extensions/${encodeURIComponent(config.id)}/generateConfig`,
|
174 | json: true,
|
175 | body: config,
|
176 | timeout: 15000,
|
177 | headers: {authorization: 'Bearer ' + this.userSettings.getToken()}
|
178 | }
|
179 |
|
180 | request(opts, (err, res, body) => {
|
181 | if (err) return reject(err)
|
182 | if (res.statusCode === 401) return reject(new UnauthorizedError(body.message))
|
183 | if (res.statusCode === 404) return reject(new NotFoundError(body.message))
|
184 | if (res.statusCode !== 200) return reject(new Error(body && body.message ? body.message : 'Could not generate Extension-Config'))
|
185 |
|
186 | if (res.headers['x-jwt']) this.userSettings.setToken(res.headers['x-jwt'])
|
187 | resolve(body)
|
188 | })
|
189 | })
|
190 | }
|
191 |
|
192 | setStartPageUrl (applicationId, startPageUrl, cb) {
|
193 | const opts = {
|
194 | method: 'PUT',
|
195 | url: `${this.dcAddress}/applications/${applicationId}/settings/startpage`,
|
196 | json: true,
|
197 | body: {startPageUrl},
|
198 | timeout: 5000,
|
199 | headers: {authorization: 'Bearer ' + this.userSettings.getToken()}
|
200 | }
|
201 | request(opts, (err, res, body) => {
|
202 | if (body) this.logger.debug(body)
|
203 | if (err) return cb(err)
|
204 | if (res.statusCode !== 204) return cb(new Error(body && body.message ? body.message : 'Setting start page url failed'))
|
205 |
|
206 | if (res.headers['x-jwt']) this.userSettings.setToken(res.headers['x-jwt'])
|
207 | cb()
|
208 | })
|
209 | }
|
210 |
|
211 | getApplicationData (applicationId, cb) {
|
212 | const opts = {
|
213 | method: 'GET',
|
214 | url: `${this.dcAddress}/applications/${applicationId}`,
|
215 | json: true,
|
216 | timeout: 5000,
|
217 | headers: {
|
218 | authorization: 'Bearer ' + this.userSettings.getToken()
|
219 | }
|
220 | }
|
221 | request(opts, (err, res, body) => {
|
222 | if (err) return cb(err)
|
223 | if (res.statusCode === 401) return cb(new UnauthorizedError(body.message))
|
224 | if (res.statusCode !== 200) return cb(new Error(body && body.message ? body.message : 'Getting application data failed'))
|
225 | cb(null, body)
|
226 | })
|
227 | }
|
228 | }
|
229 |
|
230 | module.exports = DcHttpClient
|