WebApiManager.js 3.4 KB

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