chimera_http_client 0.2.1 → 0.3.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
  SHA256:
3
- metadata.gz: 257cb743fce518d09b68b8ee5315a3e6fe47939b88fdbbb6c4b2cdf715058b13
4
- data.tar.gz: fdd6dd4ad951507c0add3deed785ea5abef4cff538ad66be11eb2c347304c2fa
3
+ metadata.gz: 809d90aabfcf20ff5823e4adfba61a39c377e334ad42821c6f8f89a09decf4c8
4
+ data.tar.gz: 1c809df7392bd1a353b34641ae810c2f884fc72873ad917792b37455f77303bc
5
5
  SHA512:
6
- metadata.gz: 8dca3db73c0e1bc12998bfc9a88534b9115be48ee6402dfa515feababb40289b75d4bbdd3ddb2f4f57c0149a965e6afa912eb2daa3f5701eb4fe4cf1f92b5b88
7
- data.tar.gz: 9d5c8136c35b2459d66da1894d67f9e5cb61622ff2236f2e37de9a1e84b7d833cf0463dd3f93657300eff870dc3088e4e31f83faddf1de9b46e7031c42ff19f2
6
+ metadata.gz: 7e4cc3b3ed6e228f8fd1a2efb31bbcc0223e53f1f66839b669dccf6dd0f44b35f1a8e6431ed9a3f831e12f088b012d6f7abd275e820c2ef4096f4d6c7ea74c8b
7
+ data.tar.gz: 4fffd616c32fb55a098c5044cc7941517eb5c847420ee6d84010750dbf0f56a579a903f0f4ba55afe2f1c57dd8a9f0fd8d4d7706678da2935944981d52536287
data/README.markdown CHANGED
@@ -36,6 +36,16 @@ On each request _the http-headers can be amended or overwritten_ completely or p
36
36
  In case you need to use an API that is protected by **basic_auth** just pass the credentials in the options hash:
37
37
  `options: { username: 'admin', password: 'secret' }`
38
38
 
39
+ ### Timeout duration
40
+
41
+ The default timeout duration is **3 seconds**.
42
+
43
+ If you want to use a different timeout, you can pass the key `timeout` when initializing the `Connection`. You can also overwrite it on every call.
44
+
45
+ ### Custom logger
46
+
47
+ By default no logging is happening. If you need request logging, you can pass your custom Logger to the key `logger` when initializing the `Connection`. It will write to `logger.info` when starting and when completing a request.
48
+
39
49
  ### Example usage
40
50
 
41
51
  To use the gem, it is recommended to write wrapper classes for the endpoints used. While it would be possible to use the `get, get!, post, post!, put, put!, patch, patch!, delete, delete!` or also the bare `request.run` methods directly, wrapper classes will unify the usage pattern and be very convenient to use by veterans and newcomers to the team. A wrapper class could look like this:
@@ -63,7 +73,7 @@ class Users
63
73
  params[:filter] = filter
64
74
  params[:page] = page
65
75
 
66
- response = connection.get!('users', params: params)
76
+ response = connection.get!('users', params: params, timeout: 10) # set longer timeout
67
77
 
68
78
  all_users = response.parsed_body
69
79
  all_users.map { |user| User.new(id: user['id'], name: user['name'], email: user['email']) }
@@ -73,7 +83,7 @@ class Users
73
83
  end
74
84
 
75
85
  def create(body:)
76
- response = connection.post!('users', body: body.to_json) # body.to_json
86
+ response = connection.post!('users', body: body.to_json) # body.to_json (!!)
77
87
 
78
88
  user = response.parsed_body
79
89
  User.new(id: user['id'], name: user['name'], email: user['email'])
@@ -85,7 +95,9 @@ class Users
85
95
  private
86
96
 
87
97
  def connection
88
- @connection ||= ChimeraHttpClient::Connection.new(base_url: @base_url)
98
+ # base_url is mandatory
99
+ # logger and timeout are optional
100
+ @connection ||= ChimeraHttpClient::Connection.new(base_url: @base_url, logger: Logger.new(STDOUT), timeout: 2)
89
101
  end
