Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hotfix. Keep axios instance between requests. Update work with headers #211

Closed
wants to merge 1 commit into from
Closed
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
31 changes: 15 additions & 16 deletions lib/report-portal-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ class RPClient {
this.baseURL = [this.config.endpoint, this.config.project].join('/');
this.headers = {
'User-Agent': 'NodeJS',
Authorization: `bearer ${this.apiKey}`,
'Content-Type': 'application/json; charset=UTF-8',
Authorization: `Bearer ${this.apiKey}`,
...(this.config.headers || {}),
};
this.helpers = helpers;
Expand Down Expand Up @@ -130,7 +131,7 @@ class RPClient {

checkConnect() {
const url = [this.config.endpoint.replace('/v2', '/v1'), 'user'].join('/');
return RestClient.request('GET', url, {}, { headers: this.headers });
return this.restClient.request('GET', url, {});
}

async triggerStatisticsEvent() {
Expand Down Expand Up @@ -201,7 +202,7 @@ class RPClient {
this.map[tempId] = this.getNewItemObj((resolve, reject) => {
const url = 'launch';
this.logDebug(`Start launch with tempId ${tempId}`, launchDataRQ);
this.restClient.create(url, launchData, { headers: this.headers }).then(
this.restClient.create(url, launchData).then(
(response) => {
this.map[tempId].realId = response.id;
this.launchUuid = response.id;
Expand Down Expand Up @@ -260,7 +261,7 @@ class RPClient {
() => {
this.logDebug(`Finish launch with tempId ${launchTempId}`, finishExecutionData);
const url = ['launch', launchObj.realId, 'finish'].join('/');
this.restClient.update(url, finishExecutionData, { headers: this.headers }).then(
this.restClient.update(url, finishExecutionData).then(
(response) => {
this.logDebug(`Success finish launch with tempId ${launchTempId}`, response);
console.log(`\nReportPortal Launch Link: ${response.link}`);
Expand Down Expand Up @@ -332,11 +333,13 @@ class RPClient {
'filter.in.uuid': launchUUIds,
'page.size': launchUUIds.length,
});
const launchSearchUrl = this.config.mode === 'DEBUG' ?
`launch/mode?${params.toString()}` : `launch?${params.toString()}`;
const launchSearchUrl =
this.config.mode === 'DEBUG'
? `launch/mode?${params.toString()}`
: `launch?${params.toString()}`;
this.logDebug(`Find launches with UUIDs to merge: ${launchUUIds}`);
return this.restClient
.retrieveSyncAPI(launchSearchUrl, { headers: this.headers })
.retrieveSyncAPI(launchSearchUrl)
.then(
(response) => {
const launchIds = response.content.map((launch) => launch.id);
Expand All @@ -352,7 +355,7 @@ class RPClient {
const request = this.getMergeLaunchesRequest(launchIds, mergeOptions);
this.logDebug(`Merge launches with ids: ${launchIds}`, request);
const mergeURL = 'launch/merge';
return this.restClient.create(mergeURL, request, { headers: this.headers });
return this.restClient.create(mergeURL, request);
})
.then((response) => {
this.logDebug(`Launches with UUIDs: ${launchUUIds} were successfully merged!`);
Expand Down Expand Up @@ -421,7 +424,7 @@ class RPClient {
() => {
const url = ['launch', launchObj.realId, 'update'].join('/');
this.logDebug(`Update launch with tempId ${launchTempId}`, launchData);
this.restClient.update(url, launchData, { headers: this.headers }).then(
this.restClient.update(url, launchData).then(
(response) => {
this.logDebug(`Launch with tempId ${launchTempId} were successfully updated`, response);
resolvePromise(response);
Expand Down Expand Up @@ -529,7 +532,7 @@ class RPClient {
}
testItemData.launchUuid = realLaunchId;
this.logDebug(`Start test item with tempId ${tempId}`, testItemData);
this.restClient.create(url, testItemData, { headers: this.headers }).then(
this.restClient.create(url, testItemData).then(
(response) => {
this.logDebug(`Success start item with tempId ${tempId}`, response);
this.map[tempId].realId = response.id;
Expand Down Expand Up @@ -715,8 +718,7 @@ class RPClient {
const isItemUuid = itemUuid !== launchUuid;
return this.restClient.create(
url,
Object.assign(saveLogRQ, { launchUuid }, isItemUuid && { itemUuid }),
{ headers: this.headers },
Object.assign(saveLogRQ, { launchUuid }, isItemUuid && { itemUuid })
);
};
return this.saveLog(itemObj, requestPromise);
Expand Down Expand Up @@ -772,7 +774,6 @@ class RPClient {
return this.restClient
.create(url, this.buildMultiPartStream([saveLogRQ], fileObj, MULTIPART_BOUNDARY), {
headers: {
...this.headers,
'Content-Type': `multipart/form-data; boundary=${MULTIPART_BOUNDARY}`,
},
})
Expand Down Expand Up @@ -835,9 +836,7 @@ class RPClient {
const url = ['item', itemObj.realId].join('/');
this.logDebug(`Finish test item with tempId ${itemTempId}`, itemObj);
this.restClient
.update(url, Object.assign(finishTestItemData, { launchUuid: this.launchUuid }), {
headers: this.headers,
})
.update(url, Object.assign(finishTestItemData, { launchUuid: this.launchUuid }))
.then(
(response) => {
this.logDebug(`Success finish item with tempId ${itemTempId}`, response);
Expand Down
136 changes: 75 additions & 61 deletions lib/rest.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,15 @@ class RestClient {
this.headers = options.headers;
this.restClientConfig = options.restClientConfig;

addLogger(this.restClientConfig ? this.restClientConfig.debug : false);
this.axiosInstance = axios.create({
timeout: DEFAULT_MAX_CONNECTION_TIME_MS,
headers: this.headers,
...this.getRestConfig(this.restClientConfig),
});

if (this.restClientConfig?.debug) {
addLogger(this.axiosInstance);
}
}

buildPath(path) {
Expand All @@ -28,14 +36,18 @@ class RestClient {
return [this.baseURL.replace('/v2', '/v1'), path].join('/');
}

static request(method, url, data, options = {}) {
return axios({
method,
url,
data,
timeout: DEFAULT_MAX_CONNECTION_TIME_MS,
...options,
})
request(method, url, data, options = {}) {
return this.axiosInstance
.request({
method,
url,
data,
...options,
headers: {
HOST: new URL(url).host,
...options.headers,
},
})
.then((response) => response.data)
.catch((error) => {
const errorMessage = error.message;
Expand Down Expand Up @@ -74,85 +86,87 @@ method: ${method}`,
return config;
}

create(path, data, options = { headers: this.headers }) {
return RestClient.request('POST', this.buildPath(path), data, {
create(path, data, options = {}) {
return this.request('POST', this.buildPath(path), data, {
...options,
...this.getRestConfig(),
});
}

retrieve(path, options = { headers: this.headers }) {
return RestClient.request(
retrieve(path, options = {}) {
return this.request(
'GET',
this.buildPath(path),
{},
{ ...options, ...this.getRestConfig() },
{
...options,
},
);
}

update(path, data, options = { headers: this.headers }) {
return RestClient.request('PUT', this.buildPath(path), data, {
update(path, data, options = {}) {
return this.request('PUT', this.buildPath(path), data, {
...options,
...this.getRestConfig(),
});
}

delete(path, data, options = { headers: this.headers }) {
return RestClient.request('DELETE', this.buildPath(path), data, {
delete(path, data, options = {}) {
return this.request('DELETE', this.buildPath(path), data, {
...options,
...this.getRestConfig(),
});
}

retrieveSyncAPI(path, options = { headers: this.headers }) {
return RestClient.request(
retrieveSyncAPI(path, options = {}) {
return this.request(
'GET',
this.buildPathToSyncAPI(path),
{},
{ ...options, ...this.getRestConfig() },
{
...options,
},
);
}
}

const addLogger = (debug) => {
if (debug) {
axios.interceptors.request.use((config) => {
const startDate = new Date();
config.startTime = startDate.valueOf();

console.log(`Request method=${config.method} url=${config.url} [${startDate.toISOString()}]`);

return config;
});

axios.interceptors.response.use(
(response) => {
const date = new Date();
const { status, config } = response;
const addLogger = (axiosInstance) => {
// axiosInstance.interceptors.request.clear();
axiosInstance.interceptors.request.use((config) => {
console.log('REQUEST CONFIG HEADERS', config);
const startDate = new Date();
// eslint-disable-next-line no-param-reassign
config.startTime = startDate.valueOf();

console.log(
`Response status=${status} url=${config.url} time=${
date.valueOf() - config.startTime
}ms [${date.toISOString()}]`,
);

return response;
},
(error) => {
const date = new Date();
const { response, config } = error;
const status = response ? response.status : null;

console.log(
`Response ${status ? 'status=' + status : "message='" + error.message + "'"} url=${
config.url
} time=${date.valueOf() - config.startTime}ms [${date.toISOString()}]`,
);
console.log(`Request method=${config.method} url=${config.url} [${startDate.toISOString()}]`);

return Promise.reject(error);
},
);
}
return config;
});

axiosInstance.interceptors.response.use(
(response) => {
const date = new Date();
const { status, config } = response;

console.log(
`Response status=${status} url=${config.url} time=${
date.valueOf() - config.startTime
}ms [${date.toISOString()}]`,
);

return response;
},
(error) => {
const date = new Date();
const { response, config } = error;
const status = response ? response.status : null;

console.log(
`Response ${status ? `status=${status}` : `message='${error.message}'`} url=${
config.url
} time=${date.valueOf() - config.startTime}ms [${date.toISOString()}]`,
);

return Promise.reject(error);
},
);
};

module.exports = RestClient;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"node": ">=12.x"
},
"dependencies": {
"axios": "^1.6.8",
"axios": "^1.7.7",
"axios-retry": "^4.1.0",
"glob": "^8.1.0",
"ini": "^2.0.0",
Expand Down
Loading