restful_client 0.3.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: ffdd1539730f64064b5e045bf4afcad0374ec2dc
4
- data.tar.gz: 3110fcd87794defb963f4214b8164e706bf1cea5
2
+ SHA256:
3
+ metadata.gz: fb2e3bbbe3c0e81b0449b9dc0231ee925fe14c8fecae5918cebf181974addb18
4
+ data.tar.gz: 22c1095b54d3375e20fcd229e6ffcb79cebc2bfa4a4118947f114f3ef92d0e3c
5
5
  SHA512:
6
- metadata.gz: 2b432086f2e3598fae207ca1a85e56a7c3a74f69194d00b10aa9ed495737c8072452e08d9f0e2c4ef97ff0b0899216400215e350832eb09461b4999013d4510e
7
- data.tar.gz: 6aebfaf5672c02e41a1b66d7c884bd0c7c3a404a4b9a678d014ef0f0fef9ae194a46868f69c5a9d4cec2bd078c9eeeab99710dde7d04e49d5f61df336a3000e6
6
+ metadata.gz: 6ca3fbc03892cc5a7c0eb238d5a01f1f0419f3c3a025c670a18c1ca10ad455c1dbd92e32f1df56c9e254b5b894441825cccac5df3a1c7c232facad2fa5384aa9
7
+ data.tar.gz: d85e49990eeaf1973aebf1334ecd82c7556bfc78fa5102b300cb6a0d564b4dcf0a9f618210cf6e0c63c517067fc8d03bb07c80a4b08f79b8c6f9b470250283f8
data/.gitignore CHANGED
@@ -3,6 +3,7 @@
3
3
  .bundle
4
4
  .config
5
5
  .yardoc
6
+ coverage
6
7
  Gemfile.lock
7
8
  InstalledFiles
8
9
  _yardoc
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- ruby-2.2.2
1
+ ruby-2.2.3
data/.travis.yml CHANGED
@@ -2,4 +2,4 @@ sudo: false
2
2
  script: 'bundle exec rspec'
3
3
  language: ruby
4
4
  rvm:
5
- - 2.2
5
+ - 2.2.3
data/Gemfile CHANGED
@@ -1,10 +1,10 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
- gem 'thin', '~> 1.6'
4
-
5
- gem 'pry-byebug', '~> 3.3.0', {}.merge(ENV['RM_INFO'] ? { require: false } : {})
6
- gem 'rspec', '~> 3.1'
7
- gem 'rspec-its', '~> 1.1'
3
+ gem 'thin', '~> 1.7'
4
+ gem 'simplecov', '~> 0.12', require: false
5
+ gem 'pry-byebug', '~> 3.4', {}.merge(ENV['RM_INFO'] ? { require: false } : {})
6
+ gem 'rspec', '~> 3.5'
7
+ gem 'rspec-its', '~> 1.2'
8
8
  gem 'rubocop', '~> 0.35.1'
9
9
 
10
10
  gemspec
@@ -11,15 +11,12 @@ module RestfulClient
11
11
 
12
12
  class RestError < StandardError; end
13
13
 
14
- class << self
15
- attr_writer :configuration, :logger, :timeout_occured_count
16
- end
17
14
  @@configuration = nil
18
15
  @@logger = nil
19
16
  @@timeout_occured_count = 0
20
17
 
21
- SERVER_SIDE_ERRORS_RANGE = 500
22
- CLIENT_SIDE_ERRORS_RANGE = 400
18
+ SERVER_SIDE_ERRORS_RANGE = 500 unless defined?(SERVER_SIDE_ERRORS_RANGE)
19
+ CLIENT_SIDE_ERRORS_RANGE = 400 unless defined?(CLIENT_SIDE_ERRORS_RANGE)
23
20
 
24
21
  def self.configure
25
22
  @@configuration ||= RestfulClientConfiguration.new
@@ -44,7 +41,7 @@ module RestfulClient
44
41
  url = RestfulClientUri.uri_join(callerr_config(caller)['url'], path)
45
42
  headers = { 'Accept' => 'application/json' }
46
43
  headers.merge!(extra.fetch('headers', {}))
47
- request_args = { headers: headers, method: 'GET', timeout: timeout, params: params }.merge(extra.fetch('args', {}))
44
+ request_args = { headers: headers, method: 'GET', timeout: timeout_for(caller), params: params }.merge(extra.fetch('args', {}))
48
45
  request = Typhoeus::Request.new(url, request_args.merge(extra.fetch('args', {})))
49
46
  run_safe_request(caller, request, true, &on_error_block)
50
47
  end
@@ -52,21 +49,15 @@ module RestfulClient
52
49
  def post(caller, path, payload, extra = {}, &on_error_block)