90
102
  end
91
103
  ```
@@ -106,6 +118,8 @@ To create and fetch a user from a remote service with the `Users` wrapper listed
106
118
 
107
119
  Usually it does not have to be used directly. It is the class that executes the `Typhoeus::Requests`, raises `Errors` on failing and returns `Response` objects on successful calls.
108
120
 
121
+ The `body` which it receives from the `Connection` class has to be in the in the (serialized) form in which the endpoint expects it. Usually this means you have to pass a JSON string to the `body` (it will **not** be serialized automatically).
122
+
109
123
  It will be expanded by a `.queue` method, that will queue (sic) calls and run them in parallel and not run every call directly, like the `.run` method does.
110
124
 
111
125
  ## The Response class
@@ -169,10 +183,18 @@ After checking out the repo, run `rake` to run the **tests and rubocop**.
169
183
 
170
184
  You can also run `rake console` to open an irb session with the `ChimeraHttpClient` pre-loaded that will allow you to experiment.
171
185
 
172
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
186
+ To build and install this gem onto your local machine, run `bundle exec rake install`.
187
+
188
+ > Maintainers only:
189
+ >
190
+ > To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
173
191
 
174
192
  Bug reports and pull requests are welcome on GitHub at <https://github.com/mediafinger/chimera_http_client>
175
193
 
194
+ ### Roadmap
195
+
196
+ https://github.com/mediafinger/chimera_http_client/blob/master/TODO.markdown
197
+
176
198
  ## Chimera
177
199
 
178
200
  Why this name? First of all, I needed a unique namespace. _HttpClient_ is already used too often. And as this gem is based on **Typhoeus** I picked the name of one of his (mythological) children.
data/TODO.markdown CHANGED
@@ -2,8 +2,7 @@
2
2
 
3
3
  ## Bugs
4
4
 
5
- * The connection_specs are testing for a Hash in the body instead for serialized JSON content
6
- * The implementation seems to be buggy, not handling JSON as intended
5
+ _none known_
7
6
 
8
7
  ## Features
9
8
 
@@ -16,12 +15,12 @@
16
15
 
17
16
  ### Logger
18
17
 
19
- * allow to pass a logger
20
- * add logger.info when starting a http call
21
- * add logger.info when finishing a successful http call
22
- * add logger.warn / .error for error cases
23
- * include the total_time of the requests in the log
24
- * add example to README
18
+ * ~allow to pass a logger~
19
+ * ~add logger.info when starting a http call~
20
+ * ~add logger.info when finishing a successful http call~
21
+ * ~include the total_time of the requests in the log~
22
+ * ~add (example) to README~
23
+ * add logger.warn / .error for error cases (?)
25
24
 
26
25
  ### Custom Serializer
27
26
 
@@ -47,10 +46,17 @@
47
46
  * optional per connection or call
48
47
  * add example to README
49
48
 
49
+ ### Timeout
50
+
51
+ * ~allow to set custom timeout per connection~
52
+ * ~allow to set custom timeout per call~
53
+ * ~add (example) to README~
54
+
50
55
  ### Release
51
56
 
52
- * make repo public
57
+ * ~rename module to have unique namespace~
58
+ * ~release to rubygems to add to the plethora of similar gems~
59
+ * ~make repo public~
53
60
  * hook up Travis-CI
54
- * ensure it runs with Ruby 2.5 and newer
61
+ * ensure it runs with Ruby 2.4 and newer
55
62
  * get feedback
56
- * release to rubygems to add to the plethora of similar gems
@@ -5,9 +5,12 @@ module ChimeraHttpClient
5
5
  class Connection
6
6
  USER_AGENT = "ChimeraHttpClient (by mediafinger)".freeze
7
7
 
8
- def initialize(base_url:, user_agent: USER_AGENT, verbose: false)
8
+ def initialize(base_url:, logger: nil, timeout: nil, user_agent: USER_AGENT, verbose: false)
9
9
  fail(ChimeraHttpClient::ParameterMissingError, "base_url expected, but not given") if base_url.nil?
10
+
10
11
  @base_url = base_url
12
+ @logger = logger
13
+ @timeout = timeout
11
14
 
12
15
  define_bang_methods
13
16
 
@@ -18,13 +21,13 @@ module ChimeraHttpClient
18
21
  end
19
22
 
20
23
  def request
21
- @request ||= Request.new
24
+ @request ||= Request.new(logger: @logger)
22
25
  end
23
26
 
24
27
  def get(endpoint, options = {})
25
28
  headers = extract_headers(options, default_headers)
26
29
 
27
- request.run(url: url(endpoint), method: :get, options: options, headers: headers)
30
+ request.run(url: url(endpoint), method: :get, options: augmented_options(options), headers: headers)
28
31
  end
29
32
 
30
33
  def post(endpoint, options = {})
@@ -45,18 +48,27 @@ module ChimeraHttpClient
45
48
 
46
49
  private
47
50
 
51
+ # Add default values to call options
52
+ def augmented_options(options)
53
+ options[:timeout] ||= @timeout
54
+
55
+ options
56
+ end
57
+
48
58
  def run_with_body(method, endpoint, options = {})
49
59
  body = extract_body(options)
50
60
  headers = extract_headers(options, default_headers)
51
61
 
52
- request.run(url: url(endpoint), method: method, body: body, options: options, headers: headers)
62
+ request.run(url: url(endpoint), method: method, body: body, options: augmented_options(options), headers: headers)
53
63
  end
54
64
 
65
+ # Build URL out of @base_url and endpoint given as String or Array, while trimming redundant "/"
55
66
  def url(endpoint)
56
67
  trimmed_endpoint = Array(endpoint).map { |e| trim(e) }
57
68
  [@base_url.chomp("/"), trimmed_endpoint].flatten.reject(&:empty?).join("/")
58
69
  end
59
70
 
71
+ # Remove leading and trailing "/" from a give part of a String (usually URL or endpoint)
60
72
  def trim(element)
61
73
  element.to_s.sub(%r{^\/}, "").chomp("/")
62
74
  end
@@ -1,14 +1,18 @@
1
1
  module ChimeraHttpClient
2
2
  class Request
3
- TIMEOUT_SECONDS = 5
3
+ TIMEOUT_SECONDS = 3
4
+
5
+ def initialize(logger: nil)
6
+ @logger = logger
7
+ end
4
8
 
5
9
  def run(url:, method:, body: nil, options: {}, headers: {})
6
10
  request_params = {
7
11
  method: method,
8
12
  body: body,
9
- params: options.fetch(:params, {}),
13
+ params: options[:params] || {},
10
14
  headers: headers,
11
- timeout: options.fetch(:timeout, TIMEOUT_SECONDS),
15
+ timeout: options[:timeout] || TIMEOUT_SECONDS,
12
16
  accept_encoding: "gzip",
13
17
  }
14
18
 
@@ -21,9 +25,13 @@ module ChimeraHttpClient
21
25
 
22
26
  result = nil
23
27
  request.on_complete do |response|
28
+ @logger&.info("Completed HTTP request: #{method.upcase} #{url} " \
29
+ "in #{response.total_time&.round(3)}sec with status code #{response.code}")
30
+
24
31
  result = on_complete_handler(response)
25
32
  end
26
33
 
34
+ @logger&.info("Starting HTTP request: #{method.upcase} #{url}")
27
35
  request.run
28
36
 
29
37
  result
@@ -1,3 +1,3 @@
1
1
  module ChimeraHttpClient
2
- VERSION = "0.2.1".freeze
2
+ VERSION = "0.3.0".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chimera_http_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andreas Finger
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-04-17 00:00:00.000000000 Z
11
+ date: 2019-04-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: typhoeus