hearth 1.0.0.pre1 → 1.0.0.pre2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -4
- data/VERSION +1 -1
- data/lib/hearth/api_error.rb +15 -1
- data/lib/hearth/auth_option.rb +21 -0
- data/lib/hearth/auth_schemes/anonymous.rb +21 -0
- data/lib/hearth/auth_schemes/http_api_key.rb +16 -0
- data/lib/hearth/auth_schemes/http_basic.rb +16 -0
- data/lib/hearth/auth_schemes/http_bearer.rb +16 -0
- data/lib/hearth/auth_schemes/http_digest.rb +16 -0
- data/lib/hearth/auth_schemes.rb +32 -0
- data/lib/hearth/checksums.rb +31 -0
- data/lib/hearth/client_stubs.rb +130 -0
- data/lib/hearth/config/env_provider.rb +53 -0
- data/lib/hearth/config/resolver.rb +52 -0
- data/lib/hearth/configuration.rb +15 -0
- data/lib/hearth/connection_pool.rb +77 -0
- data/lib/hearth/context.rb +28 -4
- data/lib/hearth/dns/host_address.rb +23 -0
- data/lib/hearth/dns/host_resolver.rb +92 -0
- data/lib/hearth/dns.rb +48 -0
- data/lib/hearth/http/api_error.rb +4 -8
- data/lib/hearth/http/client.rb +208 -59
- data/lib/hearth/http/error_inspector.rb +85 -0
- data/lib/hearth/http/error_parser.rb +18 -20
- data/lib/hearth/http/field.rb +64 -0
- data/lib/hearth/http/fields.rb +117 -0
- data/lib/hearth/http/middleware/content_length.rb +5 -2
- data/lib/hearth/http/middleware/content_md5.rb +31 -0
- data/lib/hearth/http/middleware/request_compression.rb +157 -0
- data/lib/hearth/http/middleware.rb +12 -0
- data/lib/hearth/http/networking_error.rb +1 -14
- data/lib/hearth/http/request.rb +83 -56
- data/lib/hearth/http/response.rb +42 -13
- data/lib/hearth/http.rb +14 -5
- data/lib/hearth/identities/anonymous.rb +8 -0
- data/lib/hearth/identities/http_api_key.rb +16 -0
- data/lib/hearth/identities/http_bearer.rb +16 -0
- data/lib/hearth/identities/http_login.rb +20 -0
- data/lib/hearth/identities.rb +21 -0
- data/lib/hearth/identity_resolver.rb +17 -0
- data/lib/hearth/interceptor.rb +506 -0
- data/lib/hearth/interceptor_context.rb +36 -0
- data/lib/hearth/interceptor_list.rb +48 -0
- data/lib/hearth/interceptors.rb +75 -0
- data/lib/hearth/middleware/auth.rb +100 -0
- data/lib/hearth/middleware/build.rb +32 -0
- data/lib/hearth/middleware/host_prefix.rb +10 -6
- data/lib/hearth/middleware/initialize.rb +58 -0
- data/lib/hearth/middleware/parse.rb +45 -6
- data/lib/hearth/middleware/retry.rb +97 -23
- data/lib/hearth/middleware/send.rb +137 -25
- data/lib/hearth/middleware/sign.rb +65 -0
- data/lib/hearth/middleware/validate.rb +11 -1
- data/lib/hearth/middleware.rb +19 -8
- data/lib/hearth/middleware_stack.rb +1 -43
- data/lib/hearth/networking_error.rb +18 -0
- data/lib/hearth/number_helper.rb +2 -2
- data/lib/hearth/output.rb +8 -4
- data/lib/hearth/plugin_list.rb +53 -0
- data/lib/hearth/query/param.rb +52 -0
- data/lib/hearth/query/param_list.rb +54 -0
- data/lib/hearth/query/param_matcher.rb +32 -0
- data/lib/hearth/refreshing_identity_resolver.rb +63 -0
- data/lib/hearth/request.rb +22 -0
- data/lib/hearth/response.rb +33 -0
- data/lib/hearth/retry/adaptive.rb +60 -0
- data/lib/hearth/retry/capacity_not_available_error.rb +9 -0
- data/lib/hearth/retry/client_rate_limiter.rb +143 -0
- data/lib/hearth/retry/exponential_backoff.rb +15 -0
- data/lib/hearth/retry/retry_quota.rb +56 -0
- data/lib/hearth/retry/standard.rb +46 -0
- data/lib/hearth/retry/strategy.rb +20 -0
- data/lib/hearth/retry.rb +16 -0
- data/lib/hearth/signers/anonymous.rb +16 -0
- data/lib/hearth/signers/http_api_key.rb +29 -0
- data/lib/hearth/signers/http_basic.rb +23 -0
- data/lib/hearth/signers/http_bearer.rb +19 -0
- data/lib/hearth/signers/http_digest.rb +19 -0
- data/lib/hearth/signers.rb +23 -0
- data/lib/hearth/stubs.rb +30 -0
- data/lib/hearth/time_helper.rb +5 -3
- data/lib/hearth/validator.rb +44 -5
- data/lib/hearth/waiters/poller.rb +6 -7
- data/lib/hearth/waiters/waiter.rb +17 -4
- data/lib/hearth/xml/formatter.rb +11 -2
- data/lib/hearth/xml/node.rb +2 -2
- data/lib/hearth.rb +32 -5
- data/sig/lib/hearth/aliases.rbs +4 -0
- data/sig/lib/hearth/api_error.rbs +13 -0
- data/sig/lib/hearth/auth_option.rbs +11 -0
- data/sig/lib/hearth/auth_schemes/anonymous.rbs +7 -0
- data/sig/lib/hearth/auth_schemes/http_api_key.rbs +7 -0
- data/sig/lib/hearth/auth_schemes/http_basic.rbs +7 -0
- data/sig/lib/hearth/auth_schemes/http_bearer.rbs +7 -0
- data/sig/lib/hearth/auth_schemes/http_digest.rbs +7 -0
- data/sig/lib/hearth/auth_schemes.rbs +13 -0
- data/sig/lib/hearth/block_io.rbs +9 -0
- data/sig/lib/hearth/client_stubs.rbs +5 -0
- data/sig/lib/hearth/configuration.rbs +7 -0
- data/sig/lib/hearth/dns/host_address.rbs +13 -0
- data/sig/lib/hearth/dns/host_resolver.rbs +19 -0
- data/sig/lib/hearth/http/api_error.rbs +13 -0
- data/sig/lib/hearth/http/client.rbs +9 -0
- data/sig/lib/hearth/http/field.rbs +19 -0
- data/sig/lib/hearth/http/fields.rbs +43 -0
- data/sig/lib/hearth/http/request.rbs +25 -0
- data/sig/lib/hearth/http/response.rbs +21 -0
- data/sig/lib/hearth/identities/anonymous.rbs +6 -0
- data/sig/lib/hearth/identities/http_api_key.rbs +9 -0
- data/sig/lib/hearth/identities/http_bearer.rbs +9 -0
- data/sig/lib/hearth/identities/http_login.rbs +11 -0
- data/sig/lib/hearth/identities.rbs +9 -0
- data/sig/lib/hearth/identity_resolver.rbs +7 -0
- data/sig/lib/hearth/interceptor.rbs +9 -0
- data/sig/lib/hearth/interceptor_context.rbs +15 -0
- data/sig/lib/hearth/interceptor_list.rbs +16 -0
- data/sig/lib/hearth/interfaces.rbs +65 -0
- data/sig/lib/hearth/output.rbs +11 -0
- data/sig/lib/hearth/plugin_list.rbs +15 -0
- data/sig/lib/hearth/query/param.rbs +17 -0
- data/sig/lib/hearth/query/param_list.rbs +25 -0
- data/sig/lib/hearth/request.rbs +9 -0
- data/sig/lib/hearth/response.rbs +11 -0
- data/sig/lib/hearth/retry/adaptive.rbs +13 -0
- data/sig/lib/hearth/retry/exponential_backoff.rbs +7 -0
- data/sig/lib/hearth/retry/standard.rbs +13 -0
- data/sig/lib/hearth/retry/strategy.rbs +11 -0
- data/sig/lib/hearth/retry.rbs +9 -0
- data/sig/lib/hearth/signers/anonymous.rbs +9 -0
- data/sig/lib/hearth/signers/http_api_key.rbs +9 -0
- data/sig/lib/hearth/signers/http_basic.rbs +9 -0
- data/sig/lib/hearth/signers/http_bearer.rbs +9 -0
- data/sig/lib/hearth/signers/http_digest.rbs +9 -0
- data/sig/lib/hearth/signers.rbs +9 -0
- data/sig/lib/hearth/structure.rbs +7 -0
- data/sig/lib/hearth/union.rbs +5 -0
- data/sig/lib/hearth/waiters/waiter.rbs +17 -0
- metadata +132 -22
- data/lib/hearth/http/headers.rb +0 -70
- data/lib/hearth/middleware/around_handler.rb +0 -24
- data/lib/hearth/middleware/request_handler.rb +0 -24
- data/lib/hearth/middleware/response_handler.rb +0 -25
- data/lib/hearth/middleware_builder.rb +0 -246
- data/lib/hearth/stubbing/client_stubs.rb +0 -115
- data/lib/hearth/stubbing/stubs.rb +0 -32
- data/lib/hearth/waiters/errors.rb +0 -15
- data/sig/lib/seahorse/api_error.rbs +0 -10
- data/sig/lib/seahorse/document.rbs +0 -2
- data/sig/lib/seahorse/http/api_error.rbs +0 -21
- data/sig/lib/seahorse/http/headers.rbs +0 -47
- data/sig/lib/seahorse/http/response.rbs +0 -21
- data/sig/lib/seahorse/simple_delegator.rbs +0 -3
- data/sig/lib/seahorse/structure.rbs +0 -18
- data/sig/lib/seahorse/stubbing/client_stubs.rbs +0 -103
- data/sig/lib/seahorse/stubbing/stubs.rbs +0 -14
- data/sig/lib/seahorse/union.rbs +0 -6
@@ -1,246 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Hearth
|
4
|
-
# A utility class for registering middleware for a request.
|
5
|
-
# You register middleware handlers to execute relative to
|
6
|
-
# Middleware classes. You can register middleware
|
7
|
-
# before/after/around any middleware class in the stack.
|
8
|
-
# You may also remove middleware from the stack.
|
9
|
-
# There are also convenience methods
|
10
|
-
# (eg: before_build, after_parse, ect)
|
11
|
-
# defined for all of the key request lifecycle events:
|
12
|
-
#
|
13
|
-
# * validate
|
14
|
-
# * host_prefix
|
15
|
-
# * build
|
16
|
-
# * send
|
17
|
-
# * parse
|
18
|
-
# * retry
|
19
|
-
#
|
20
|
-
# You can register request handlers that invoke before, after,
|
21
|
-
# or around each lifecycle events. These handlers are
|
22
|
-
# request, response, or around handlers.
|
23
|
-
#
|
24
|
-
# All before/after/around methods are defined on the class
|
25
|
-
# and instance and are chainable:
|
26
|
-
#
|
27
|
-
# MiddlewareBuilder.before_send(my_request_handler)
|
28
|
-
# .after_parse(my_response_handler)
|
29
|
-
#
|
30
|
-
# ## Request Handlers
|
31
|
-
#
|
32
|
-
# A request handler is invoked before the request is sent.
|
33
|
-
#
|
34
|
-
# # invoked after a request has been built, but before it has
|
35
|
-
# # been signed/authorized
|
36
|
-
# middleware.before_build do |input, context|
|
37
|
-
# # use input, inspect or modify the request
|
38
|
-
# # context.request
|
39
|
-
# end
|
40
|
-
#
|
41
|
-
# ## Response Handlers
|
42
|
-
#
|
43
|
-
# A response handler is invoked after the HTTP request has been sent
|
44
|
-
# and a HTTP response has been received.
|
45
|
-
#
|
46
|
-
# # invoked after the HTTP response has been parsed
|
47
|
-
# middleware.after_parse do |output, context|
|
48
|
-
# # inspect or modify the output or context
|
49
|
-
# # output.data
|
50
|
-
# # output.error
|
51
|
-
# # context.response
|
52
|
-
# end
|
53
|
-
#
|
54
|
-
# ## Around Handlers
|
55
|
-
#
|
56
|
-
# Around handlers see a request before it has been sent along
|
57
|
-
# with the response returned. Around handlers must invoke `#call`
|
58
|
-
# method of the next middleware in the stack. Around handlers
|
59
|
-
# must also return the response returned from the next middleware.
|
60
|
-
#
|
61
|
-
# # invoke before the request has been sent, receives the
|
62
|
-
# # response from the send middleware
|
63
|
-
# middleware.around_send do |app, input, context|
|
64
|
-
#
|
65
|
-
# # this code is invoked before the request is sent
|
66
|
-
# # ...
|
67
|
-
#
|
68
|
-
# # around handlers MUST call the next middleware in the stack
|
69
|
-
# output = app.call(input, context)
|
70
|
-
#
|
71
|
-
# # this code is invoked after the response has been received
|
72
|
-
# # ...
|
73
|
-
#
|
74
|
-
# # around handlers must return the response down the stack
|
75
|
-
# output
|
76
|
-
# end
|
77
|
-
#
|
78
|
-
# ## Removing Middleware
|
79
|
-
# You may remove existing middleware from the stack using either the class
|
80
|
-
# or instance `remove` methods and providing the middleware class to
|
81
|
-
# be removed. The remove methods are chainable and convenience methods are
|
82
|
-
# defined for the same set of lifecycle events as the `before`, `after`
|
83
|
-
# and `around` methods:
|
84
|
-
#
|
85
|
-
# # create a middleware builder that removes send and build middlewares
|
86
|
-
# MiddlewareBuilder
|
87
|
-
# .remove_send
|
88
|
-
# .remove_build
|
89
|
-
#
|
90
|
-
class MiddlewareBuilder
|
91
|
-
# @private
|
92
|
-
BOTH = 'expected a handler or a Proc, got both'
|
93
|
-
|
94
|
-
# @private
|
95
|
-
NEITHER = 'expected a handler or a Proc, got neither'
|
96
|
-
|
97
|
-
# @private
|
98
|
-
TOO_MANY = 'wrong number of arguments (given %<count>d, expected 0 or 1)'
|
99
|
-
|
100
|
-
# @private
|
101
|
-
CALLABLE = 'expected handler to respond to #call'
|
102
|
-
|
103
|
-
# @param [Proc, MiddlewareBuilder] middleware
|
104
|
-
#
|
105
|
-
# If `middleware` is another {MiddlewareBuilder} instance, then
|
106
|
-
# the middleware handlers are copied.
|
107
|
-
#
|
108
|
-
def initialize(middleware = nil)
|
109
|
-
@middleware = []
|
110
|
-
case middleware
|
111
|
-
when MiddlewareBuilder then @middleware.concat(middleware.to_a)
|
112
|
-
when nil then nil
|
113
|
-
else
|
114
|
-
raise ArgumentError, 'expected :middleware to be a' \
|
115
|
-
'Hearth::MiddlewareBuilder,' \
|
116
|
-
" got #{middleware.class}"
|
117
|
-
end
|
118
|
-
end
|
119
|
-
|
120
|
-
# @param [MiddlewareStack] middleware_stack
|
121
|
-
def apply(middleware_stack)
|
122
|
-
@middleware.each do |handler|
|
123
|
-
method, relation, middleware, kwargs = handler
|
124
|
-
if method == :remove
|
125
|
-
middleware_stack.remove(relation)
|
126
|
-
else
|
127
|
-
middleware_stack.send(method, relation, middleware, **kwargs)
|
128
|
-
end
|
129
|
-
end
|
130
|
-
end
|
131
|
-
|
132
|
-
def before(klass, *args, &block)
|
133
|
-
@middleware << [
|
134
|
-
:use_before,
|
135
|
-
klass,
|
136
|
-
Middleware::RequestHandler,
|
137
|
-
{ handler: handler_or_proc!(args, &block) }
|
138
|
-
]
|
139
|
-
self
|
140
|
-
end
|
141
|
-
|
142
|
-
def after(klass, *args, &block)
|
143
|
-
@middleware << [
|
144
|
-
:use_before,
|
145
|
-
klass,
|
146
|
-
Middleware::ResponseHandler,
|
147
|
-
{ handler: handler_or_proc!(args, &block) }
|
148
|
-
]
|
149
|
-
self
|
150
|
-
end
|
151
|
-
|
152
|
-
def around(klass, *args, &block)
|
153
|
-
@middleware << [
|
154
|
-
:use_before,
|
155
|
-
klass,
|
156
|
-
Middleware::AroundHandler,
|
157
|
-
{ handler: handler_or_proc!(args, &block) }
|
158
|
-
]
|
159
|
-
self
|
160
|
-
end
|
161
|
-
|
162
|
-
def remove(klass)
|
163
|
-
@middleware << [
|
164
|
-
:remove,
|
165
|
-
klass,
|
166
|
-
nil,
|
167
|
-
nil
|
168
|
-
]
|
169
|
-
self
|
170
|
-
end
|
171
|
-
|
172
|
-
# Define convenience methods for chaining
|
173
|
-
class << self
|
174
|
-
def before(klass, *args, &block)
|
175
|
-
MiddlewareBuilder.new.before(klass, *args, &block)
|
176
|
-
end
|
177
|
-
|
178
|
-
def after(klass, *args, &block)
|
179
|
-
MiddlewareBuilder.new.after(klass, *args, &block)
|
180
|
-
end
|
181
|
-
|
182
|
-
def around(klass, *args, &block)
|
183
|
-
MiddlewareBuilder.new.around(klass, *args, &block)
|
184
|
-
end
|
185
|
-
|
186
|
-
def remove(klass)
|
187
|
-
MiddlewareBuilder.new.remove(klass)
|
188
|
-
end
|
189
|
-
end
|
190
|
-
|
191
|
-
# define convenience methods for standard middleware classes
|
192
|
-
# these define methods and class methods for before,after,around
|
193
|
-
# eg: before_build, after_build, around_build.
|
194
|
-
STANDARD_MIDDLEWARE = [
|
195
|
-
Hearth::Middleware::Validate,
|
196
|
-
Hearth::Middleware::HostPrefix,
|
197
|
-
Hearth::Middleware::Build,
|
198
|
-
Hearth::Middleware::Send,
|
199
|
-
Hearth::Middleware::Retry,
|
200
|
-
Hearth::Middleware::Parse
|
201
|
-
].freeze
|
202
|
-
|
203
|
-
STANDARD_MIDDLEWARE.each do |klass|
|
204
|
-
simple_step_name = klass.to_s.split('::').last.downcase
|
205
|
-
%w[before after around].each do |method|
|
206
|
-
method_name = "#{method}_#{simple_step_name}"
|
207
|
-
define_method(method_name) do |*args, &block|
|
208
|
-
return send(method, klass, *args, &block)
|
209
|
-
end
|
210
|
-
|
211
|
-
define_singleton_method(method_name) do |*args, &block|
|
212
|
-
return send(method, klass, *args, &block)
|
213
|
-
end
|
214
|
-
end
|
215
|
-
|
216
|
-
remove_method_name = "remove_#{simple_step_name}"
|
217
|
-
define_method(remove_method_name) do
|
218
|
-
return remove(klass)
|
219
|
-
end
|
220
|
-
|
221
|
-
define_singleton_method(remove_method_name) do
|
222
|
-
return remove(klass)
|
223
|
-
end
|
224
|
-
end
|
225
|
-
|
226
|
-
def to_a
|
227
|
-
@middleware
|
228
|
-
end
|
229
|
-
|
230
|
-
private
|
231
|
-
|
232
|
-
def handler_or_proc!(args, &block)
|
233
|
-
validate_args!(args, &block)
|
234
|
-
callable = args.first || Proc.new(&block)
|
235
|
-
raise ArgumentError, CALLABLE unless callable.respond_to?(:call)
|
236
|
-
|
237
|
-
callable
|
238
|
-
end
|
239
|
-
|
240
|
-
def validate_args!(args, &block)
|
241
|
-
raise ArgumentError, BOTH if args.size.positive? && block
|
242
|
-
raise ArgumentError, NEITHER if args.empty? && block.nil?
|
243
|
-
raise ArgumentError, format(TOO_MANY, count: args.size) if args.size > 1
|
244
|
-
end
|
245
|
-
end
|
246
|
-
end
|
@@ -1,115 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require_relative 'stubs'
|
4
|
-
|
5
|
-
module Hearth
|
6
|
-
# This module provides the ability to specify the data and/or errors to
|
7
|
-
# return when a client is using stubbed responses.
|
8
|
-
# This module should be included in generated service clients.
|
9
|
-
#
|
10
|
-
# Pass `stub_responses: true` to a client constructor to enable this
|
11
|
-
# behavior.
|
12
|
-
module ClientStubs
|
13
|
-
# Configures what data / errors should be returned from the named operation
|
14
|
-
# when response stubbing is enabled.
|
15
|
-
#
|
16
|
-
# ## Basic usage
|
17
|
-
#
|
18
|
-
# When you enable response stubbing, the client will generate fake
|
19
|
-
# responses and will not make any HTTP requests.
|
20
|
-
#
|
21
|
-
# client = Service::Client.new(stub_responses: true)
|
22
|
-
# client.operation
|
23
|
-
# #=> #<struct Service:Types::Operation param1=[], param2=nil>
|
24
|
-
#
|
25
|
-
# You can specify the stub data using {#stub_responses}
|
26
|
-
#
|
27
|
-
# client = Service::Client.new(stub_responses: true)
|
28
|
-
# client.stub_responses(:operation, {
|
29
|
-
# param1: [{ name: 'value1' }]
|
30
|
-
# })
|
31
|
-
#
|
32
|
-
# client.operation.param1.map(&:name)
|
33
|
-
# #=> ['value1']
|
34
|
-
#
|
35
|
-
# ## Stubbing Errors
|
36
|
-
#
|
37
|
-
# When stubbing is enabled, the SDK will default to generate
|
38
|
-
# fake responses with placeholder values. You can override the data
|
39
|
-
# returned. You can also specify errors it should raise.
|
40
|
-
#
|
41
|
-
# # to simulate errors, give the error class, you must
|
42
|
-
# # be able to construct an instance with `.new`
|
43
|
-
# client.stub_responses(:operation, Timeout::Error)
|
44
|
-
# client.operation(param1: 'value')
|
45
|
-
# #=> raises new Timeout::Error
|
46
|
-
#
|
47
|
-
# # or you can give an instance of an error class
|
48
|
-
# client.stub_responses(:operation, RuntimeError.new('custom message'))
|
49
|
-
# client.operation(param1: 'value')
|
50
|
-
# #=> raises the given runtime error object
|
51
|
-
#
|
52
|
-
# ## Dynamic Stubbing
|
53
|
-
#
|
54
|
-
# In addition to creating static stubs, it's also possible to generate
|
55
|
-
# stubs dynamically based on the parameters with which operations were
|
56
|
-
# called, by passing a `Proc` object:
|
57
|
-
#
|
58
|
-
# client.stub_responses(:operation, -> (context) {
|
59
|
-
# if context.params[:param] == 'foo'
|
60
|
-
# # return a stub
|
61
|
-
# { param1: [{ name: 'value1'}]}
|
62
|
-
# else
|
63
|
-
# # return an error
|
64
|
-
# Services::Errors::NotFound
|
65
|
-
# end
|
66
|
-
# })
|
67
|
-
#
|
68
|
-
# ## Stubbing Raw Protocol Responses
|
69
|
-
#
|
70
|
-
# As an alternative to providing the response data, you can modify the
|
71
|
-
# response object provided by the `Proc` object and then
|
72
|
-
# return nil.
|
73
|
-
#
|
74
|
-
# client.stub_responses(:operation, -> (context) {
|
75
|
-
# context.response.status = 404 # simulate an error
|
76
|
-
# nil
|
77
|
-
# })
|
78
|
-
#
|
79
|
-
# ## Stubbing Multiple Responses
|
80
|
-
#
|
81
|
-
# Calling an operation multiple times will return similar responses.
|
82
|
-
# You can configure multiple stubs and they will be returned in sequence.
|
83
|
-
#
|
84
|
-
# client.stub_responses(:operation, [
|
85
|
-
# Errors::NotFound,
|
86
|
-
# { content_length: 150 },
|
87
|
-
# ])
|
88
|
-
#
|
89
|
-
# client.operation(param1: 'value1')
|
90
|
-
# #=> raises Errors::NotFound
|
91
|
-
#
|
92
|
-
# resp = client.operation(param1: 'value2')
|
93
|
-
# resp.content_length #=> 150
|
94
|
-
#
|
95
|
-
# @param [Symbol] operation_name
|
96
|
-
#
|
97
|
-
# @param [Mixed] stubs One or more responses to return from the named
|
98
|
-
# operation.
|
99
|
-
#
|
100
|
-
# @return [void]
|
101
|
-
#
|
102
|
-
# @raise [RuntimeError] Raises a runtime error when called
|
103
|
-
# on a client that has not enabled response stubbing via
|
104
|
-
# `:stub_responses => true`.
|
105
|
-
def stub_responses(operation_name, *stubs)
|
106
|
-
if @stub_responses
|
107
|
-
@stubs.add_stubs(operation_name, stubs.flatten)
|
108
|
-
else
|
109
|
-
msg = 'Stubbing is not enabled. Enable stubbing in the constructor '\
|
110
|
-
'with `stub_responses: true`'
|
111
|
-
raise ArgumentError, msg
|
112
|
-
end
|
113
|
-
end
|
114
|
-
end
|
115
|
-
end
|
@@ -1,32 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Hearth
|
4
|
-
# @api private
|
5
|
-
module Stubbing
|
6
|
-
# Provides a thread safe data structure for adding and getting stubs
|
7
|
-
# per operation.
|
8
|
-
class Stubs
|
9
|
-
def initialize
|
10
|
-
@stubs = {}
|
11
|
-
@stub_mutex = Mutex.new
|
12
|
-
end
|
13
|
-
|
14
|
-
def add_stubs(operation_name, stubs)
|
15
|
-
@stub_mutex.synchronize do
|
16
|
-
@stubs[operation_name.to_sym] = stubs
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
def next(operation_name)
|
21
|
-
@stub_mutex.synchronize do
|
22
|
-
stubs = @stubs[operation_name] || []
|
23
|
-
case stubs.length
|
24
|
-
when 0 then nil
|
25
|
-
when 1 then stubs.first
|
26
|
-
else stubs.shift
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
@@ -1,15 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Hearth
|
4
|
-
module Waiters
|
5
|
-
module Errors
|
6
|
-
class WaiterFailed < StandardError; end
|
7
|
-
|
8
|
-
class FailureStateError < StandardError; end
|
9
|
-
|
10
|
-
class UnexpectedError < StandardError; end
|
11
|
-
|
12
|
-
class MaxWaitTimeExceeded < StandardError; end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
@@ -1,10 +0,0 @@
|
|
1
|
-
module Hearth
|
2
|
-
# Base class for errors returned from an API. This excludes networking
|
3
|
-
# errors and errors generated on the client-side.
|
4
|
-
class ApiError < StandardError
|
5
|
-
def initialize: (error_code: String, ?message: String) -> ApiError
|
6
|
-
|
7
|
-
# @return [String]
|
8
|
-
attr_reader error_code: String
|
9
|
-
end
|
10
|
-
end
|
@@ -1,21 +0,0 @@
|
|
1
|
-
module Hearth
|
2
|
-
module HTTP
|
3
|
-
# Base class for HTTP errors returned from an API. Inherits from
|
4
|
-
# {Hearth::ApiError}.
|
5
|
-
class ApiError < Hearth::ApiError
|
6
|
-
def initialize: (http_resp: Response, **untyped kwargs) -> ApiError
|
7
|
-
|
8
|
-
# @return [Integer]
|
9
|
-
attr_reader http_status: Integer
|
10
|
-
|
11
|
-
# @return [Hash<String, String>]
|
12
|
-
attr_reader http_headers: Hash[String, String]
|
13
|
-
|
14
|
-
# @return [String]
|
15
|
-
attr_reader http_body: String
|
16
|
-
|
17
|
-
# @return [String]
|
18
|
-
attr_reader request_id: String
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
@@ -1,47 +0,0 @@
|
|
1
|
-
module Hearth
|
2
|
-
module HTTP
|
3
|
-
# Provides Hash like access for Headers with key normalization
|
4
|
-
# @api private
|
5
|
-
class Headers
|
6
|
-
# @param [Hash<String,String>] headers
|
7
|
-
def initialize: (?headers: ::Hash[String, String] headers) -> Headers
|
8
|
-
|
9
|
-
# @param [String] key
|
10
|
-
def []: (String key) -> String
|
11
|
-
|
12
|
-
# @param [String] key
|
13
|
-
# @param [String] value
|
14
|
-
def []=: (String key, String value) -> String
|
15
|
-
|
16
|
-
# @param [String] key
|
17
|
-
# @return [Boolean] Returns `true` if there is a header with
|
18
|
-
# the given key.
|
19
|
-
def key?: (String key) -> bool
|
20
|
-
|
21
|
-
# @return [Array<String>]
|
22
|
-
def keys: () -> Array[String]
|
23
|
-
|
24
|
-
# @param [String] key
|
25
|
-
# @return [String, nil] Returns the value for the deleted key.
|
26
|
-
def delete: (String key) -> (String | nil)
|
27
|
-
|
28
|
-
# @return [Enumerable<String,String>]
|
29
|
-
def each_pair: () { () -> String } -> Enumerable[Array[String]]
|
30
|
-
|
31
|
-
alias each each_pair
|
32
|
-
|
33
|
-
# @return [Hash]
|
34
|
-
def to_hash: () -> Hash[String, String]
|
35
|
-
|
36
|
-
alias to_h to_hash
|
37
|
-
|
38
|
-
# @return [Integer] Returns the number of entries in the headers
|
39
|
-
# hash.
|
40
|
-
def size: () -> Integer
|
41
|
-
|
42
|
-
private
|
43
|
-
|
44
|
-
def normalize: (String key) -> String
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
@@ -1,21 +0,0 @@
|
|
1
|
-
module Hearth
|
2
|
-
module HTTP
|
3
|
-
# Represents an HTTP Response.
|
4
|
-
# @api private
|
5
|
-
class Response
|
6
|
-
# @param [Integer] status
|
7
|
-
# @param [Headers] headers
|
8
|
-
# @param [IO] body
|
9
|
-
def initialize: (?status: ::Integer status, ?headers: Headers headers, ?body: IO body) -> Response
|
10
|
-
|
11
|
-
# @return [Integer]
|
12
|
-
attr_accessor status: Integer
|
13
|
-
|
14
|
-
# @return [Headers]
|
15
|
-
attr_accessor headers: Headers
|
16
|
-
|
17
|
-
# @return [IO]
|
18
|
-
attr_accessor body: IO
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
@@ -1,18 +0,0 @@
|
|
1
|
-
module Hearth
|
2
|
-
# A module mixed into Structs that provides utility methods.
|
3
|
-
module Structure
|
4
|
-
# Deeply converts the Struct into a hash. Structure members that
|
5
|
-
# are `nil` are omitted from the resultant hash.
|
6
|
-
#
|
7
|
-
# @return [Hash]
|
8
|
-
def to_h: (?untyped obj) -> Hash[Symbol,untyped]
|
9
|
-
|
10
|
-
alias to_hash to_h
|
11
|
-
|
12
|
-
private
|
13
|
-
|
14
|
-
def _to_h_struct: (untyped obj) -> untyped
|
15
|
-
|
16
|
-
def _to_h_hash: (untyped obj) -> untyped
|
17
|
-
end
|
18
|
-
end
|
@@ -1,103 +0,0 @@
|
|
1
|
-
module Hearth
|
2
|
-
# This module provides the ability to specify the data and/or errors to
|
3
|
-
# return when a client is using stubbed responses.
|
4
|
-
# This module should be included in generated service clients.
|
5
|
-
#
|
6
|
-
# Pass `stub_responses: true` to a client constructor to enable this
|
7
|
-
# behavior.
|
8
|
-
module ClientStubs
|
9
|
-
# Configures what data / errors should be returned from the named operation
|
10
|
-
# when response stubbing is enabled.
|
11
|
-
#
|
12
|
-
# ## Basic usage
|
13
|
-
#
|
14
|
-
# When you enable response stubbing, the client will generate fake
|
15
|
-
# responses and will not make any HTTP requests.
|
16
|
-
#
|
17
|
-
# client = Service::Client.new(stub_responses: true)
|
18
|
-
# client.operation
|
19
|
-
# #=> #<struct Service:Types::Operation param1=[], param2=nil>
|
20
|
-
#
|
21
|
-
# You can specify the stub data using {#stub_responses}
|
22
|
-
#
|
23
|
-
# client = Service::Client.new(stub_responses: true)
|
24
|
-
# client.stub_responses(:operation, {
|
25
|
-
# param1: [{ name: 'value1' }]
|
26
|
-
# })
|
27
|
-
#
|
28
|
-
# client.operation.param1.map(&:name)
|
29
|
-
# #=> ['value1']
|
30
|
-
#
|
31
|
-
# ## Stubbing Errors
|
32
|
-
#
|
33
|
-
# When stubbing is enabled, the SDK will default to generate
|
34
|
-
# fake responses with placeholder values. You can override the data
|
35
|
-
# returned. You can also specify errors it should raise.
|
36
|
-
#
|
37
|
-
# # to simulate errors, give the error class, you must
|
38
|
-
# # be able to construct an instance with `.new`
|
39
|
-
# client.stub_responses(:operation, Timeout::Error)
|
40
|
-
# client.operation(param1: 'value')
|
41
|
-
# #=> raises new Timeout::Error
|
42
|
-
#
|
43
|
-
# # or you can give an instance of an error class
|
44
|
-
# client.stub_responses(:operation, RuntimeError.new('custom message'))
|
45
|
-
# client.operation(param1: 'value')
|
46
|
-
# #=> raises the given runtime error object
|
47
|
-
#
|
48
|
-
# ## Dynamic Stubbing
|
49
|
-
#
|
50
|
-
# In addition to creating static stubs, it's also possible to generate
|
51
|
-
# stubs dynamically based on the parameters with which operations were
|
52
|
-
# called, by passing a `Proc` object:
|
53
|
-
#
|
54
|
-
# client.stub_responses(:operation, -> (context) {
|
55
|
-
# if context.params[:param] == 'foo'
|
56
|
-
# # return a stub
|
57
|
-
# { param1: [{ name: 'value1'}]}
|
58
|
-
# else
|
59
|
-
# # return an error
|
60
|
-
# Services::Errors::NotFound
|
61
|
-
# end
|
62
|
-
# })
|
63
|
-
#
|
64
|
-
# ## Stubbing Raw Protocol Responses
|
65
|
-
#
|
66
|
-
# As an alternative to providing the response data, you can modify the
|
67
|
-
# response object provided by the `Proc` object and then
|
68
|
-
# return nil.
|
69
|
-
#
|
70
|
-
# client.stub_responses(:operation, -> (context) {
|
71
|
-
# context.response.status = 404 # simulate an error
|
72
|
-
# nil
|
73
|
-
# })
|
74
|
-
#
|
75
|
-
# ## Stubbing Multiple Responses
|
76
|
-
#
|
77
|
-
# Calling an operation multiple times will return similar responses.
|
78
|
-
# You can configure multiple stubs and they will be returned in sequence.
|
79
|
-
#
|
80
|
-
# client.stub_responses(:operation, [
|
81
|
-
# Errors::NotFound,
|
82
|
-
# { content_length: 150 },
|
83
|
-
# ])
|
84
|
-
#
|
85
|
-
# client.operation(param1: 'value1')
|
86
|
-
# #=> raises Errors::NotFound
|
87
|
-
#
|
88
|
-
# resp = client.operation(param1: 'value2')
|
89
|
-
# resp.content_length #=> 150
|
90
|
-
#
|
91
|
-
# @param [Symbol] operation_name
|
92
|
-
#
|
93
|
-
# @param [Mixed] stubs One or more responses to return from the named
|
94
|
-
# operation.
|
95
|
-
#
|
96
|
-
# @return [void]
|
97
|
-
#
|
98
|
-
# @raise [RuntimeError] Raises a runtime error when called
|
99
|
-
# on a client that has not enabled response stubbing via
|
100
|
-
# `:stub_responses => true`.
|
101
|
-
def stub_responses: (Symbol operation_name, *Hearth::Stubbing::Stubs stubs) -> void
|
102
|
-
end
|
103
|
-
end
|
@@ -1,14 +0,0 @@
|
|
1
|
-
module Hearth
|
2
|
-
# @api private
|
3
|
-
module Stubbing
|
4
|
-
# Provides a thread safe data structure for adding and getting stubs
|
5
|
-
# per operation.
|
6
|
-
class Stubs
|
7
|
-
def initialize: () -> Stubs
|
8
|
-
|
9
|
-
def add_stubs: (Symbol operation_name, Array[Hash[Symbol, untyped]] | Proc | StandardError stubs) -> void
|
10
|
-
|
11
|
-
def next: (Symbol operation_name) -> Hash[Symbol, untyped]
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|