53
50
  url = RestfulClientUri.uri_join(callerr_config(caller)['url'], path)
54
51
  headers, payload_as_str = prepare_payload_with_headers(payload, extra.fetch('headers', {}))
55
- request_args = { headers: headers, method: 'POST', body: payload_as_str, timeout: timeout }
52
+ request_args = { headers: headers, method: 'POST', body: payload_as_str, timeout: timeout_for(caller) }
56
53
  request = Typhoeus::Request.new(url, request_args.merge(extra.fetch('args', {})))
57
54
  run_safe_request(caller, request, false, &on_error_block)
58
55
  end
59
56
 
60
- def post_raw(caller, path, payload, custom_timeout = timeout, &on_error_block)
61
- url = RestfulClientUri.uri_join(callerr_config(caller)['url'], path)
62
- request = Typhoeus::Request.new(url, method: 'POST', body: payload, timeout: custom_timeout)
63
- run_safe_request(caller, request, false, &on_error_block)
64
- end
65
-
66
57
  def delete(caller, path, payload = {}, extra = {}, &on_error_block)
67
58
  url = RestfulClientUri.uri_join(callerr_config(caller)['url'], path)
68
59
  headers, payload_as_str = prepare_payload_with_headers(payload, extra.fetch('headers', {}))
69
- request_args = { headers: headers, method: 'DELETE', body: payload_as_str, timeout: timeout }
60
+ request_args = { headers: headers, method: 'DELETE', body: payload_as_str, timeout: timeout_for(caller) }
70
61
  request = Typhoeus::Request.new(url, request_args.merge(extra.fetch('args', {})))
71
62
  run_safe_request(caller, request, true, &on_error_block)
72
63
  end
@@ -74,7 +65,7 @@ module RestfulClient
74
65
  def put(caller, path, payload, extra = {}, &on_error_block)
75
66
  url = RestfulClientUri.uri_join(callerr_config(caller)['url'], path)
76
67
  headers, payload_as_str = prepare_payload_with_headers(payload, extra.fetch('headers', {}))
77
- request = Typhoeus::Request.new(url, headers: headers, method: 'PUT', body: payload_as_str, timeout: timeout)
68
+ request = Typhoeus::Request.new(url, headers: headers, method: 'PUT', body: payload_as_str, timeout: timeout_for(caller))
78
69
  run_safe_request(caller, request, false, &on_error_block)
79
70
  end
80
71
 
@@ -87,10 +78,10 @@ module RestfulClient
87
78
  def run_safe_request(caller, request, retry_if_needed, &on_error_block)
88
79
  @@timeout_occured_count = 0
89
80
  if !use_jynx?
90
- response = run_request(request.dup, __method__, false)
81
+ response = run_request(request.dup, __method__, false, caller)
91
82
  elsif ServiceJynx.alive?(caller)
92
83
  begin
93
- response = run_request(request.dup, __method__, retry_if_needed)
84
+ response = run_request(request.dup, __method__, retry_if_needed, caller)
94
85
  end while response.is_a?(Typhoeus::Response)
95
86
 
96
87
  response
@@ -108,7 +99,7 @@ module RestfulClient
108
99
  on_error_block.call("Exception in #{caller} execution - #{e.message}") if on_error_block
109
100
  end
110
101
 
111
- def run_request(request, method, retry_if_needed)
102
+ def run_request(request, method, retry_if_needed, service_name)
112
103
  logger.debug { "#{__method__} :: Request :: #{request.inspect}" }
113
104
  request.options[:headers].merge!('X-Forwarded-For' => $client_ip) if $client_ip
114
105
  request.options[:headers].merge!('User-Agent' => user_agent)
@@ -116,7 +107,7 @@ module RestfulClient
116
107
  # 200, OK
117
108
  if response.success?
118
109
  logger.debug { "Success in #{method} :: Code: #{response.response_code}, #{response.body}" }
119
- return '' if response.body.empty?
110
+ return '' if response.body.empty? || response.body == ' '
120
111
  begin
121
112
  return JSON.parse(response.body)
122
113
  rescue => e
@@ -126,7 +117,7 @@ module RestfulClient
126
117
  # Timeout occured
127
118
  elsif response.timed_out?
128
119
  @@timeout_occured_count += 1
129
- skip_raise = (retry_if_needed && @@timeout_occured_count <= retries)
120
+ skip_raise = (retry_if_needed && @@timeout_occured_count <= retries_for(service_name))
130
121
 
131
122
  error_type = 'TimeoutOccured'
132
123
  error_description = prettify_logger(error_type, request, response)
