wrappi 0.2.6 → 0.2.7
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/README.md +52 -11
- data/lib/wrappi/endpoint.rb +48 -24
- data/lib/wrappi/executer.rb +1 -1
- data/lib/wrappi/response.rb +4 -3
- data/lib/wrappi/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5ebe424766ef9fa2db743cf82b52ff134c17ad752dc431477d6265f052e9e26c
|
4
|
+
data.tar.gz: 2fd7b9bc1ed174b20c016214de557c216163834f542f13f4d90ffd53183c5734
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2cae0bd674b5596b4901a38a7e4135c51efbbb7f0489b85841243d3b1179ceec170d3141e3b44a26c73e55cd79691fa99a69771c980a0d15d641239952be41f5
|
7
|
+
data.tar.gz: c7f06f0f12e49ad69a4f9ca737b55fdcf8df3dc83dbaf3453f6db02f2fa87d9711e0da6dc3d482118c26c7b62c183cbe937b4bd7a4d9b8d9fbfe1ef74e46406c
|
data/README.md
CHANGED
@@ -6,7 +6,7 @@
|
|
6
6
|
Making APIs fun again!
|
7
7
|
|
8
8
|
Wrappi is a Framework to create API clients. The intention is to bring the best practices and standardize how API clients behave.
|
9
|
-
It allows to create API clients in a declarative way improving readability and unifying the behavior. It abstracts complex operations like caching, retries background requests and error handling.
|
9
|
+
It allows to create API clients in a declarative way improving readability and unifying the behavior. It abstracts complex operations like caching, retries, background requests and error handling.
|
10
10
|
|
11
11
|
Enjoy!
|
12
12
|
|
@@ -60,23 +60,29 @@ user.status_code # => 200
|
|
60
60
|
user.body # => {"login"=>"arturictus", "id"=>1930175, ...}
|
61
61
|
```
|
62
62
|
|
63
|
-
|
63
|
+
#### #success?
|
64
64
|
|
65
65
|
The next behaviours are using `#success?` method. You can override by redefining your own success?
|
66
66
|
|
67
67
|
The current `#success?` is defined like this:
|
68
68
|
|
69
|
-
_wrappi/
|
69
|
+
_wrappi/endpoint.rb_
|
70
70
|
```ruby
|
71
|
-
def success?
|
72
|
-
|
71
|
+
def self.success?(request)
|
72
|
+
request.code < 300 && request.code >= 200
|
73
73
|
end
|
74
74
|
```
|
75
75
|
|
76
76
|
Overrride your own in Endpoint
|
77
77
|
```ruby
|
78
|
-
|
79
|
-
|
78
|
+
class User < Wrappi::Endpoint
|
79
|
+
client Client
|
80
|
+
verb :get
|
81
|
+
path "users/:username"
|
82
|
+
|
83
|
+
def self.success?(request)
|
84
|
+
request.status == 200
|
85
|
+
end
|
80
86
|
end
|
81
87
|
```
|
82
88
|
|
@@ -174,7 +180,7 @@ Github::User.new(username: 'arturictus').async(create: true, set: { wait: 10.min
|
|
174
180
|
```
|
175
181
|
|
176
182
|
#### Cache
|
177
|
-
You can enable cache per endpoint.
|
183
|
+
You can enable cache per endpoint. It depends on `::success?` method to determine if it will be cached or nor.
|
178
184
|
|
179
185
|
Set the cache Handler in your client.
|
180
186
|
It must behave like `Rails.cache` and respond to:
|
@@ -237,8 +243,8 @@ This will retry if status code is not `200`
|
|
237
243
|
client Client
|
238
244
|
verb :get
|
239
245
|
path "users/:username"
|
240
|
-
retry_if do |response
|
241
|
-
|
246
|
+
retry_if do |response|
|
247
|
+
response.code != 200
|
242
248
|
end
|
243
249
|
end
|
244
250
|
```
|
@@ -653,7 +659,42 @@ user = GithubCLI.user(username: 'arturictus')
|
|
653
659
|
user.success?
|
654
660
|
```
|
655
661
|
|
656
|
-
|
662
|
+
#### Customization in you parent project
|
663
|
+
|
664
|
+
Once you created a gem Wrappi allows to parent projects to customize endpoints without having to change the gem's code.
|
665
|
+
|
666
|
+
example customizing `GithubCLI::User`
|
667
|
+
|
668
|
+
```ruby
|
669
|
+
GithubCLI::User.setup do
|
670
|
+
cache true
|
671
|
+
async_callback do |opts|
|
672
|
+
if success?
|
673
|
+
# do something
|
674
|
+
end
|
675
|
+
end
|
676
|
+
end
|
677
|
+
```
|
678
|
+
|
679
|
+
Example customizing all the Endpoints, adding loging to all the requests and changing client depending of enviroment:
|
680
|
+
|
681
|
+
```ruby
|
682
|
+
GithubCLI::Endpoint.setup do
|
683
|
+
client do
|
684
|
+
if ENV['production']
|
685
|
+
GithubCLI::Client
|
686
|
+
else
|
687
|
+
GithubCLI::MyStagingClient
|
688
|
+
end
|
689
|
+
end
|
690
|
+
|
691
|
+
around_request do |request, endpoint|
|
692
|
+
endpoint.logger.info("making a request to #{endpoint.url} with params: #{endpoint.consummated_params}")
|
693
|
+
request.call # IMPORTANT
|
694
|
+
endpoint.logger.info("response status is: #{request.status_code}")
|
695
|
+
end
|
696
|
+
end
|
697
|
+
```
|
657
698
|
|
658
699
|
## The HTTP clients war
|
659
700
|
|
data/lib/wrappi/endpoint.rb
CHANGED
@@ -20,12 +20,8 @@ module Wrappi
|
|
20
20
|
}
|
21
21
|
)
|
22
22
|
|
23
|
-
|
24
|
-
|
25
|
-
@input_params = input_params
|
26
|
-
@options = options
|
27
|
-
end
|
28
|
-
|
23
|
+
############## ClassMethods ################
|
24
|
+
# ============ API class metnods ================
|
29
25
|
def self.call(*args)
|
30
26
|
new(*args).call
|
31
27
|
end
|
@@ -38,6 +34,50 @@ module Wrappi
|
|
38
34
|
new(*args).body
|
39
35
|
end
|
40
36
|
|
37
|
+
def self.setup(&block)
|
38
|
+
instance_exec(&block)
|
39
|
+
end
|
40
|
+
|
41
|
+
# ============= Configs =================
|
42
|
+
def self.async_callback(&block)
|
43
|
+
@async_callback = block
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.around_request(&block)
|
47
|
+
@around_request = block
|
48
|
+
end
|
49
|
+
|
50
|
+
def self.retry_if(&block)
|
51
|
+
@retry_if = block
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.cache_options(&block)
|
55
|
+
@cache_options = block
|
56
|
+
end
|
57
|
+
|
58
|
+
# ============= Inheritance =================
|
59
|
+
def self.inherited(subclass)
|
60
|
+
super(subclass)
|
61
|
+
subclass.instance_variable_set(:@async_callback, @async_callback)
|
62
|
+
subclass.instance_variable_set(:@around_request, @around_request)
|
63
|
+
subclass.instance_variable_set(:@retry_if, @retry_if)
|
64
|
+
subclass.instance_variable_set(:@cache_options, @cache_options)
|
65
|
+
end
|
66
|
+
|
67
|
+
# ============== success behaviour ===================
|
68
|
+
# overridable
|
69
|
+
def self.success?(request)
|
70
|
+
request.code >= 200 && request.code < 300
|
71
|
+
end
|
72
|
+
#######################################################
|
73
|
+
|
74
|
+
attr_reader :input_params, :options
|
75
|
+
def initialize(input_params = {}, options = {})
|
76
|
+
@input_params = input_params
|
77
|
+
@options = options
|
78
|
+
end
|
79
|
+
|
80
|
+
|
41
81
|
def on_success(&block)
|
42
82
|
block.call(self) if success?
|
43
83
|
self
|
@@ -88,23 +128,6 @@ module Wrappi
|
|
88
128
|
end.to_s
|
89
129
|
end
|
90
130
|
|
91
|
-
|
92
|
-
def self.async_callback(&block)
|
93
|
-
@async_callback = block
|
94
|
-
end
|
95
|
-
|
96
|
-
def self.around_request(&block)
|
97
|
-
@around_request = block
|
98
|
-
end
|
99
|
-
|
100
|
-
def self.retry_if(&block)
|
101
|
-
@retry_if = block
|
102
|
-
end
|
103
|
-
|
104
|
-
def self.cache_options(&block)
|
105
|
-
@cache_options = block
|
106
|
-
end
|
107
|
-
|
108
131
|
def perform_async_callback(async_options = {})
|
109
132
|
instance_exec(async_options, &async_callback)
|
110
133
|
end
|
@@ -114,6 +137,7 @@ module Wrappi
|
|
114
137
|
@cache_key ||= "[#{verb.to_s.upcase}]##{url}#{params_cache_key}"
|
115
138
|
end
|
116
139
|
|
140
|
+
|
117
141
|
def around_request
|
118
142
|
self.class.instance_variable_get(:@around_request)
|
119
143
|
end
|
@@ -121,6 +145,7 @@ module Wrappi
|
|
121
145
|
def retry_if
|
122
146
|
self.class.instance_variable_get(:@retry_if)
|
123
147
|
end
|
148
|
+
|
124
149
|
def cache_options
|
125
150
|
self.class.instance_variable_get(:@cache_options)
|
126
151
|
end
|
@@ -131,7 +156,6 @@ module Wrappi
|
|
131
156
|
self.class.instance_variable_get(:@async_callback) || proc {}
|
132
157
|
end
|
133
158
|
|
134
|
-
|
135
159
|
def logger
|
136
160
|
client.logger
|
137
161
|
end
|
data/lib/wrappi/executer.rb
CHANGED
data/lib/wrappi/response.rb
CHANGED
@@ -4,8 +4,9 @@ module Wrappi
|
|
4
4
|
# https://github.com/httprb/http/wiki/Response-Handling
|
5
5
|
class Response
|
6
6
|
|
7
|
-
attr_reader :block
|
8
|
-
def initialize(&block)
|
7
|
+
attr_reader :block, :endpoint_klass
|
8
|
+
def initialize(endpoint_klass, &block)
|
9
|
+
@endpoint_klass = endpoint_klass
|
9
10
|
@block = block
|
10
11
|
end
|
11
12
|
|
@@ -24,7 +25,7 @@ module Wrappi
|
|
24
25
|
end
|
25
26
|
|
26
27
|
def success?
|
27
|
-
@success ||=
|
28
|
+
@success ||= endpoint_klass.success?(request)
|
28
29
|
end
|
29
30
|
|
30
31
|
def error?
|
data/lib/wrappi/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wrappi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Artur Pañach
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-09-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|