/
client.coffee
118 lines (90 loc) · 3.22 KB
/
client.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
require('pkginfo')(module)
version = @version
_ = require('underscore')
fs = require('fs')
request = require('request')
class Client
MAX_RETRIES = 5
constructor: (company, product, options = {}, default_options = {}, extra_options_list = []) ->
core_default_options =
user_agent: @version()
@options_list = ['scheme', 'host', 'port', 'user_agent'].concat(extra_options_list)
@options = {}
@load_from_hash('params', options)
@load_from_config(company, product, options.config_file)
@load_from_env(company.toUpperCase() + '_' + product.toUpperCase())
@load_from_env(company.toUpperCase())
@load_from_config(company, product, "./.#{company}.json")
@load_from_config(company, product, "./#{company}.json")
@load_from_config(company, product, "~/.#{company}.json")
@load_from_hash('defaults', default_options)
@load_from_hash('defaults', core_default_options)
version: ->
"iron_core_node-#{version}"
set_option: (source, name, value) ->
if (not @options[name]?) and value?
console.log("Setting #{name} to '#{value}' from #{source}")
@options[name] = value
load_from_hash: (source, hash) ->
if hash?
@set_option(source, option, hash[option]) for option in @options_list
load_from_env: (prefix) ->
@set_option('environment_variable', option, process.env[prefix + '_' + option.toUpperCase()]) for option in @options_list
load_from_config: (company, product, config_file) ->
if config_file?
try
real_config_file = config_file.replace(/^~/, process.env.HOME)
config = JSON.parse(fs.readFileSync(real_config_file))
@load_from_hash(config_file, config["#{company}_#{product}"])
@load_from_hash(config_file, config[company])
@load_from_hash(config_file, config)
headers: ->
{'User-Agent': @options.user_agent}
url: ->
"#{@options.scheme}://#{@options.host}:#{@options.port}/"
request: (request_info, cb, retry = 0) ->
request_bind = _.bind(@request, @)
request(request_info, (error, response, body) ->
if response.statusCode == 200
cb(error, response, body)
else
if response.statusCode == 503 and retry < @MAX_RETRIES
delay = Math.pow(4, retry) * 100 * Math.random()
_.delay(request_bind, delay, request_info, cb, retry + 1)
else
cb(error, response, body)
)
get: (method, params, cb) ->
request_info =
method: 'GET'
uri: @url() + method
headers: @headers()
qs: params
@request(request_info, cb)
post: (method, params, cb) ->
request_info =
method: 'POST'
uri: @url() + method
headers: @headers()
json: params
@request(request_info, cb)
put: (method, params, cb) ->
request_info =
method: 'PUT'
uri: @url() + method
headers: @headers()
json: params
@request(request_info, cb)
delete: (method, params, cb) ->
request_info =
method: 'DELETE'
uri: @url() + method
headers: @headers()
qs: params
@request(request_info, cb)
parse_response: (error, response, body, cb) ->
if response.statusCode == 200
cb(null, body)
else
cb(new Error(body), null)
module.exports.Client = Client