WebApiManager.js 3.5 KB

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