method: method
headers: 'Content-Type': content_type
opts.headers['Authorization'] = token if token?
- # no Accept header: -> unsupported media type
- opts.headers['Accept'] = '*/*;q=0.8' # -> bad request (in json)
- #opts.headers['Accept'] = 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' # -> bad request (in json) or blank 404
- #opts.headers['Accept'] = 'application/json' -> unsupported media type
+ opts.headers['Accept'] = '*/*;q=0.8'
async.waterfall [
(callback) ->
if content_type is 'multipart/form-data'
req.end()
], callback
+# send json
+# try parsing result as json, and fall back to returning string
json_request = (method, path, data, token, callback) ->
if data?
data = JSON.stringify data
request method, path, 'application/json', data, token, (err, response) ->
return callback err if err
- return callback() if response is ' '
- return callback() if response is ''
+ return callback null, '' if response is ' '
+ return callback null, '' if response is ''
try
callback null, JSON.parse(response)
catch error
- callback "Error: AF server returned invalid JSON for #{method} to #{path}: \"#{response}\""
+ # some api calls return plain text
+ callback null, response
json_get = (path, token, callback) ->
json_request 'GET', path, null, token, callback
else
callback "login: couldn't find the token in the server response: #{JSON.stringify response}"
-# this is the friendly one that updates the files then restarts the app
-exports.app_publish = (token, app_name, zip_file, callback) ->
- exports.app_update_files token, app_name, zip_file, (err) ->
- return callback err if err?
- # FIXME add "restart" call that only calls app_info once
- exports.app_stop token, app_name, (err) ->
- if err?
- return callback "Couldn't stop the app (to restart it). App is probably still running old version (though perhaps accessing new files.). Server responded: #{err}"
- exports.app_start token, app_name, (err) ->
- if err?
- return callback "CRITICAL ERROR: Couldn't start the app back up! Server responded: #{err}"
- callback()
-
exports.app_update_files = (token, app_name, zip_file, callback) ->
request(
'POST',
exports.app_set_info = (token, app_name, info, callback) ->
json_put "/apps/#{app_name}", info, token, callback
-app_set_state = (token, app_name, state, callback) ->
- exports.app_info token, app_name, (err, info) ->
- return callback err if err?
- info.state = state
- exports.app_set_info token, app_name, info, callback
-
-exports.app_start = (token, app_name, callback) ->
- app_set_state token, app_name, 'STARTED', callback
-
-exports.app_stop = (token, app_name, callback) ->
- app_set_state token, app_name, 'STOPPED', callback
-
-
app_get = (path) ->
return (token, app_name, callback) ->
json_get "/apps/#{app_name}#{path}", token, callback