api_client_base 1.10.0 → 1.11.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 +4 -4
- data/CHANGELOG.md +5 -0
- data/README.md +33 -2
- data/api_client_base.gemspec +1 -1
- data/lib/api_client_base/base.rb +4 -0
- data/lib/api_client_base/client/attributes.rb +5 -1
- data/lib/api_client_base/client/class_methods.rb +5 -1
- data/lib/api_client_base/client.rb +18 -0
- data/lib/api_client_base/response.rb +9 -0
- data/lib/api_client_base/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e748c8549dc84cfd16776fe9a92be6aafb043328916a80d88c5dbfd08e7b75d2
|
4
|
+
data.tar.gz: 145246c9dac0f31dace95aa41cf2b0183e706cfe112ba05831b7673f8ab8bd2e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 418ac87d1b08d87a1f2931a977f88c7da8f4a2ddbe49d0d7d3933be3fc73a2f598358aa8eae468023b50ff677319f044efbf303dc0d246d4d6e6131affcedbcf
|
7
|
+
data.tar.gz: 3351c32d99bed922cb89e388ad815976819a09cf46bf8e995fd9fdfb0df1600fe5c88e13500401dca83c7c7d3203dbd224ec9a9d02287b2cb950386a4aec10cc
|
data/CHANGELOG.md
CHANGED
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
|
|
4
4
|
The format is based on [Keep a Changelog](http://keepachangelog.com/)
|
5
5
|
and this project adheres to [Semantic Versioning](http://semver.org/).
|
6
6
|
|
7
|
+
## [1.11.0] - 2021-11-26
|
8
|
+
### Added
|
9
|
+
- `after_response` hook
|
10
|
+
- Add `#headers` and `#header(key)` methods to response for easy access to the headers
|
11
|
+
|
7
12
|
## [1.10.0] - 2021-06-15
|
8
13
|
### Added
|
9
14
|
- `dry-validation` 1.x support (0.x support has not been dropped)
|
data/README.md
CHANGED
@@ -42,6 +42,8 @@ MyGem.configure do |c|
|
|
42
42
|
end
|
43
43
|
```
|
44
44
|
|
45
|
+
Note: there is a default configuration setting called `after_response`. See the "Response hooks" section for more details.
|
46
|
+
|
45
47
|
- instantiate `MyGem::Client` by calling `MyGem.new(host: "https://api.com", username: "user", password: "password")`. If you do not specify an option, it will use the gem's default.
|
46
48
|
|
47
49
|
### Configuring the `Client`
|
@@ -240,7 +242,36 @@ module MyGem
|
|
240
242
|
end
|
241
243
|
```
|
242
244
|
|
243
|
-
You can an example gem
|
245
|
+
You can an example gem [here](https://github.com/bloom-solutions/binance_client-ruby).
|
246
|
+
|
247
|
+
#### Response hooks
|
248
|
+
|
249
|
+
If you want to give the applications access to the request and response objects after each response (useful when tracking rate limits that are reported in the response, for example), then:
|
250
|
+
|
251
|
+
```ruby
|
252
|
+
MyGem.configure do |c|
|
253
|
+
c.after_request = ->(request, response) do
|
254
|
+
if response.header("remaining-requets") < 20
|
255
|
+
Rails.logger.warn "mayday!"
|
256
|
+
end
|
257
|
+
end
|
258
|
+
end
|
259
|
+
```
|
260
|
+
|
261
|
+
Note: the request and response objects are the request and response instances such as:
|
262
|
+
- `GetUserResponse`
|
263
|
+
- `GetUserRequest`
|
264
|
+
|
265
|
+
You can assign any object that response to `call`:
|
266
|
+
|
267
|
+
```ruby
|
268
|
+
class AfterMyGemResponse
|
269
|
+
def self.call(request, response)
|
270
|
+
end
|
271
|
+
end
|
272
|
+
```
|
273
|
+
|
274
|
+
Note: avoid putting long running/expensive requests here because this will block the Ruby process.
|
244
275
|
|
245
276
|
## Development
|
246
277
|
|
@@ -250,7 +281,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
|
|
250
281
|
|
251
282
|
## Contributing
|
252
283
|
|
253
|
-
Bug reports and pull requests are welcome on GitHub at https://github.com/
|
284
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/bloom-solutions/api_client-ruby. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
254
285
|
|
255
286
|
## License
|
256
287
|
|
data/api_client_base.gemspec
CHANGED
@@ -11,7 +11,7 @@ Gem::Specification.new do |spec|
|
|
11
11
|
|
12
12
|
spec.summary = %q{Abstractions to help author API wrappers in Ruby.}
|
13
13
|
spec.description = %q{Abstractions to help author API wrappers in Ruby.}
|
14
|
-
spec.homepage = "https://github.com/
|
14
|
+
spec.homepage = "https://github.com/bloom-solutions/api_client-ruby"
|
15
15
|
spec.license = "MIT"
|
16
16
|
|
17
17
|
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
data/lib/api_client_base/base.rb
CHANGED
@@ -10,7 +10,7 @@ module APIClientBase
|
|
10
10
|
private
|
11
11
|
|
12
12
|
def self.inherit_attributes!(klass)
|
13
|
-
parent_module = klass
|
13
|
+
parent_module = _api_client_parent_module(klass)
|
14
14
|
return unless parent_module.respond_to?(:configuration)
|
15
15
|
parent_module.configuration.rules.each do |rule|
|
16
16
|
self.inherit_attribute!(klass, rule)
|
@@ -21,6 +21,10 @@ module APIClientBase
|
|
21
21
|
klass.attribute rule[0]
|
22
22
|
end
|
23
23
|
|
24
|
+
def self._api_client_parent_module(klass)
|
25
|
+
klass.name.deconstantize.constantize
|
26
|
+
end
|
27
|
+
|
24
28
|
end
|
25
29
|
end
|
26
30
|
end
|
@@ -28,7 +28,11 @@ module APIClientBase
|
|
28
28
|
|
29
29
|
request = request_class.new(request_args)
|
30
30
|
raw_response = request.()
|
31
|
-
response_class.new(raw_response: raw_response)
|
31
|
+
response = response_class.new(raw_response: raw_response)
|
32
|
+
|
33
|
+
_api_client_call_hook_with request, response
|
34
|
+
|
35
|
+
response
|
32
36
|
end
|
33
37
|
end
|
34
38
|
|
@@ -22,6 +22,24 @@ module APIClientBase
|
|
22
22
|
included do
|
23
23
|
include APIClientBase::Client::Attributes
|
24
24
|
extend APIClientBase::Client::ClassMethods
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def _api_client_call_hook_with(request, response)
|
29
|
+
hook = _api_client_after_response
|
30
|
+
return if hook.nil?
|
31
|
+
|
32
|
+
hook.(request, response)
|
33
|
+
end
|
34
|
+
|
35
|
+
def _api_client_gem_module
|
36
|
+
@_api_client_gem_module ||= self.class.name.deconstantize.constantize
|
37
|
+
end
|
38
|
+
|
39
|
+
def _api_client_after_response
|
40
|
+
return nil if not _api_client_gem_module.respond_to?(:configuration)
|
41
|
+
_api_client_gem_module.configuration.after_response
|
42
|
+
end
|
25
43
|
end
|
26
44
|
|
27
45
|
end
|
@@ -26,6 +26,7 @@ module APIClientBase
|
|
26
26
|
attribute :success, self::Boolean, lazy: true, default: :default_success
|
27
27
|
attribute :code, Integer, lazy: true, default: :default_code
|
28
28
|
attribute :body, String, lazy: true, default: :default_body
|
29
|
+
attribute :headers, Hash, lazy: true, default: :default_headers
|
29
30
|
end
|
30
31
|
|
31
32
|
def default_success
|
@@ -40,5 +41,13 @@ module APIClientBase
|
|
40
41
|
raw_response.body
|
41
42
|
end
|
42
43
|
|
44
|
+
def default_headers
|
45
|
+
(raw_response.headers || {}).transform_keys {|key| key.upcase }
|
46
|
+
end
|
47
|
+
|
48
|
+
def header(key)
|
49
|
+
headers[key.upcase]
|
50
|
+
end
|
51
|
+
|
43
52
|
end
|
44
53
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: api_client_base
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.11.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ramon Tayag
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-11-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -200,7 +200,7 @@ files:
|
|
200
200
|
- lib/api_client_base/services/build_typhoeus_options.rb
|
201
201
|
- lib/api_client_base/services/validate.rb
|
202
202
|
- lib/api_client_base/version.rb
|
203
|
-
homepage: https://github.com/
|
203
|
+
homepage: https://github.com/bloom-solutions/api_client-ruby
|
204
204
|
licenses:
|
205
205
|
- MIT
|
206
206
|
metadata:
|