Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 20 additions & 28 deletions lib/holdcloud.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

const _ = require('lodash');
const crypto = require('crypto');
const got = require('got');
const {request} = require('./request');

class HoldCloud {
/**
* [constructor description]
Expand All @@ -21,43 +22,28 @@ class HoldCloud {
}
}

const getRp = (baseUrl, noStrictSSL, options = {}) => {
return got.extend({
prefixUrl: baseUrl,
responseType: 'json',
resolveBodyOnly: true,
https: {
rejectUnauthorized: noStrictSSL ? false : true,
},
});
}

HoldCloud.prototype.requestWithToken = async function (options) {
HoldCloud.prototype.requestWithToken = async function (options = {}) {
options = _.cloneDeep(options);
if (!this.token) {
await this.updateToken();
}
_.set(options, 'headers.jweToken', this.token);
let tokenRefreshed = false;
const self = this;
const rp = getRp(this.BASE_URI, this.noStrictSSL);
return (() => {
try {
return rp(options);
} catch (error) {
if (error.statusCode === 401 && !tokenRefreshed) {
self.updateToken();
tokenRefreshed = true;
self.requestWithToken(options);
} else {
throw error;
}
const rp = request(this.BASE_URI, this.noStrictSSL);
return await rp(options).catch((error) => {
if (error.statusCode === 401 && !tokenRefreshed) {
self.updateToken();
tokenRefreshed = true;
self.requestWithToken(options);
} else {
throw error;
}
})();
});
};

HoldCloud.prototype.updateToken = async function () {
const rp = getRp(this.BASE_URI, this.noStrictSSL);
const rp = request(this.BASE_URI, this.noStrictSSL);
const result = await rp({
method: 'POST',
url: 'login',
Expand Down Expand Up @@ -111,7 +97,13 @@ HoldCloud.prototype.createContainerApp = async function (projectId, options) {
HoldCloud.prototype.checkContainerappsName = async function (projectId, name) {
const data = await this.requestWithToken({
method: 'GET',
url: `project/${projectId}/unionservices?itemsPerPage=10&page=1&filterBy=name,^${name}$,&sortBy=a,name`,
url: `project/${projectId}/unionservices`,
searchParams: {
itemsPerPage: 10,
page: 1,
filterBy: `name,^${name}$,`,
sortBy: 'a,name,'
}
});
if (data.totalItems !== 0 && data.items[0].name === name) {
return {code: 400, id: data.items[0].id};
Expand Down
27 changes: 27 additions & 0 deletions lib/request.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const got = require('got');

exports.request = (baseUrl, noStrictSSL, options = {}) => {
return got.extend({
prefixUrl: baseUrl,
responseType: 'json',
resolveBodyOnly: true,
https: {
rejectUnauthorized: noStrictSSL ? false : true,
},
hooks: {
beforeError: [
error => {
const {response} = error;
error.statusCode = 500;
error.message = '服务器发生错误';
if (response?.body) {
error.statusCode = response.statusCode;
error.message = response.body.message;
}
return error;
},
],
},
});
};