my_api_client 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MyApiClient
4
+ module Params
5
+ # Description of Params
6
+ class Request
7
+ attr_reader :method, :pathname, :headers, :query, :body
8
+
9
+ def initialize(method, pathname, headers, query, body)
10
+ @method = method
11
+ @pathname = pathname
12
+ @headers = headers
13
+ @query = query
14
+ @body = body
15
+ end
16
+
17
+ def to_sawyer_args
18
+ [method, pathname, body, { headers: headers, query: query }]
19
+ end
20
+
21
+ # Returns contents as string for to be readable for human
22
+ #
23
+ # @return [String] Contents as string
24
+ def inspect
25
+ { method: method, pathname: pathname, headers: headers, query: query, body: body }.inspect
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MyApiClient
4
+ # Description of Request
5
+ module Request
6
+ # Description of #_request
7
+ #
8
+ # @param http_method [Symbol] describe_http_method_here
9
+ # @param pathname [String] describe_pathname_here
10
+ # @param headers [Hash, nil] describe_headers_here
11
+ # @param query [Hash, nil] describe_query_here
12
+ # @param body [Hash, nil] describe_body_here
13
+ # @param logger [::Logger] describe_logger_here
14
+ # @return [Sawyer::Resource] description_of_returned_object
15
+ # rubocop:disable Metrics/ParameterLists
16
+ def _request(http_method, pathname, headers, query, body, logger)
17
+ request_params = Params::Request.new(http_method, pathname, headers, query, body)
18
+ request_logger = Logger.new(logger, faraday, http_method, pathname)
19
+ call(:_execute, request_params, request_logger)
20
+ end
21
+ # rubocop:enable Metrics/ParameterLists
22
+
23
+ private
24
+
25
+ # Description of #agent
26
+ #
27
+ # @return [Sawyer::Agent] description_of_returned_object
28
+ def agent
29
+ @agent ||= Sawyer::Agent.new(endpoint, faraday: faraday)
30
+ end
31
+
32
+ # Description of #faraday
33
+ #
34
+ # @return [Faraday::Connection] description_of_returned_object
35
+ def faraday
36
+ @faraday ||=
37
+ Faraday.new(
38
+ nil,
39
+ request: {
40
+ timeout: (http_read_timeout if respond_to?(:http_read_timeout)),
41
+ open_timeout: (http_open_timeout if respond_to?(:http_open_timeout)),
42
+ }.compact
43
+ )
44
+ end
45
+
46
+ # Description of #_execute
47
+ #
48
+ # @param request_params [MyApiClient::Params::Request] describe_request_params_here
49
+ # @param request_logger [MyApiClient::Logger] describe_request_logger_here
50
+ # @return [Sawyer::Resource] description_of_returned_object
51
+ # @raise [MyApiClient::Error]
52
+ def _execute(request_params, request_logger)
53
+ request_logger.info('Start')
54
+ response = agent.call(*request_params.to_sawyer_args)
55
+ request_logger.info("Duration #{response.timing} sec")
56
+ params = Params::Params.new(request_params, response)
57
+ _verify(params, request_logger)
58
+ rescue *NETWORK_ERRORS => e
59
+ params ||= Params::Params.new(request_params, nil)
60
+ request_logger.error("Network Error (#{e.message})")
61
+ raise MyApiClient::NetworkError.new(params, e)
62
+ rescue MyApiClient::Error => e
63
+ request_logger.warn("Failure (#{response.status})")
64
+ raise e
65
+ else
66
+ request_logger.info("Success (#{response.status})")
67
+ response.data
68
+ end
69
+
70
+ # Description of #_verify
71
+ #
72
+ # @param params [MyApiClient::Params::Params] describe_params_here
73
+ # @param request_logger [MyApiClient::Logger] describe_request_logger_here
74
+ # @return [nil] description_of_returned_object
75
+ # @raise [MyApiClient::Error]
76
+ def _verify(params, request_logger)
77
+ case error_handler = error_handling(params.response)
78
+ when Proc
79
+ error_handler.call(params, request_logger)
80
+ when Symbol
81
+ send(error_handler, params, request_logger)
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MyApiClient
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'my_api_client/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'my_api_client'
9
+ spec.version = MyApiClient::VERSION
10
+ spec.authors = ['ryz310']
11
+ spec.email = ['ryz310@gmail.com']
12
+
13
+ spec.summary = 'Assists to create your API client'
14
+ spec.description = 'Provides error handling, retry and other functions ' \
15
+ 'to your API Client'
16
+ spec.homepage = 'https://github.com/ryz310/my_api_client'
17
+ spec.license = 'MIT'
18
+
19
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^spec/}) }
21
+ end
22
+ spec.bindir = 'exe'
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ['lib']
25
+
26
+ spec.add_dependency 'activesupport', '>= 4.2.0'
27
+ spec.add_dependency 'jsonpath'
28
+ spec.add_dependency 'sawyer'
29
+
30
+ spec.add_development_dependency 'bundler', '~> 1.16'
31
+ spec.add_development_dependency 'pr_comet'
32
+ spec.add_development_dependency 'pry-byebug'
33
+ spec.add_development_dependency 'rake', '~> 10.0'
34
+ spec.add_development_dependency 'rspec'
35
+ spec.add_development_dependency 'rspec_junit_formatter'
36
+ spec.add_development_dependency 'rubocop'
37
+ spec.add_development_dependency 'rubocop-performance'
38
+ spec.add_development_dependency 'rubocop-rspec'
39
+ spec.add_development_dependency 'simplecov'
40
+ spec.add_development_dependency 'webmock'
41
+ spec.add_development_dependency 'yard'
42
+ end
metadata ADDED
@@ -0,0 +1,288 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: my_api_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - ryz310
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-05-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 4.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 4.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: jsonpath
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sawyer
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.16'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.16'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pr_comet
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: pry-byebug
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '10.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '10.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rspec_junit_formatter
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: rubocop
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: rubocop-performance
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: rubocop-rspec
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ - !ruby/object:Gem::Dependency
182
+ name: simplecov
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - ">="
186
+ - !ruby/object:Gem::Version
187
+ version: '0'
188
+ type: :development
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - ">="
193
+ - !ruby/object:Gem::Version
194
+ version: '0'
195
+ - !ruby/object:Gem::Dependency
196
+ name: webmock
197
+ requirement: !ruby/object:Gem::Requirement
198
+ requirements:
199
+ - - ">="
200
+ - !ruby/object:Gem::Version
201
+ version: '0'
202
+ type: :development
203
+ prerelease: false
204
+ version_requirements: !ruby/object:Gem::Requirement
205
+ requirements:
206
+ - - ">="
207
+ - !ruby/object:Gem::Version
208
+ version: '0'
209
+ - !ruby/object:Gem::Dependency
210
+ name: yard
211
+ requirement: !ruby/object:Gem::Requirement
212
+ requirements:
213
+ - - ">="
214
+ - !ruby/object:Gem::Version
215
+ version: '0'
216
+ type: :development
217
+ prerelease: false
218
+ version_requirements: !ruby/object:Gem::Requirement
219
+ requirements:
220
+ - - ">="
221
+ - !ruby/object:Gem::Version
222
+ version: '0'
223
+ description: Provides error handling, retry and other functions to your API Client
224
+ email:
225
+ - ryz310@gmail.com
226
+ executables: []
227
+ extensions: []
228
+ extra_rdoc_files: []
229
+ files:
230
+ - ".circleci/config.yml"
231
+ - ".envrc.skeleton"
232
+ - ".gitignore"
233
+ - ".rspec"
234
+ - ".rubocop.yml"
235
+ - ".rubocop_todo.yml"
236
+ - CODE_OF_CONDUCT.md
237
+ - Gemfile
238
+ - Gemfile.lock
239
+ - LICENSE.txt
240
+ - README.jp.md
241
+ - README.md
242
+ - Rakefile
243
+ - bin/console
244
+ - bin/release
245
+ - bin/setup
246
+ - lib/generators/rails/USAGE
247
+ - lib/generators/rails/api_client_generator.rb
248
+ - lib/generators/rails/templates/api_client.rb.erb
249
+ - lib/generators/rails/templates/application_api_client.rb.erb
250
+ - lib/generators/rspec/USAGE
251
+ - lib/generators/rspec/api_client_generator.rb
252
+ - lib/generators/rspec/templates/api_client_spec.rb.erb
253
+ - lib/my_api_client.rb
254
+ - lib/my_api_client/base.rb
255
+ - lib/my_api_client/config.rb
256
+ - lib/my_api_client/error_handling.rb
257
+ - lib/my_api_client/errors.rb
258
+ - lib/my_api_client/exceptions.rb
259
+ - lib/my_api_client/logger.rb
260
+ - lib/my_api_client/params/params.rb
261
+ - lib/my_api_client/params/request.rb
262
+ - lib/my_api_client/request.rb
263
+ - lib/my_api_client/version.rb
264
+ - my_api_client.gemspec
265
+ homepage: https://github.com/ryz310/my_api_client
266
+ licenses:
267
+ - MIT
268
+ metadata: {}
269
+ post_install_message:
270
+ rdoc_options: []
271
+ require_paths:
272
+ - lib
273
+ required_ruby_version: !ruby/object:Gem::Requirement
274
+ requirements:
275
+ - - ">="
276
+ - !ruby/object:Gem::Version
277
+ version: '0'
278
+ required_rubygems_version: !ruby/object:Gem::Requirement
279
+ requirements:
280
+ - - ">="
281
+ - !ruby/object:Gem::Version
282
+ version: '0'
283
+ requirements: []
284
+ rubygems_version: 3.0.3
285
+ signing_key:
286
+ specification_version: 4
287
+ summary: Assists to create your API client
288
+ test_files: []