Skip to content

Commit

Permalink
BC-8941 Replace request-promise (part 2) (#5520)
Browse files Browse the repository at this point in the history
  • Loading branch information
dyedwiper authored Feb 25, 2025
1 parent d94381b commit ae89cb3
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 441 deletions.
331 changes: 2 additions & 329 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,6 @@
"prom-client": "^15.1.3",
"qs": "^6.14.0",
"reflect-metadata": "^0.2.2",
"request-promise-core": "^1.1.4",
"request-promise-native": "^1.0.9",
"response-time": "^2.3.3",
"rimraf": "^6.0.1",
"rss-parser": "^3.13.0",
Expand Down
8 changes: 5 additions & 3 deletions src/services/edusharing/services/MerlinTokenGenerator.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { Configuration } = require('@hpi-schul-cloud/commons');
const request = require('request-promise-native');
const axios = require('axios');
const { getCounty } = require('../helpers');

class MerlinTokenGenerator {
Expand All @@ -8,7 +8,9 @@ class MerlinTokenGenerator {
}

async post(options) {
return request.post(options);
const res = await axios(options);

return res.data;
}

async getMerlinCredentials(county = null) {
Expand Down Expand Up @@ -58,7 +60,7 @@ class MerlinTokenGenerator {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
form: {
data: {
username: credentials.username,
password: credentials.password,
},
Expand Down
92 changes: 22 additions & 70 deletions src/services/etherpad/utils/EtherpadClient.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const rp = require('request-promise-native');
const rpErrors = require('request-promise-core/errors');
const axios = require('axios');

const { Configuration } = require('@hpi-schul-cloud/commons');
const { BadRequest, Conflict } = require('../../../errors');
Expand Down Expand Up @@ -40,93 +39,58 @@ class EtherpadClient {
};
}

createSettings(
{
method = 'POST',
endpoint,
formDef = {
apikey: Configuration.get('ETHERPAD__API_KEY'),
},
body,
},
params = {}
) {
const form = { ...formDef, ...params };
createOptions(endpoint, params = {}) {
const apikey = Configuration.get('ETHERPAD__API_KEY');
const data = { apikey, ...params };
return {
method,
uri: `${this.uri()}/${endpoint}`,
form,
body,
json: false,
method: 'POST',
url: `${this.uri()}/${endpoint}`,
data,
};
}

handleEtherpadResponse(res) {
const responseJSON = JSON.parse(res);
const responseJSON = res.data;
switch (responseJSON.code) {
case 0:
return responseJSON;
case 1:
throw new Conflict(responseJSON.message, rpErrors.RequestError(responseJSON.message, res));
throw new Conflict(res);
default:
throw new BadRequest(responseJSON.message, rpErrors.RequestError(responseJSON.message, res));
throw new BadRequest(res);
}
}

createOrGetAuthor(params) {
return rp(
this.createSettings(
{
endpoint: 'createAuthorIfNotExistsFor',
},
params
)
)
const options = this.createOptions('createAuthorIfNotExistsFor', params);
return axios(options)
.then((res) => this.handleEtherpadResponse(res))
.catch((err) => {
throw new BadRequest(this.err.createOrGetAuthor, err);
});
}

createOrGetGroup(params) {
return rp(
this.createSettings(
{
endpoint: 'createGroupIfNotExistsFor',
},
params
)
)
const options = this.createOptions('createGroupIfNotExistsFor', params);
return axios(options)
.then((res) => this.handleEtherpadResponse(res))
.catch((err) => {
throw new BadRequest(this.err.createOrGetGroup, err);
});
}

getActiveSessions(params) {
return rp(
this.createSettings(
{
endpoint: 'listSessionsOfAuthor',
},
params
)
)
const options = this.createOptions('listSessionsOfAuthor', params);
return axios(options)
.then((res) => this.handleEtherpadResponse(res))
.catch((err) => {
throw new BadRequest(this.err.getActiveSessions, err);
});
}

createSession(params) {
return rp(
this.createSettings(
{
endpoint: 'createSession',
},
params
)
)
const options = this.createOptions('createSession', params);
return axios(options)
.then((res) => this.handleEtherpadResponse(res))
.catch((err) => {
throw new BadRequest(this.err.createSession, err);
Expand All @@ -140,14 +104,8 @@ class EtherpadClient {
sourceID: params.oldPadId,
destinationID: newPadId,
};
return rp(
this.createSettings(
{
endpoint: 'copyPad',
},
copyParams
)
)
const options = this.createOptions('copyPad', copyParams);
return axios(options)
.then((res) => {
const response = this.handleEtherpadResponse(res);
response.data = {
Expand All @@ -159,14 +117,8 @@ class EtherpadClient {
throw new BadRequest(this.err.copyOldPadToGroupPad, err);
});
}
return rp(
this.createSettings(
{
endpoint: 'createGroupPad',
},
params
)
)
const options = this.createOptions('createGroupPad', params);
return axios(options)
.then((res) => this.handleEtherpadResponse(res))
.catch((err) => {
// pad is already there, just return the constructed pad path
Expand Down
23 changes: 10 additions & 13 deletions src/services/fileStorage/proxy-service.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const fs = require('fs');
const url = require('url');
const rp = require('request-promise-native');
const axios = require('axios');
const { Configuration } = require('@hpi-schul-cloud/commons');
const { filesRepo } = require('../../components/fileStorage/repo');

Expand Down Expand Up @@ -29,10 +29,7 @@ const { sortRoles } = require('../role/utils/rolesHelper');
const { userModel } = require('../user/model');
const logger = require('../../logger');
const { equal: equalIds } = require('../../helper/compare').ObjectId;
const {
FILE_SECURITY_CHECK_MAX_FILE_SIZE,
SECURITY_CHECK_SERVICE_PATH,
} = require('../../../config/globals');
const { FILE_SECURITY_CHECK_MAX_FILE_SIZE, SECURITY_CHECK_SERVICE_PATH } = require('../../../config/globals');
const AWSS3Strategy = require('./strategies/awsS3');

const sanitizeObj = (obj) => {
Expand Down Expand Up @@ -110,20 +107,20 @@ const prepareSecurityCheck = async (file, userId, strategy) => {
.then((signedUrl) => {
const params = {
url: Configuration.get('FILE_SECURITY_CHECK_SERVICE_URI'),
method: 'POST',
auth: {
user: Configuration.get('FILE_SECURITY_SERVICE_USERNAME'),
pass: Configuration.get('FILE_SECURITY_SERVICE_PASSWORD'),
username: Configuration.get('FILE_SECURITY_SERVICE_USERNAME'),
password: Configuration.get('FILE_SECURITY_SERVICE_PASSWORD'),
},
body: {
data: {
download_uri: signedUrl,
callback_uri: url.resolve(
Configuration.get('API_HOST'),
`${SECURITY_CHECK_SERVICE_PATH}${file.securityCheck.requestToken}`
),
},
json: true,
};
const send = rp.post(params);
const send = axios(params);
return send;
});
}
Expand Down Expand Up @@ -775,10 +772,10 @@ const newFileService = {
if (Configuration.get('REQUEST_OPTION__KEEP_ALIVE')) {
headers.Connection = 'Keep-Alive';
}
return rp({
return axios({
method: 'PUT',
uri: signedUrl.url,
body: buffer,
url: signedUrl.url,
data: buffer,
headers,
});
})
Expand Down
13 changes: 7 additions & 6 deletions src/services/oauth2/hydra.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
const request = require('request-promise-native');
const axios = require('axios');

const mockTlsTermination = {
'X-Forwarded-Proto': 'https',
};

module.exports = (hydraUrl) => {
return {
introspectOAuth2Token: (token, scope) => {
introspectOAuth2Token: async (token, scope) => {
const options = {
uri: `${hydraUrl}/oauth2/introspect`,
url: `${hydraUrl}/oauth2/introspect`,
method: 'POST',
body: `token=${token}&scope=${scope}`,
data: `token=${token}&scope=${scope}`,
headers: {
...mockTlsTermination,
'Content-Type': 'application/x-www-form-urlencoded',
},
json: true,
};
return request(options);
const res = await axios(options);

return res.data;
},
};
};
1 change: 0 additions & 1 deletion src/services/rocketChat/services/rocketChatChannel.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const request = require('request-promise-native');
const { BadRequest } = require('../../../errors');
const { makeStringRCConform } = require('../helpers');
const { TEAM_FEATURES } = require('../../teams/model');
Expand Down
15 changes: 8 additions & 7 deletions src/services/sync/strategies/TSP/TSP.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const rp = require('request-promise-native');
const axios = require('axios');
const url = require('url');
const moment = require('moment');
const { JWE, JWK, JWS } = require('jose');
Expand Down Expand Up @@ -219,15 +219,16 @@ class TspApi {
*/
async request(path, lastChange = new Date(0)) {
const lastChangeDate = moment(lastChange).format('YYYY-MM-DD HH:mm:ss.SSS');

const requestUrl = url.resolve(BASE_URL, path);
const response = await rp(requestUrl, {
const options = {
url: url.resolve(BASE_URL, path),
headers: this.getHeaders(),
qs: {
params: {
dtLetzteAenderung: lastChangeDate,
},
});
return JSON.parse(response);
};
const response = await axios(options);

return response.data;
}
}

Expand Down
21 changes: 11 additions & 10 deletions src/services/wopi/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Provides a basic wopi - endpoint, https://wopirest.readthedocs.io/en/latest/index.html
*/
const rp = require('request-promise-native');
const axios = require('axios');
const { static: staticContent } = require('@feathersjs/express');
const path = require('path');

Expand Down Expand Up @@ -140,12 +140,14 @@ class WopiFilesContentsService {
})
.then((signedUrl) => {
const opt = {
uri: signedUrl.url,
encoding: null,
url: signedUrl.url,
responseType: 'arraybuffer',
};
return rp(opt).catch((err) => {
logger.warning(new Error(err));
});
return axios(opt)
.then((res) => res.data)
.catch((err) => {
logger.warning(new Error(err));
});
})
.catch((err) => {
logger.warning(new Error(err));
Expand Down Expand Up @@ -188,12 +190,11 @@ class WopiFilesContentsService {
// put binary content directly to file in storage
const options = {
method: 'PUT',
uri: signedUrl.url,
contentType: file.type,
body: data,
url: signedUrl.url,
data,
};

return rp(options)
return axios(options)
.then(() =>
FileModel.findOneAndUpdate(
{ _id: file._id },
Expand Down

0 comments on commit ae89cb3

Please sign in to comment.