WebApiManager.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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('@overleaf/settings')
  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) {
  27. callback = function (error, body) {}
  28. }
  29. return request.get(
  30. {
  31. url: `${Settings.apis.web.url}${url}`,
  32. timeout: MAX_HTTP_REQUEST_LENGTH,
  33. maxAttempts: 2, // for node-request-retry
  34. auth: {
  35. user: Settings.apis.web.user,
  36. pass: Settings.apis.web.pass,
  37. sendImmediately: true,
  38. },
  39. },
  40. function (error, res, body) {
  41. if (error != null) {
  42. return callback(error)
  43. }
  44. if (res.statusCode === 404) {
  45. logger.log({ url }, 'got 404 from web api')
  46. return callback(null, null)
  47. }
  48. if (res.statusCode >= 200 && res.statusCode < 300) {
  49. return callback(null, body)
  50. } else {
  51. error = new Error(
  52. `web returned a non-success status code: ${res.statusCode} (attempts: ${res.attempts})`
  53. )
  54. return callback(error)
  55. }
  56. }
  57. )
  58. },
  59. getUserInfo(user_id, callback) {
  60. if (callback == null) {
  61. callback = function (error, userInfo) {}
  62. }
  63. const url = `/user/${user_id}/personal_info`
  64. logger.log({ user_id }, 'getting user info from web')
  65. return WebApiManager.sendRequest(url, function (error, body) {
  66. let user
  67. if (error != null) {
  68. logger.error({ err: error, user_id, url }, 'error accessing web')
  69. return callback(error)
  70. }
  71. if (body === null) {
  72. logger.error({ user_id, url }, 'no user found')
  73. return callback(null, null)
  74. }
  75. try {
  76. user = JSON.parse(body)
  77. } catch (error1) {
  78. error = error1
  79. return callback(error)
  80. }
  81. return callback(null, {
  82. id: user.id,
  83. email: user.email,
  84. first_name: user.first_name,
  85. last_name: user.last_name,
  86. })
  87. })
  88. },
  89. getProjectDetails(project_id, callback) {
  90. if (callback == null) {
  91. callback = function (error, details) {}
  92. }
  93. const url = `/project/${project_id}/details`
  94. logger.log({ project_id }, 'getting project details from web')
  95. return WebApiManager.sendRequest(url, function (error, body) {
  96. let project
  97. if (error != null) {
  98. logger.error({ err: error, project_id, url }, 'error accessing web')
  99. return callback(error)
  100. }
  101. try {
  102. project = JSON.parse(body)
  103. } catch (error1) {
  104. error = error1
  105. return callback(error)
  106. }
  107. return callback(null, project)
  108. })
  109. },
  110. }