@@ -164,7 +155,6 @@ module RestfulClient
164
155
  logger.error { "#{error_type} #{response.code}/#{response.return_code} for: #{error_description}" }
165
156
  return ''
166
157
  else
167
-
168
158
  fail RestError.new(:BadReturnCode)
169
159
  end
170
160
  end
@@ -219,4 +209,12 @@ module RestfulClient
219
209
  "#{type} #{response.code}/#{response.return_code} for: #{request.options.fetch(:method)}, "\
220
210
  "#{request.base_url}, Total time: #{response.total_time} seconds"
221
211
  end
212
+
213
+ def timeout_for(service_name)
214
+ callerr_config(service_name)['timeout'] || timeout
215
+ end
216
+
217
+ def retries_for(service_name)
218
+ callerr_config(service_name)['retries'] || retries
219
+ end
222
220
  end
@@ -3,12 +3,12 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
3
 
4
4
  Gem::Specification.new do |spec|
5
5
  spec.name = 'restful_client'
6
- spec.version = '0.3.0'
6
+ spec.version = '1.1.0'
7
7
  spec.authors = ['Avner Cohen']
8
8
  spec.email = ['israbirding@gmail.com']
9
9
  spec.description = 'An HTTP framework for micro-services based environment, build on top of Typheous and Service Jynx'
10
10
  spec.summary = 'An HTTP framework for micro-services based environment'
11
- spec.homepage = 'https://github.com/AvnerCohen/restful_client'
11
+ spec.homepage = 'https://github.com/AvnerCohen/restful-client'
12
12
  spec.license = 'MIT'
13
13
 
14
14
  spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
@@ -16,8 +16,6 @@ Gem::Specification.new do |spec|
16
16
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
17
  spec.require_paths = ['lib']
18
18
 
19
- spec.add_development_dependency 'bundler'
20
-
21
- spec.add_runtime_dependency 'service_jynx'
22
- spec.add_runtime_dependency 'typhoeus'
19
+ spec.add_runtime_dependency 'service_jynx', '~> 0'
20
+ spec.add_runtime_dependency 'typhoeus', '~> 0'
23
21
  end
@@ -4,9 +4,9 @@ default: &default
4
4
  posts:
5
5
  url: http://1.2.3.4:8383/api/v1/
6
6
  locally:
7
- url: http://127.0.0.1:8383/api/v1/
7
+ url: http://localhost:8383/api/v1/
8
8
  pompa_service:
9
- url: http://127.0.0.1:8383/api/v1/
9
+ url: http://localhost:8383/api/v1/
10
10
 
11
11
 
12
12
  development: &development
@@ -19,4 +19,4 @@ production: &production
19
19
  posts:
20
20
  url: http://1.2.3.4:8383/api/v0/
21
21
  locally:
22
- url: http://127.0.0.1:8383/api/v0/
22
+ url: http://localhost:8383/api/v0/
@@ -201,10 +201,6 @@ describe :RestfulClient do
201
201
  RestfulClient.configure do |config|
202
202
  config.env_name = 'production'
203
203
  config.config_folder = 'spec/config'
204
- config.report_method = proc do |*args|
205
- klass = args.first
206
- set_global(klass)
207
- end
208
204
  end
209
205
  RestfulClient.get('locally', '/client_error') { nil }
210
206
 
@@ -215,10 +211,6 @@ describe :RestfulClient do
215
211
  RestfulClient.configure do |config|
216
212
  config.env_name = 'production'
217
213
  config.config_folder = 'spec/config'
218
- config.report_method = proc do |*args|
219
- klass = args.first
220
- set_global(klass)
221
- end
222
214
  end
223
215
 
224
216
  RestfulClient.get('locally', '/non_existing') { nil }
@@ -231,11 +223,8 @@ describe :RestfulClient do
231
223
  RestfulClient.configure do |config|
232
224
  config.env_name = 'production'
233
225
  config.config_folder = 'spec/config'
234
- config.report_method = proc do |*args|
235
- klass = args.first
236
- set_global(klass)
237
- end
238
226
  end
227
+
239
228
  0.upto(9) do
240
229
  RestfulClient.get('locally', '/non_existing') { 'default' }
241
230
  end
@@ -262,6 +251,25 @@ describe :RestfulClient do
262
251
 
263
252
  expect($some_global).to eq('ServiceJynx')
264
253
  end
254
+
255
+ it 'should not call server after max errors with http_code => 500' do
256
+ RestfulClient.configure do |config|
257
+ config.env_name = 'production'
258
+ config.config_folder = 'spec/config'
259
+ config.report_method = proc do |*args|
260
+ klass = args.first
261
+ set_global(klass)
262
+ end
263
+ end
264
+ 0.upto(10) do
265
+ RestfulClient.get('locally', '/server_error') { 'default' }
266
+ end
267
+
268
+ RestfulClient.get('locally', '/server_error') { |msg| $some_global = msg }
269
+
270
+ expect($some_global).to include('set as down')
271
+ end
272
+
265
273
  end
