WebApiManager.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. */
  5. // TODO: This file was created by bulk-decaffeinate.
  6. // Fix any style issues and re-enable lint.
  7. /*
  8. * decaffeinate suggestions:
  9. * DS102: Remove unnecessary code created because of implicit returns
  10. * DS207: Consider shorter variations of null checks
  11. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  12. */
  13. let WebApiManager;
  14. const request = require("requestretry"); // allow retry on error https://github.com/FGRibreau/node-request-retry
  15. const logger = require("logger-sharelatex");
  16. const Settings = require("settings-sharelatex");
  17. // Don't let HTTP calls hang for a long time
  18. const MAX_HTTP_REQUEST_LENGTH = 15000; // 15 seconds
  19. // DEPRECATED! This method of getting user details via track-changes is deprecated
  20. // in the way we lay out our services.
  21. // Instead, web should be responsible for collecting the raw data (user_ids) and
  22. // filling it out with calls to other services. All API calls should create a
  23. // tree-like structure as much as possible, with web as the root.
  24. module.exports = (WebApiManager = {
  25. sendRequest(url, callback) {
  26. if (callback == null) { callback = function(error, body) {}; }
  27. return request.get({
  28. url: `${Settings.apis.web.url}${url}`,
  29. timeout: MAX_HTTP_REQUEST_LENGTH,
  30. maxAttempts: 2, // for node-request-retry
  31. auth: {
  32. user: Settings.apis.web.user,
  33. pass: Settings.apis.web.pass,
  34. sendImmediately: true
  35. }
  36. }, function(error, res, body){
  37. if (error != null) {
  38. return callback(error);
  39. }
  40. if (res.statusCode === 404) {
  41. logger.log({url}, "got 404 from web api");
  42. return callback(null, null);
  43. }
  44. if ((res.statusCode >= 200) && (res.statusCode < 300)) {
  45. return callback(null, body);
  46. } else {
  47. error = new Error(`web returned a non-success status code: ${res.statusCode} (attempts: ${res.attempts})`);
  48. return callback(error);
  49. }
  50. });
  51. },
  52. getUserInfo(user_id, callback) {
  53. if (callback == null) { callback = function(error, userInfo) {}; }
  54. const url = `/user/${user_id}/personal_info`;
  55. logger.log({user_id}, "getting user info from web");
  56. return WebApiManager.sendRequest(url, function(error, body) {
  57. let user;
  58. if (error != null) {
  59. logger.error({err: error, user_id, url}, "error accessing web");
  60. return callback(error);
  61. }
  62. if (body === null) {
  63. logger.error({user_id, url}, "no user found");
  64. return callback(null, null);
  65. }
  66. try {
  67. user = JSON.parse(body);
  68. } catch (error1) {
  69. error = error1;
  70. return callback(error);
  71. }
  72. return callback(null, {
  73. id: user.id,
  74. email: user.email,
  75. first_name: user.first_name,
  76. last_name: user.last_name
  77. });
  78. });
  79. },
  80. getProjectDetails(project_id, callback) {
  81. if (callback == null) { callback = function(error, details) {}; }
  82. const url = `/project/${project_id}/details`;
  83. logger.log({project_id}, "getting project details from web");
  84. return WebApiManager.sendRequest(url, function(error, body) {
  85. let project;
  86. if (error != null) {
  87. logger.error({err: error, project_id, url}, "error accessing web");
  88. return callback(error);
  89. }
  90. try {
  91. project = JSON.parse(body);
  92. } catch (error1) {
  93. error = error1;
  94. return callback(error);
  95. }
  96. return callback(null, project);
  97. });
  98. }
  99. });