2017-04-03 06:06:23 -04:00
|
|
|
request = require "requestretry" # allow retry on error https://github.com/FGRibreau/node-request-retry
|
2014-03-06 13:04:00 -05:00
|
|
|
logger = require "logger-sharelatex"
|
|
|
|
Settings = require "settings-sharelatex"
|
|
|
|
|
2016-04-13 11:30:20 -04:00
|
|
|
# Don't let HTTP calls hang for a long time
|
2017-04-12 11:47:34 -04:00
|
|
|
MAX_HTTP_REQUEST_LENGTH = 15000 # 15 seconds
|
2016-04-13 11:30:20 -04:00
|
|
|
|
2015-10-07 08:44:40 -04:00
|
|
|
# DEPRECATED! This method of getting user details via track-changes is deprecated
|
|
|
|
# in the way we lay out our services.
|
|
|
|
# Instead, web should be responsible for collecting the raw data (user_ids) and
|
|
|
|
# filling it out with calls to other services. All API calls should create a
|
|
|
|
# tree-like structure as much as possible, with web as the root.
|
2014-03-06 13:04:00 -05:00
|
|
|
module.exports = WebApiManager =
|
2014-03-28 09:05:16 -04:00
|
|
|
sendRequest: (url, callback = (error, body) ->) ->
|
2014-03-06 13:04:00 -05:00
|
|
|
request.get {
|
2014-03-28 09:05:16 -04:00
|
|
|
url: "#{Settings.apis.web.url}#{url}"
|
2016-04-13 11:30:20 -04:00
|
|
|
timeout: MAX_HTTP_REQUEST_LENGTH
|
2017-04-03 06:06:23 -04:00
|
|
|
maxAttempts: 2 # for node-request-retry
|
2014-03-06 13:04:00 -05:00
|
|
|
auth:
|
|
|
|
user: Settings.apis.web.user
|
|
|
|
pass: Settings.apis.web.pass
|
|
|
|
sendImmediately: true
|
|
|
|
}, (error, res, body)->
|
|
|
|
if error?
|
|
|
|
return callback(error)
|
2015-09-09 10:48:22 -04:00
|
|
|
if res.statusCode == 404
|
2015-09-10 09:32:35 -04:00
|
|
|
logger.log url: url, "got 404 from web api"
|
2015-09-09 10:48:22 -04:00
|
|
|
return callback null, null
|
2014-03-06 13:04:00 -05:00
|
|
|
if res.statusCode >= 200 and res.statusCode < 300
|
2014-03-28 09:05:16 -04:00
|
|
|
return callback null, body
|
2014-03-06 13:04:00 -05:00
|
|
|
else
|
2017-04-03 06:06:23 -04:00
|
|
|
error = new Error("web returned a non-success status code: #{res.statusCode} (attempts: #{res.attempts})")
|
2014-03-28 09:05:16 -04:00
|
|
|
callback error
|
|
|
|
|
|
|
|
getUserInfo: (user_id, callback = (error, userInfo) ->) ->
|
|
|
|
url = "/user/#{user_id}/personal_info"
|
|
|
|
logger.log user_id: user_id, "getting user info from web"
|
|
|
|
WebApiManager.sendRequest url, (error, body) ->
|
|
|
|
if error?
|
2014-03-06 13:04:00 -05:00
|
|
|
logger.error err: error, user_id: user_id, url: url, "error accessing web"
|
2014-03-28 09:05:16 -04:00
|
|
|
return callback error
|
|
|
|
|
2015-09-09 10:48:22 -04:00
|
|
|
if body == null
|
|
|
|
logger.error user_id: user_id, url: url, "no user found"
|
|
|
|
return callback null, null
|
2014-03-28 09:05:16 -04:00
|
|
|
try
|
|
|
|
user = JSON.parse(body)
|
|
|
|
catch error
|
|
|
|
return callback(error)
|
|
|
|
callback null, {
|
|
|
|
id: user.id
|
|
|
|
email: user.email
|
|
|
|
first_name: user.first_name
|
|
|
|
last_name: user.last_name
|
|
|
|
}
|
|
|
|
|
|
|
|
getProjectDetails: (project_id, callback = (error, details) ->) ->
|
|
|
|
url = "/project/#{project_id}/details"
|
|
|
|
logger.log project_id: project_id, "getting project details from web"
|
|
|
|
WebApiManager.sendRequest url, (error, body) ->
|
|
|
|
if error?
|
|
|
|
logger.error err: error, project_id: project_id, url: url, "error accessing web"
|
|
|
|
return callback error
|
|
|
|
|
|
|
|
try
|
|
|
|
project = JSON.parse(body)
|
|
|
|
catch error
|
|
|
|
return callback(error)
|
2015-10-07 08:44:40 -04:00
|
|
|
callback null, project
|