266
274
  end
267
275
 
@@ -422,4 +430,16 @@ describe :RestfulClient do
422
430
  expect(RestfulClient.srv_url('posts')).to eq('http://1.2.3.4:8383/api/v0/')
423
431
  end
424
432
  end
433
+
434
+ describe :NonJsonResponse do
435
+ it 'should fail to block when server side returns a plain string rather than json' do
436
+ RestfulClient.configure do |config|
437
+ config.env_name = 'production'
438
+ config.config_folder = 'spec/config'
439
+ end
440
+
441
+ RestfulClient.get('locally', '/non_json') { |msg| $some_global = msg }
442
+ expect($some_global).to include('unexpected token')
443
+ end
444
+ end
425
445
  end
data/spec/spec_helper.rb CHANGED
@@ -1,7 +1,8 @@
1
1
  require 'bundler'
2
2
  Bundler.require
3
3
 
4
- require 'rspec'
4
+ require 'simplecov'
5
+ SimpleCov.start
5
6
 
6
7
  Dir['./spec/support/**/*.rb'].each { |f| load(f) }
7
8
  load("#{File.dirname(__FILE__)}/../lib/restful_client.rb")
@@ -15,6 +15,8 @@ module MyApp
15
15
  [500, { 'Content-Type' => 'application/json' }, { counter: @@counter }.to_json]
16
16
  elsif path == '/api/v0/bounce'
17
17
  [200, { 'Content-Type' => 'text/plain' }, env['rack.input'].gets]
18
+ elsif path == '/api/v0/non_json'
19
+ [200, { 'Content-Type' => 'text/plain' }, 'moshe']
18
20
  elsif path == '/api/v0/non_existing'
19
21
  [404, { 'Content-Type' => 'text/plain' }, {}.to_json]
20
22
  elsif path == '/api/v0/server_error'
metadata CHANGED
@@ -1,55 +1,41 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: restful_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Avner Cohen
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-30 00:00:00.000000000 Z
11
+ date: 2026-09-14 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: bundler
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ">="
18
- - !ruby/object:Gem::Version
19
- version: '0'
20
- type: :development
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ">="
25
- - !ruby/object:Gem::Version
26
- version: '0'
27
13
  - !ruby/object:Gem::Dependency
28
14
  name: service_jynx
29
15
  requirement: !ruby/object:Gem::Requirement
30
16
  requirements:
31
- - - ">="
17
+ - - "~>"
32
18
  - !ruby/object:Gem::Version
33
19
  version: '0'
34
20
  type: :runtime
35
21
  prerelease: false
36
22
  version_requirements: !ruby/object:Gem::Requirement
37
23
  requirements:
38
- - - ">="
24
+ - - "~>"
39
25
  - !ruby/object:Gem::Version
40
26
  version: '0'
41
27
  - !ruby/object:Gem::Dependency
42
28
  name: typhoeus
43
29
  requirement: !ruby/object:Gem::Requirement
44
30
  requirements:
45
- - - ">="
31
+ - - "~>"
46
32
  - !ruby/object:Gem::Version
47
33
  version: '0'
48
34
  type: :runtime
49
35
  prerelease: false
50
36
  version_requirements: !ruby/object:Gem::Requirement
51
37
  requirements:
52
- - - ">="
38
+ - - "~>"
53
39
  - !ruby/object:Gem::Version
54
40
  version: '0'
55
41
  description: An HTTP framework for micro-services based environment, build on top
@@ -79,11 +65,11 @@ files:
79
65
  - spec/restful_client_spec.rb
80
66
  - spec/spec_helper.rb
81
67
  - spec/support/test_server.rb
82
- homepage: https://github.com/AvnerCohen/restful_client
68
+ homepage: https://github.com/AvnerCohen/restful-client
83
69
  licenses:
84
70
  - MIT
85
71
  metadata: {}
86
- post_install_message:
72
+ post_install_message:
87
73
  rdoc_options: []
88
74
  require_paths:
89
75
  - lib
@@ -98,9 +84,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
98
84
  - !ruby/object:Gem::Version
99
85
  version: '0'
100
86
  requirements: []
101
- rubyforge_project:
102
- rubygems_version: 2.4.8
103
- signing_key:
87
+ rubygems_version: 3.3.27
88
+ signing_key:
104
89
  specification_version: 4
105
90
  summary: An HTTP framework for micro-services based environment
106
91
  test_files: