hyperion_http 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e3bd10bd7cea98830da2b676efba1a80adba83c9
4
- data.tar.gz: 16385d656757d9c325a2a388f89c0d1e688ed920
3
+ metadata.gz: e7eaba5a3e46d889e7b8dbe19627464e09adfc22
4
+ data.tar.gz: 320017b31fd4e3e86e578605c9ac35bfacd951bb
5
5
  SHA512:
6
- metadata.gz: e4dc321c4b89ffb9029e88e7f3393cd79162cbb07f9500c969b603cb70ea7514e4947444070292b3c686c44a766aeee83b2c8c06cb4786ce0ea67ad5f55cfa3c
7
- data.tar.gz: 8766676236d1a9eb42768df79d4c45c5d9a0eada6aec79092dcc7501eac49ce4cb84e4aa7291df4762590de5871dac3f1dccc5564c650ce4dc4506e41eb02858
6
+ metadata.gz: 26b8db547297df97177e84431ea8f803f534b3183b5fabee16aa8bbd254118027cb8099048ab80b1c293ce95e6b938b01006c440c3aa5449879a65a5c26e2061
7
+ data.tar.gz: 308161f9274acdcd1432fc41eaf82012da25a63f185f44651d57868814c4aac2b85cf82d3952e92c21a2d997d07f2eb00c46a46110c00e10c491f6382aefb451
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.4.1
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = 'hyperion_http'
7
- spec.version = '0.5.0'
7
+ spec.version = '0.6.0'
8
8
  spec.authors = ['Indigo BioAutomation, Inc.']
9
9
  spec.email = ['pwinton@indigobio.com']
10
10
  spec.summary = 'Ruby REST client'
@@ -15,12 +15,13 @@ class Hyperion
15
15
  # @param body [String] the body to send with POST or PUT
16
16
  # @param additional_headers [Hash] headers to send in addition to the ones
17
17
  # already determined by the route. Example: +{'User-Agent' => 'Mozilla/5.0'}+
18
+ # @param timeout [Integer] The limit of the entire request in seconds
18
19
  # @yield [result] yields the result if a block is provided
19
20
  # @yieldparam [HyperionResult]
20
21
  # @return [HyperionResult, Object] If a block is provided, returns the block's
21
22
  # return value; otherwise, returns the result.
22
- def self.request(route, body=nil, additional_headers={}, &block)
23
- self.new(route).request(body, additional_headers, &block)
23
+ def self.request(route, body: nil, additional_headers: {}, timeout: 0, &block)
24
+ self.new(route).request(body, additional_headers, timeout, &block)
24
25
  end
25
26
 
26
27
  # @private
@@ -29,13 +30,14 @@ class Hyperion
29
30
  end
30
31
 
31
32
  # @private
32
- def request(body=nil, additional_headers={}, &dispatch)
33
+ def request(body, additional_headers, timeout, &dispatch)
33
34
  uri = transform_uri(route.uri).to_s
34
35
  with_request_logging(route, uri, route_headers(route)) do
35
36
  typho_result = Typho.request(uri,
36
37
  method: route.method,
37
38
  headers: build_headers(additional_headers),
38
- body: write(body, route.payload_descriptor))
39
+ body: write(body, route.payload_descriptor),
40
+ timeout: timeout)
39
41
  hyperion_result_for(typho_result, dispatch)
40
42
  end
41
43
  end
@@ -12,6 +12,9 @@ class Hyperion
12
12
  # HyperionResult and returns the final value to return from `request`
13
13
  # @option opts [Proc] :render A transformer, usually a proc returned by
14
14
  # `as` or `as_many`. Only called on HTTP 200.
15
+ # @option opts [Integer] :timeout The limit of the entire request in seconds.
16
+ # The default is 0 which means there will be no timeout during transfer;
17
+ # there still may be a timeout during connection.
15
18
  # @yield [rendered] Yields to allow an additional transformation.
16
19
  # Only called on HTTP 200.
17
20
  def request(route, opts={}, &project)
@@ -21,10 +24,11 @@ class Hyperion
21
24
  body = opts[:body]
22
25
  headers = opts[:headers] || {}
23
26
  additional_handler_hash = opts[:also_handle] || {}
27
+ timeout = opts[:timeout] || 0
24
28
  render = opts[:render] || Proc.identity
25
29
  project = project || Proc.identity
26
30
 
27
- Hyperion.request(route, body, headers) do |result|
31
+ Hyperion.request(route, body: body, additional_headers: headers, timeout: timeout) do |result|
28
32
  all_handlers = [hash_handler(additional_handler_hash),
29
33
  handler_from_including_class,
30
34
  built_in_handler(project, render)]
@@ -19,16 +19,24 @@ describe Hyperion::Requestor do
19
19
  include Hyperion::Requestor
20
20
 
21
21
  def arrange(method, response)
22
- @route = RestRoute.new(method, 'http://indigo.com/things',
23
- ResponseDescriptor.new('thing', 1, :json),
24
- PayloadDescriptor.new(:json))
25
-
22
+ create_route(method)
26
23
  fake_route(@route) do |request|
27
24
  @request = request
28
25
  response
29
26
  end
30
27
  end
31
28
 
