-
Notifications
You must be signed in to change notification settings - Fork 5
/
http_client.rb
295 lines (256 loc) · 7.67 KB
/
http_client.rb
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
require 'net/http'
class Vayacondios
# A concrete implementation of a Vayacondios client which
# communicates with the Vayacondios server over HTTP.
#
# Conforms to the public API of the Vayacondios::Client class.
#
# @see Vayacondios::Client
#
# @todo Decide whether to continue to use the current
# no-dependencies-its-just-net-http approach or pick an HTTP
# client library that is non-blocking, persistent, performant,
# robust, &c.
class HttpClient < Client
# The default host for the Vayacondios server.
HOST = 'localhost'
# The default port for the Vayacondios server.
PORT = 9000
# The default headers to include with each request.
HEADERS = {'Content-Type' => 'application/json'}
# The default timeout for HTTP requests.
TIMEOUT = 30
# A module that is dynamically extended into each returned
# response.
module HttpResponse
# The HTTP response code of the raw response.
attr_accessor :response_code
# The HTTP body of the raw response.
attr_accessor :body
# Used to set an error during communication with the server, not
# an error returned by the server.
attr_accessor :http_error
# Was this response a success?
#
# Corresponds to HTTP response code 200.
def success?
response_code == 200
end
# Does this response indicate an object that wasn't found?
#
# Corresponds to HTTP response code 404.
def not_found?
response_code == 404
end
# Is this response an error?
#
# @return [true, false]
def error?
http_error || (response_code >= 400)
end
# Is this response an error that is **not** due to an object
# that wasn't found?
#
# @return [true, false]
def bad?
not_found? ? false : error?
end
# The error message contained in this response.
#
# @return [String, nil]
def error_message
return unless error?
self['error'] rescue nil
end
end
attr_accessor :host
attr_accessor :port
attr_accessor :headers
attr_accessor :timeout
# Create a new Vayacondios::HttpClient.
#
# @param [Hash] options
# @option options [String] :host ('localhost') the host of the Vayacondios server
# @option options [Integer] :port (9000) the port of the Vayacondios server
# @option options [Hash] :headers default headers to include with every request
def initialize options={}
super(options)
self.host = (options[:host] || HOST).to_s
self.port = (options[:port] || PORT).to_i
self.headers = (options[:headers] || HEADERS)
self.timeout = (options[:timeout] || TIMEOUT).to_i
end
# The connection maintained to the Vayacondios server.
#
# @return [Net::HTTP]
def connection
@connection ||= Net::HTTP.new(host, port)
end
# Perform an HTTP request.
#
# Each element of `args` is turned into a segment in the path.
# The last argument, if a Hash, is treated as the body of the
# request.
#
# This method is useful for getting at various features of the
# Vayacondios API that aren't directly exposed by the client.
#
# @example Retrieve an event using its ID
#
# client.request(:get, 'event', 'transactions', '2387238')
#
# @param [String] method the HTTP method to use
# @return [Hash,Array,String,Numeric,nil] the parsed JSON object returned by the server
def request(method, *args)
send_request(create_request(method, *args))
end
protected
# Perform the actual announcement..
#
# @param [String] topic
# @param [Hash] event
# @param [String] id
# @return [Hash]
#
# @see Client#announce
def perform_announce topic, event, id=nil
request(:post, 'event', topic, id, body: event)
end
# Perform the actual search for events.
#
# @param [String] topic
# @param [Hash] query
# @return [Array<Hash>]
#
# @see Client#events
def perform_events topic, query={}
request(:get, 'events', topic, body: query)
end
# Perform the actual get request.
#
# @param [String] topic
# @param [String] id
# @return [Object]
#
# @see Client#get
def perform_get topic, id=nil
request(:get, 'stash', topic, id)
end
# Perform the actual search for stashes.
#
# @param [Hash] query
# @return [Array<Hash>]
#
# @see Client#stashes
def perform_stashes query={}
request(:get, 'stashes', body: query)
end
# Perform the actual set request.
#
# @param [String] topic
# @param [String] id
# @param [Object] document
# @return [Object]
#
# @see Client#set
def perform_set topic, id, document
request(:put, 'stash', topic, id, body: document)
end
# Perform the actual set_many request.
#
# @param [Hash] query
# @param [Hash] update
#
# @see Client#set_many
def perform_set_many query, update
request(:put, 'stashes', body: {query: query, update: update})
end
# Perform the actual set! request.
#
# @param [String] topic
# @param [String] id
# @param [Object] document
# @return [Object]
#
# @see Client#set!
def perform_set! topic, id, document
request(:post, 'stash', topic, id, body: document)
end
# Perform the actual set_many! request.
#
# @param [Hash] query
# @param [Hash] update
#
# @see Client#set_many!
def perform_set_many! query, replacement
request(:post, 'stashes', body: {query: query, update: replacement})
end
# Perform the delete request.
#
# @param [String] topic
# @param [String] id
# @return [Hash]
#
# @see Client#delete
def perform_delete topic, id=nil
request(:delete, 'stash', topic, id)
end
# Perform the actual delete_many request.
#
# @param [Hash] query
#
# @see Client#delete_many
def perform_delete_many query
request(:delete, 'stashes', body: query)
end
private
# :nodoc:
def create_request method, *args
document = args.pop[:body] if args.last.is_a?(Hash)
path = File.join("/#{Client::VERSION}", organization, *args.compact.map(&:to_s))
msgs = [method.to_s.upcase, "http://#{host}:#{port}#{path}"]
Net::HTTP.const_get(method.to_s.capitalize).new(path, headers).tap do |req|
if document
output = MultiJson.dump(document)
req.body = output
msgs << output
end
log.debug(msgs.join(' '))
end
end
# :nodoc:
def send_request req
begin
Timeout.timeout(self.timeout) do
handle_response(connection.request(req))
end
rescue Timeout::Error => e
handle_error("Timed out connecting to http://#{host}:#{port}")
rescue Errno::ECONNREFUSED => e
handle_error("Could not connect to http://#{host}:#{port}")
end
end
# :nodoc:
def handle_response raw_response
response = MultiJson.load(raw_response.body)
response.extend(HttpResponse)
response.response_code = raw_response.code.to_i
response.body = raw_response.body
case
when response.not_found?
log.debug(response.error_message)
when response.error?
log.error(response.error_message)
end
response
end
# :nodoc:
def handle_error message
response = { 'error' => message }
response.extend(HttpResponse)
response.http_error = true
log.error(response.error_message)
response
end
end
end