X-Git-Url: https://jasonwoof.com/gitweb/?p=af-coffee.git;a=blobdiff_plain;f=api.coffee;h=156e91c1c313ca28691c5ece8dfd9f110cac82ba;hp=3108f4f61919067093e1b81fd7f6f848d63fc259;hb=36afa8a844f8dc9fc50bf6ee4da3fc55e900d9b1;hpb=6651ab6419d8b498cc143cb7e82d8d99807ae83b diff --git a/api.coffee b/api.coffee index 3108f4f..156e91c 100644 --- a/api.coffee +++ b/api.coffee @@ -135,17 +135,20 @@ request = (method, path, content_type, data, token, callback) -> 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