29
+ def arrange_timeout(method)
30
+ create_route(method)
31
+ fake_route(@route) { sleep 2 }
32
+ end
33
+
34
+ def create_route(method)
35
+ @route = RestRoute.new(method, 'http://indigo.com/things',
36
+ ResponseDescriptor.new('thing', 1, :json),
37
+ PayloadDescriptor.new(:json))
38
+ end
39
+
32
40
  def assert_result(expected_result)
33
41
  expect(@result).to eql expected_result
34
42
  end
@@ -42,7 +50,7 @@ describe Hyperion::Requestor do
42
50
  it 'makes requests with additional headers' do
43
51
  headers = {'X-my-header' => 'value'}
44
52
  arrange(:get, {'a' => 'b'})
45
- expect(Hyperion).to receive(:request).with(@route, nil, headers)
53
+ expect(Hyperion).to receive(:request).with(@route, body: nil, additional_headers: headers, timeout: 0)
46
54
  @result = request(@route, headers: headers)
47
55
  end
48
56
 
@@ -53,6 +61,13 @@ describe Hyperion::Requestor do
53
61
  expect(@request.body).to eql({'the' => 'body'})
54
62
  end
55
63
 
64
+ it 'makes requests with a timeout' do
65
+ arrange_timeout(:get)
66
+ error = 'oops: time out'
67
+ handlers = {HyperionStatus::TIMED_OUT => proc { raise error }}
68
+ expect { request(@route, timeout: 1, also_handle: handlers) }.to raise_error error
69
+ end
70
+
56
71
  it 'renders the response with the render proc' do
57
72
  arrange(:get, {'x' => 1, 'y' => 2})
58
73
  @result = request(@route, render: OpenStruct.method(:new))
@@ -31,7 +31,7 @@ describe Hyperion do
31
31
  result = Hyperion.request(get_user_route)
32
32
  expect_success(result, {'name' => 'freddy'})
33
33
 
34
- result = Hyperion.request(post_greeting_route, write({'name' => 'freddy'}, :json))
34
+ result = Hyperion.request(post_greeting_route, body: write({'name' => 'freddy'}, :json))
35
35
  expect_success(result, {'greeting' => 'hello, freddy'})
36
36
  end
37
37
  it 'returns 404 if a requested route is not stubbed' do
@@ -96,7 +96,7 @@ describe Hyperion do
96
96
 
97
97
  result = Hyperion.request(RestRoute.new(:post, 'http://somesite.org/users/0',
98
98
  user_response_params, PayloadDescriptor.new(:json)),
99
- write({'name' => 'annie'}, :json))
99
+ body: write({'name' => 'annie'}, :json))
100
100
  expect(result.body).to eql({'updated' => {'name' => 'annie'}})
101
101
  end
102
102
 
@@ -23,10 +23,10 @@ describe Hyperion do
23
23
  )
24
24
 
25
25
  expect(Hyperion::Typho).to receive(:request).
26
- with(uri, {method: method, headers: expected_headers, body: 'Ventura'}).
26
+ with(uri, {method: method, headers: expected_headers, body: 'Ventura', timeout: 1}).
27
27
  and_return(make_typho_response(200, write({'foo' => 'bar'}, :json)))
28
28
 
29
- result = Hyperion.request(route, body, additional_headers)
29
+ result = Hyperion.request(route, body: body, additional_headers: additional_headers, timeout: 1)
30
30
  expect(result).to be_a HyperionResult
31
31
  expect(result.status).to eql HyperionStatus::SUCCESS
32
32
  expect(result.code).to eql 200
@@ -53,16 +53,16 @@ describe Hyperion do
53
53
  end
54
54
  it 'serializes the payload' do
55
55
  expect(Hyperion::Typho).to receive(:request).
56
- with(uri, {method: method, headers: expected_headers, body: '{"c":"d"}'}).
56
+ with(uri, {method: method, headers: expected_headers, body: '{"c":"d"}', timeout: 0}).
57
57
  and_return(make_typho_response(200, write({}, :json)))
58
- Hyperion.request(route, {'c' => 'd'})
58
+ Hyperion.request(route, body: {'c' => 'd'})
59
59
  end
60
60
  it 'deserializes 400-level errors to ClientErrorResponse' do
61
61
  client_error = ClientErrorResponse.new('oops', [], ClientErrorCode::MISSING)
62
62
  allow(Hyperion::Typho).to receive(:request).
63
- with(uri, {method: method, headers: expected_headers, body: '{"c":"d"}'}).
63
+ with(uri, {method: method, headers: expected_headers, body: '{"c":"d"}', timeout: 0}).
64
64
  and_return(make_typho_response(400, write(client_error.as_json, :json)))
65
- result = Hyperion.request(route, {'c' => 'd'})
65
+ result = Hyperion.request(route, body: {'c' => 'd'})
66
66
  expect(result.body).to be_a ClientErrorResponse
67
67
  expect(result.body.message).to eql 'oops'
68
68
  expect(result.body.code).to eql ClientErrorCode::MISSING
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hyperion_http
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Indigo BioAutomation, Inc.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-05-24 00:00:00.000000000 Z
11
+ date: 2017-06-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -201,6 +201,7 @@ extra_rdoc_files: []
201
201
  files:
202
202
  - ".gitignore"
203
203
  - ".rspec"
204
+ - ".ruby-version"
204
205
  - ".travis.yml"
205
206
  - CHANGES.md
206
207
  - Gemfile