hearth 1.0.0.pre1 → 1.0.0.pre3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (176) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +9 -4
  3. data/VERSION +1 -1
  4. data/lib/hearth/anonymous_auth_resolver.rb +11 -0
  5. data/lib/hearth/api_error.rb +15 -1
  6. data/lib/hearth/auth_option.rb +21 -0
  7. data/lib/hearth/auth_schemes/anonymous.rb +21 -0
  8. data/lib/hearth/auth_schemes/http_api_key.rb +16 -0
  9. data/lib/hearth/auth_schemes/http_basic.rb +16 -0
  10. data/lib/hearth/auth_schemes/http_bearer.rb +16 -0
  11. data/lib/hearth/auth_schemes/http_digest.rb +16 -0
  12. data/lib/hearth/auth_schemes.rb +32 -0
  13. data/lib/hearth/checksums.rb +31 -0
  14. data/lib/hearth/client.rb +66 -0
  15. data/lib/hearth/client_stubs.rb +128 -0
  16. data/lib/hearth/config/env_provider.rb +53 -0
  17. data/lib/hearth/config/resolver.rb +53 -0
  18. data/lib/hearth/configuration.rb +15 -0
  19. data/lib/hearth/connection_pool.rb +77 -0
  20. data/lib/hearth/context.rb +29 -4
  21. data/lib/hearth/dns/host_address.rb +27 -0
  22. data/lib/hearth/dns/host_resolver.rb +92 -0
  23. data/lib/hearth/dns.rb +48 -0
  24. data/lib/hearth/endpoint_rules.rb +154 -0
  25. data/lib/hearth/http/api_error.rb +4 -8
  26. data/lib/hearth/http/client.rb +206 -59
  27. data/lib/hearth/http/error_inspector.rb +85 -0
  28. data/lib/hearth/http/error_parser.rb +18 -20
  29. data/lib/hearth/http/field.rb +49 -0
  30. data/lib/hearth/http/fields.rb +117 -0
  31. data/lib/hearth/http/header_list_builder.rb +42 -0
  32. data/lib/hearth/http/header_list_parser.rb +92 -0
  33. data/lib/hearth/http/middleware/content_length.rb +7 -4
  34. data/lib/hearth/http/middleware/content_md5.rb +30 -0
  35. data/lib/hearth/http/middleware/request_compression.rb +154 -0
  36. data/lib/hearth/http/middleware.rb +12 -0
  37. data/lib/hearth/http/networking_error.rb +1 -14
  38. data/lib/hearth/http/request.rb +83 -56
  39. data/lib/hearth/http/response.rb +42 -13
  40. data/lib/hearth/http.rb +16 -5
  41. data/lib/hearth/identities/anonymous.rb +8 -0
  42. data/lib/hearth/identities/http_api_key.rb +16 -0
  43. data/lib/hearth/identities/http_bearer.rb +16 -0
  44. data/lib/hearth/identities/http_login.rb +20 -0
  45. data/lib/hearth/identities.rb +21 -0
  46. data/lib/hearth/identity_provider.rb +17 -0
  47. data/lib/hearth/interceptor.rb +506 -0
  48. data/lib/hearth/interceptor_context.rb +40 -0
  49. data/lib/hearth/interceptor_list.rb +48 -0
  50. data/lib/hearth/interceptors.rb +76 -0
  51. data/lib/hearth/json.rb +4 -4
  52. data/lib/hearth/middleware/auth.rb +103 -0
  53. data/lib/hearth/middleware/build.rb +32 -1
  54. data/lib/hearth/middleware/endpoint.rb +79 -0
  55. data/lib/hearth/middleware/host_prefix.rb +11 -8
  56. data/lib/hearth/middleware/initialize.rb +57 -0
  57. data/lib/hearth/middleware/parse.rb +45 -7
  58. data/lib/hearth/middleware/retry.rb +105 -24
  59. data/lib/hearth/middleware/send.rb +137 -26
  60. data/lib/hearth/middleware/sign.rb +65 -0
  61. data/lib/hearth/middleware/validate.rb +11 -1
  62. data/lib/hearth/middleware.rb +20 -8
  63. data/lib/hearth/middleware_stack.rb +2 -44
  64. data/lib/hearth/networking_error.rb +18 -0
  65. data/lib/hearth/number_helper.rb +3 -3
  66. data/lib/hearth/output.rb +8 -4
  67. data/lib/hearth/plugin_list.rb +53 -0
  68. data/lib/hearth/query/param.rb +56 -0
  69. data/lib/hearth/query/param_list.rb +54 -0
  70. data/lib/hearth/query/param_matcher.rb +31 -0
  71. data/lib/hearth/refreshing_identity_provider.rb +63 -0
  72. data/lib/hearth/request.rb +22 -0
  73. data/lib/hearth/response.rb +36 -0
  74. data/lib/hearth/retry/adaptive.rb +60 -0
  75. data/lib/hearth/retry/capacity_not_available_error.rb +9 -0
  76. data/lib/hearth/retry/client_rate_limiter.rb +145 -0
  77. data/lib/hearth/retry/exponential_backoff.rb +15 -0
  78. data/lib/hearth/retry/retry_quota.rb +56 -0
  79. data/lib/hearth/retry/standard.rb +46 -0
  80. data/lib/hearth/retry.rb +29 -0
  81. data/lib/hearth/signers/anonymous.rb +16 -0
  82. data/lib/hearth/signers/http_api_key.rb +29 -0
  83. data/lib/hearth/signers/http_basic.rb +23 -0
  84. data/lib/hearth/signers/http_bearer.rb +19 -0
  85. data/lib/hearth/signers/http_digest.rb +19 -0
  86. data/lib/hearth/signers.rb +23 -0
  87. data/lib/hearth/structure.rb +7 -3
  88. data/lib/hearth/stubs.rb +38 -0
  89. data/lib/hearth/time_helper.rb +6 -5
  90. data/lib/hearth/validator.rb +60 -5
  91. data/lib/hearth/waiters/poller.rb +10 -9
  92. data/lib/hearth/waiters/waiter.rb +23 -9
  93. data/lib/hearth/xml/formatter.rb +11 -2
  94. data/lib/hearth/xml/node.rb +2 -3
  95. data/lib/hearth/xml/node_matcher.rb +0 -1
  96. data/lib/hearth.rb +37 -6
  97. data/sig/lib/hearth/aliases.rbs +6 -0
  98. data/sig/lib/hearth/anonymous_auth_resolver.rbs +5 -0
  99. data/sig/lib/hearth/api_error.rbs +13 -0
  100. data/sig/lib/hearth/auth_option.rbs +11 -0
  101. data/sig/lib/hearth/auth_schemes/anonymous.rbs +7 -0
  102. data/sig/lib/hearth/auth_schemes/http_api_key.rbs +7 -0
  103. data/sig/lib/hearth/auth_schemes/http_basic.rbs +7 -0
  104. data/sig/lib/hearth/auth_schemes/http_bearer.rbs +7 -0
  105. data/sig/lib/hearth/auth_schemes/http_digest.rbs +7 -0
  106. data/sig/lib/hearth/auth_schemes.rbs +13 -0
  107. data/sig/lib/hearth/block_io.rbs +9 -0
  108. data/sig/lib/hearth/client.rbs +9 -0
  109. data/sig/lib/hearth/client_stubs.rbs +5 -0
  110. data/sig/lib/hearth/configuration.rbs +7 -0
  111. data/sig/lib/hearth/dns/host_address.rbs +11 -0
  112. data/sig/lib/hearth/dns/host_resolver.rbs +19 -0
  113. data/sig/lib/hearth/endpoint_rules.rbs +17 -0
  114. data/sig/lib/hearth/http/api_error.rbs +13 -0
  115. data/sig/lib/hearth/http/client.rbs +9 -0
  116. data/sig/lib/hearth/http/field.rbs +19 -0
  117. data/sig/lib/hearth/http/fields.rbs +43 -0
  118. data/sig/lib/hearth/http/header_list_builder.rbs +15 -0
  119. data/sig/lib/hearth/http/header_list_parser.rbs +19 -0
  120. data/sig/lib/hearth/http/networking_error.rbs +6 -0
  121. data/sig/lib/hearth/http/request.rbs +25 -0
  122. data/sig/lib/hearth/http/response.rbs +21 -0
  123. data/sig/lib/hearth/identities/anonymous.rbs +6 -0
  124. data/sig/lib/hearth/identities/http_api_key.rbs +9 -0
  125. data/sig/lib/hearth/identities/http_bearer.rbs +9 -0
  126. data/sig/lib/hearth/identities/http_login.rbs +11 -0
  127. data/sig/lib/hearth/identities.rbs +9 -0
  128. data/sig/lib/hearth/identity_provider.rbs +7 -0
  129. data/sig/lib/hearth/interceptor.rbs +9 -0
  130. data/sig/lib/hearth/interceptor_context.rbs +17 -0
  131. data/sig/lib/hearth/interceptor_list.rbs +16 -0
  132. data/sig/lib/hearth/interfaces.rbs +87 -0
  133. data/sig/lib/hearth/json/parse_error.rbs +9 -0
  134. data/sig/lib/hearth/networking_error.rbs +7 -0
  135. data/sig/lib/hearth/output.rbs +11 -0
  136. data/sig/lib/hearth/plugin_list.rbs +13 -0
  137. data/sig/lib/hearth/query/param.rbs +17 -0
  138. data/sig/lib/hearth/query/param_list.rbs +25 -0
  139. data/sig/lib/hearth/refreshing_identity_provider.rbs +10 -0
  140. data/sig/lib/hearth/request.rbs +9 -0
  141. data/sig/lib/hearth/response.rbs +11 -0
  142. data/sig/lib/hearth/retry/adaptive.rbs +13 -0
  143. data/sig/lib/hearth/retry/exponential_backoff.rbs +7 -0
  144. data/sig/lib/hearth/retry/standard.rbs +13 -0
  145. data/sig/lib/hearth/retry/strategy.rbs +11 -0
  146. data/sig/lib/hearth/retry.rbs +9 -0
  147. data/sig/lib/hearth/signers/anonymous.rbs +9 -0
  148. data/sig/lib/hearth/signers/http_api_key.rbs +9 -0
  149. data/sig/lib/hearth/signers/http_basic.rbs +9 -0
  150. data/sig/lib/hearth/signers/http_bearer.rbs +9 -0
  151. data/sig/lib/hearth/signers/http_digest.rbs +9 -0
  152. data/sig/lib/hearth/signers.rbs +9 -0
  153. data/sig/lib/hearth/structure.rbs +6 -0
  154. data/sig/lib/hearth/stubs.rbs +9 -0
  155. data/sig/lib/hearth/union.rbs +5 -0
  156. data/sig/lib/hearth/waiters/waiter.rbs +17 -0
  157. data/sig/lib/hearth/xml/parse_error.rbs +9 -0
  158. metadata +151 -25
  159. data/lib/hearth/http/headers.rb +0 -70
  160. data/lib/hearth/middleware/around_handler.rb +0 -24
  161. data/lib/hearth/middleware/request_handler.rb +0 -24
  162. data/lib/hearth/middleware/response_handler.rb +0 -25
  163. data/lib/hearth/middleware_builder.rb +0 -246
  164. data/lib/hearth/stubbing/client_stubs.rb +0 -115
  165. data/lib/hearth/stubbing/stubs.rb +0 -32
  166. data/lib/hearth/waiters/errors.rb +0 -15
  167. data/sig/lib/seahorse/api_error.rbs +0 -10
  168. data/sig/lib/seahorse/document.rbs +0 -2
  169. data/sig/lib/seahorse/http/api_error.rbs +0 -21
  170. data/sig/lib/seahorse/http/headers.rbs +0 -47
  171. data/sig/lib/seahorse/http/response.rbs +0 -21
  172. data/sig/lib/seahorse/simple_delegator.rbs +0 -3
  173. data/sig/lib/seahorse/structure.rbs +0 -18
  174. data/sig/lib/seahorse/stubbing/client_stubs.rbs +0 -103
  175. data/sig/lib/seahorse/stubbing/stubs.rbs +0 -14
  176. data/sig/lib/seahorse/union.rbs +0 -6
@@ -2,41 +2,122 @@
2
2
 
3
3
  module Hearth
4
4
  module Middleware
5
- # A middleware that retries the request.
6
- # @api private
5
+ # A middleware that retries the request using a retry strategy.
7
6
  class Retry
7
+ include Middleware::Logging
8
+
8
9
  # @param [Class] app The next middleware in the stack.
9
- # @param [Integer] max_attempts The maximum number of attempts to make
10
- # before giving up.
11
- # @param [Integer] max_delay The maximum delay between attempts.
12
- def initialize(app, max_attempts:, max_delay:)
10
+ # @param [Strategy] retry_strategy (Standard) The retry strategy
11
+ # to use. Hearth has two built in classes, Standard and Adaptive.
12
+ # * `Retry::Standard` - A standardized set of retry rules across
13
+ # the AWS SDKs. This includes support for retry quotas, which limit
14
+ # the number of unsuccessful retries a client can make.
15
+ # * `Retry::Adaptive` - An experimental retry mode that includes
16
+ # all the functionality of `standard` mode along with automatic
17
+ # client side throttling. This is a provisional mode that may change
18
+ # behavior in the future.
19
+ def initialize(app, retry_strategy:, error_inspector_class:)
13
20
  @app = app
14
- @max_attempts = max_attempts
15
- @max_delay = max_delay
21
+ @retry_strategy = retry_strategy
22
+ @error_inspector_class = error_inspector_class
23
+
24
+ @retries = 0
16
25
  end
17
26
 
18
- # @param input
19
- # @param context
20
- # @return [Output]
21
27
  def call(input, context)
22
- attempt = 1
23
- begin
24
- @app.call(input, context)
25
- rescue Hearth::HTTP::NetworkingError => e
26
- raise e if attempt >= @max_attempts
27
-
28
- Kernel.sleep(backoff_with_jitter(attempt))
29
- attempt += 1
30
- retry
28
+ interceptor_error = Interceptors.invoke(
29
+ hook: Interceptor::MODIFY_BEFORE_RETRY_LOOP,
30
+ input: input,
31
+ context: context,
32
+ output: nil,
33
+ aggregate_errors: false
34
+ )
35
+ return Hearth::Output.new(error: interceptor_error) if interceptor_error
36
+
37
+ output = nil
38
+ token = @retry_strategy.acquire_initial_retry_token(nil)
39
+ log_debug(context, "Starting retry loop with token: #{token}")
40
+ loop do
41
+ interceptor_error = Interceptors.invoke(
42
+ hook: Interceptor::READ_BEFORE_ATTEMPT,
43
+ input: input,
44
+ context: context,
45
+ output: nil,
46
+ aggregate_errors: true
47
+ )
48
+
49
+ output =
50
+ if interceptor_error
51
+ Hearth::Output.new(error: interceptor_error)
52
+ else
53
+ log_debug(context, 'Attempting request in retry loop')
54
+ @app.call(input, context)
55
+ end
56
+
57
+ interceptor_error = Interceptors.invoke(
58
+ hook: Interceptor::MODIFY_BEFORE_ATTEMPT_COMPLETION,
59
+ input: input,
60
+ context: context,
61
+ output: output,
62
+ aggregate_errors: false
63
+ )
64
+ output.error = interceptor_error if interceptor_error
65
+
66
+ interceptor_error = Interceptors.invoke(
67
+ hook: Interceptor::READ_AFTER_ATTEMPT,
68
+ input: input,
69
+ context: context,
70
+ output: output,
71
+ aggregate_errors: true
72
+ )
73
+ output.error = interceptor_error if interceptor_error
74
+
75
+ if (error = output.error)
76
+ log_debug(context, "Request failed with error: #{error}")
77
+ break unless retryable?(context.request)
78
+
79
+ error_info = @error_inspector_class.new(error, context.response)
80
+ token = @retry_strategy.refresh_retry_token(token, error_info)
81
+ break unless token
82
+
83
+ log_debug(context, "Retry token refreshed: #{token}")
84
+ log_debug(context, "Sleeping for #{token.retry_delay} seconds")
85
+ Kernel.sleep(token.retry_delay)
86
+ else
87
+ @retry_strategy.record_success(token)
88
+ log_debug(context, 'Request succeeded')
89
+ break
90
+ end
91
+
92
+ reset_request(context)
93
+ reset_response(context, output)
94
+ @retries += 1
31
95
  end
96
+ log_debug(context, 'Finished retry loop')
97
+ log_debug(context, "Total retries: #{@retries}")
98
+ output
32
99
  end
33
100
 
34
101
  private
35
102
 
36
- # https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/
37
- def backoff_with_jitter(attempt)
38
- # scales like 1,2,4,8
39
- Kernel.rand * [@max_delay, 2**(attempt - 1)].min
103
+ def retryable?(request)
104
+ # IO responds to #rewind however it returns an illegal seek error
105
+ request.body.respond_to?(:rewind) && !request.body.instance_of?(IO)
106
+ end
107
+
108
+ def reset_request(context)
109
+ request = context.request
110
+ request.body.rewind
111
+
112
+ context.auth.signer.reset(
113
+ request: request,
114
+ properties: context.auth.signer_properties
115
+ )
116
+ end
117
+
118
+ def reset_response(context, output)
119
+ context.response.reset
120
+ output.error = nil
40
121
  end
41
122
  end
42
123
  end
@@ -3,60 +3,171 @@
3
3
  module Hearth
4
4
  module Middleware
5
5
  # A middleware used to send the request.
6
- # @api private
7
6
  class Send
7
+ include Middleware::Logging
8
+
8
9
  # @param [Class] _app The next middleware in the stack.
9
10
  # @param [Boolean] stub_responses If true, a request is not sent and a
10
11
  # stubbed response is returned.
11
- # @param [Class] stub_class A stub object that is responsible for creating
12
- # a stubbed response. It must respond to #stub and take the response
13
- # and stub data as arguments.
14
- # @param [Stubs] stubs A {Hearth::Stubbing:Stubs} object containing
12
+ # @param [Class] stub_data_class A stub object that is responsible for
13
+ # creating a stubbed data response. It must respond to #stub and take
14
+ # the response and stub data as arguments.
15
+ # @param [Array<Class>] stub_error_classes An array of error classes
16
+ # that are responsible for creating a stubbed error response. They
17
+ # must respond to #stub and take the response and stub data as
18
+ # arguments.
19
+ # @param [Stubs] stubs A {Hearth::Stubs} object containing
15
20
  # stubbed data for any given operation.
16
- def initialize(_app, client:, stub_responses:, stub_class:, stubs:)
21
+ def initialize(_app, client:, stub_responses:,
22
+ stub_data_class:, stub_error_classes:, stubs:)
17
23
  @client = client
18
24
  @stub_responses = stub_responses
19
- @stub_class = stub_class
25
+ @stub_data_class = stub_data_class
26
+ @stub_error_classes = stub_error_classes
20
27
  @stubs = stubs
21
28
  end
22
29
 
23
- # @param _input
30
+ # @param input
24
31
  # @param context
25
32
  # @return [Output]
26
- def call(_input, context)
33
+ def call(input, context)
34
+ interceptor_error = Interceptors.invoke(
35
+ hook: Interceptor::MODIFY_BEFORE_TRANSMIT,
36
+ input: input,
37
+ context: context,
38
+ output: nil,
39
+ aggregate_errors: false
40
+ )
41
+ return Hearth::Output.new(error: interceptor_error) if interceptor_error
42
+
43
+ interceptor_error = Interceptors.invoke(
44
+ hook: Interceptor::READ_BEFORE_TRANSMIT,
45
+ input: input,
46
+ context: context,
47
+ output: nil,
48
+ aggregate_errors: false
49
+ )
50
+ return Hearth::Output.new(error: interceptor_error) if interceptor_error
51
+
52
+ output = Output.new
27
53
  if @stub_responses
28
- stub = @stubs.next(context.operation_name)
29
- output = Output.new
30
- apply_stub(stub, context, output)
31
- output
54
+ stub_response(input, context, output)
32
55
  else
33
- @client.transmit(
34
- request: context.request,
35
- response: context.response
36
- )
37
- Output.new
56
+ send_request(context, output)
38
57
  end
58
+
59
+ interceptor_error = Interceptors.invoke(
60
+ hook: Interceptor::READ_AFTER_TRANSMIT,
61
+ input: input,
62
+ context: context,
63
+ output: output,
64
+ aggregate_errors: false
65
+ )
66
+ output.error = interceptor_error if interceptor_error
67
+
68
+ output
39
69
  end
40
70
 
41
71
  private
42
72
 
43
- def apply_stub(stub, context, output)
73
+ def stub_response(input, context, output)
74
+ stub = @stubs.next(context.operation_name)
75
+ log_debug(context, "Stubbing response with stub: #{stub}")
76
+ apply_stub(stub, input, context, output)
77
+ log_debug(context, "Stubbed response: #{context.response.inspect}")
78
+ return unless context.response.body.respond_to?(:rewind)
79
+
80
+ context.response.body.rewind
81
+ end
82
+
83
+ def send_request(context, output)
84
+ log_debug(context, "Sending request: #{context.request.inspect}")
85
+ @client.transmit(
86
+ request: context.request,
87
+ response: context.response,
88
+ logger: context.logger
89
+ )
90
+ log_debug(context, "Received response: #{context.response.inspect}")
91
+ rescue Hearth::NetworkingError => e
92
+ output.error = e
93
+ end
94
+
95
+ def apply_stub(stub, input, context, output)
44
96
  case stub
45
97
  when Proc
46
- stub = stub.call(context)
47
- apply_stub(stub, context, output) if stub
48
- when Exception
98
+ stub = stub.call(input)
99
+ apply_stub(stub, input, context, output)
100
+ when Exception, ApiError
49
101
  output.error = stub
50
- when Class
51
- output.error = stub.new
52
102
  when Hash
53
- @stub_class.stub(context.response, stub: stub)
103
+ apply_stub_hash(stub, context)
54
104
  when NilClass
55
- @stub_class.stub(context.response, stub: @stub_class.default)
105
+ apply_stub_nil(context)
106
+ when Hearth::Structure
107
+ apply_stub_hearth_structure(stub, context)
108
+ when Hearth::Response
109
+ context.response.replace(stub)
56
110
  else
57
111
  raise ArgumentError, 'Unsupported stub type'
58
112
  end
59
113
  end
114
+
115
+ def apply_stub_hash(stub, context)
116
+ if stub.key?(:error) && !stub.key?(:data)
117
+ apply_stub_hash_error(stub, context)
118
+ elsif stub.key?(:data) && !stub.key?(:error)
119
+ apply_stub_hash_data(stub, context)
120
+ else
121
+ raise ArgumentError, 'Unsupported stub hash, must be :data or :error'
122
+ end
123
+ end
124
+
125
+ def apply_stub_hash_data(stub, context)
126
+ output = @stub_data_class.build(stub[:data], context: 'stub')
127
+ @stub_data_class.validate!(output, context: 'stub')
128
+ @stub_data_class.stub(context.response, stub: output)
129
+ end
130
+
131
+ def apply_stub_hash_error(stub, context)
132
+ stub_error_class = stub_error_class(stub[:error][:class])
133
+ output = stub_error_class.build(
134
+ stub[:error][:data] || {},
135
+ context: 'stub'
136
+ )
137
+ stub_error_class.validate!(output, context: 'stub')
138
+ stub_error_class.stub(context.response, stub: output)
139
+ end
140
+
141
+ def stub_error_class(error_class)
142
+ raise ArgumentError, 'Missing stub error class' unless error_class
143
+
144
+ unless error_class.is_a?(Class)
145
+ raise ArgumentError, 'Stub error class must be a class'
146
+ end
147
+
148
+ error_base_name = error_class.name.split('::').last
149
+ stub_class = @stub_error_classes.find do |stub_error_class|
150
+ stub_base_name = stub_error_class.name.split('::').last
151
+ error_base_name == stub_base_name
152
+ end
153
+ raise ArgumentError, 'Unsupported stub error class' unless stub_class
154
+
155
+ stub_class
156
+ end
157
+
158
+ def apply_stub_nil(context)
159
+ output = @stub_data_class.build(
160
+ @stub_data_class.default,
161
+ context: 'stub'
162
+ )
163
+ @stub_data_class.validate!(output, context: 'stub')
164
+ @stub_data_class.stub(context.response, stub: output)
165
+ end
166
+
167
+ def apply_stub_hearth_structure(stub, context)
168
+ @stub_data_class.validate!(stub, context: 'stub')
169
+ @stub_data_class.stub(context.response, stub: stub)
170
+ end
60
171
  end
61
172
  end
62
173
  end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hearth
4
+ module Middleware
5
+ # A middleware that signs requests using the resolved identity.
6
+ # @api private
7
+ class Sign
8
+ include Middleware::Logging
9
+
10
+ # @param [Class] app The next middleware in the stack.
11
+ def initialize(app)
12
+ @app = app
13
+ end
14
+
15
+ # @param input
16
+ # @param context
17
+ # @return [Output]
18
+ def call(input, context)
19
+ interceptor_error = Interceptors.invoke(
20
+ hook: Interceptor::MODIFY_BEFORE_SIGNING,
21
+ input: input,
22
+ context: context,
23
+ output: nil,
24
+ aggregate_errors: false
25
+ )
26
+ return Hearth::Output.new(error: interceptor_error) if interceptor_error
27
+
28
+ interceptor_error = Interceptors.invoke(
29
+ hook: Interceptor::READ_BEFORE_SIGNING,
30
+ input: input,
31
+ context: context,
32
+ output: nil,
33
+ aggregate_errors: false
34
+ )
35
+ return Hearth::Output.new(error: interceptor_error) if interceptor_error
36
+
37
+ sign_request(context)
38
+ output = @app.call(input, context)
39
+
40
+ interceptor_error = Interceptors.invoke(
41
+ hook: Interceptor::READ_AFTER_SIGNING,
42
+ input: input,
43
+ context: context,
44
+ output: output,
45
+ aggregate_errors: false
46
+ )
47
+ output.error = interceptor_error if interceptor_error
48
+
49
+ output
50
+ end
51
+
52
+ private
53
+
54
+ def sign_request(context)
55
+ log_debug(context, "Signing request with: #{context.auth.signer}")
56
+ context.auth.signer.sign(
57
+ request: context.request,
58
+ identity: context.auth.identity,
59
+ properties: context.auth.signer_properties
60
+ )
61
+ log_debug(context, "Signed request: #{context.request.inspect}")
62
+ end
63
+ end
64
+ end
65
+ end
@@ -5,6 +5,8 @@ module Hearth
5
5
  # A middleware used to validate input.
6
6
  # @api private
7
7
  class Validate
8
+ include Middleware::Logging
9
+
8
10
  # @param [Class] app The next middleware in the stack.
9
11
  # @param [Boolean] validate_input If true, the input is validated against
10
12
  # the model and an error is raised for unexpected types.
@@ -21,9 +23,17 @@ module Hearth
21
23
  # @param context
22
24
  # @return [Output]
23
25
  def call(input, context)
24
- @validator.validate!(input, context: 'input') if @validate_input
26
+ validate_input(input, context) if @validate_input
25
27
  @app.call(input, context)
26
28
  end
29
+
30
+ private
31
+
32
+ def validate_input(input, context)
33
+ log_debug(context, "Validating input with: #{input}")
34
+ @validator.validate!(input, context: 'input')
35
+ log_debug(context, 'Validated input')
36
+ end
27
37
  end
28
38
  end
29
39
  end
@@ -1,16 +1,28 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative 'middleware/around_handler'
3
+ module Hearth
4
+ # @api private
5
+ module Middleware
6
+ # Helper methods for logging to middleware.
7
+ module Logging
8
+ # Logs a debug message to the logger on context. The message is prefixed
9
+ # with the invocation id and the class name.
10
+ def log_debug(context, message)
11
+ context.logger.debug(
12
+ "[#{context.invocation_id}] [#{self.class}] #{message}"
13
+ )
14
+ end
15
+ end
16
+ end
17
+ end
18
+
19
+ require_relative 'middleware/auth'
4
20
  require_relative 'middleware/build'
21
+ require_relative 'middleware/endpoint'
5
22
  require_relative 'middleware/host_prefix'
6
23
  require_relative 'middleware/parse'
7
- require_relative 'middleware/request_handler'
8
- require_relative 'middleware/response_handler'
9
24
  require_relative 'middleware/retry'
10
25
  require_relative 'middleware/send'
26
+ require_relative 'middleware/sign'
11
27
  require_relative 'middleware/validate'
12
-
13
- module Hearth
14
- # @api private
15
- module Middleware; end
16
- end
28
+ require_relative 'middleware/initialize'
@@ -11,53 +11,11 @@ module Hearth
11
11
  @middleware.push([middleware, middleware_kwargs])
12
12
  end
13
13
 
14
- def use_before(before, middleware, **middleware_kwargs)
15
- new_middleware = []
16
- @middleware.each do |klass, args|
17
- new_middleware << [middleware, middleware_kwargs] if before == klass
18
- new_middleware << [klass, args]
19
- end
20
- unless new_middleware.size == @middleware.size + 1
21
- raise ArgumentError,
22
- "Failed to insert #{middleware} before #{before}"
23
- end
24
-
25
- @middleware = new_middleware
26
- end
27
-
28
- def use_after(after, middleware, **middleware_kwargs)
29
- new_middleware = []
30
- @middleware.each do |klass, args|
31
- new_middleware << [klass, args]
32
- new_middleware << [middleware, middleware_kwargs] if after == klass
33
- end
34
- unless new_middleware.size == @middleware.size + 1
35
- raise ArgumentError,
36
- "Failed to insert #{middleware} after #{after}"
37
- end
38
-
39
- @middleware = new_middleware
40
- end
41
-
42
- def remove(remove)
43
- new_middleware = []
44
- @middleware.each do |klass, args|
45
- new_middleware << [klass, args] unless klass == remove
46
- end
47
-
48
- unless new_middleware.size == @middleware.size - 1
49
- raise ArgumentError,
50
- "Failed to remove #{remove}"
51
- end
52
-
53
- @middleware = new_middleware
54
- end
55
-
56
14
  # @param input
57
15
  # @param context
58
16
  # @return [Output]
59
- def run(input:, context:)
60
- stack.call(input, context)
17
+ def run(input, context)
18
+ stack&.call(input, context) || Output.new
61
19
  end
62
20
 
63
21
  private
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hearth
4
+ # Thrown by a Client when encountering a networking error while transmitting
5
+ # a request or receiving a response. You can access the original error
6
+ # by calling {#original_error}.
7
+ class NetworkingError < StandardError
8
+ MSG = 'Encountered an error while transmitting the request: %<message>s'
9
+
10
+ def initialize(original_error)
11
+ @original_error = original_error
12
+ super(format(MSG, message: original_error.message))
13
+ end
14
+
15
+ # @return [StandardError]
16
+ attr_reader :original_error
17
+ end
18
+ end
@@ -6,8 +6,8 @@ module Hearth
6
6
  # @api private
7
7
  module NumberHelper
8
8
  class << self
9
- # @param [Number,String] input
10
- # @return [String] The serialized number
9
+ # @param [Number] input
10
+ # @return [Number, String] The serialized number
11
11
  def serialize(input)
12
12
  if input == ::Float::INFINITY then 'Infinity'
13
13
  elsif input == -::Float::INFINITY then '-Infinity'
@@ -18,7 +18,7 @@ module Hearth
18
18
  end
19
19
 
20
20
  # @param [String] str
21
- # @return [Number] The input as a number
21
+ # @return [Float, nil] The input as a number
22
22
  def deserialize(str)
23
23
  case str
24
24
  when 'Infinity' then ::Float::INFINITY
data/lib/hearth/output.rb CHANGED
@@ -2,19 +2,23 @@
2
2
 
3
3
  module Hearth
4
4
  # A wrapper class that contains an error or data from the response.
5
- # @api private
6
5
  class Output
7
- # @param [StandardError] error The error class to be raised.
6
+ # @param [ApiError] error The error class to be raised.
8
7
  # @param [Struct] data The data returned by a client.
9
- def initialize(error: nil, data: nil)
8
+ # @param [Hash] metadata Response metadata set by client middleware.
9
+ def initialize(error: nil, data: nil, metadata: {})
10
10
  @error = error
11
11
  @data = data
12
+ @metadata = metadata
12
13
  end
13
14
 
14
- # @return [StandardError, nil]
15
+ # @return [ApiError, nil]
15
16
  attr_accessor :error
16
17
 
17
18
  # @return [Struct, nil]
18
19
  attr_accessor :data
20
+
21
+ # @return [Hash]
22
+ attr_accessor :metadata
19
23
  end
20
24
  end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hearth
4
+ # A list of plugins that can be applied to config. Plugins are callables
5
+ # that take one argument (config) and are called during client initialization
6
+ # or operation invocation to modify config.
7
+ class PluginList
8
+ include Enumerable
9
+
10
+ # Initialize a PluginList.
11
+ #
12
+ # @param [Array] plugins ([]) A list of plugins to initialize with
13
+ def initialize(plugins = [])
14
+ unless plugins.respond_to?(:each)
15
+ raise ArgumentError, 'Plugins must be an enumerable'
16
+ end
17
+
18
+ @plugins = []
19
+ plugins.each { |p| add(p) }
20
+ end
21
+
22
+ # Add a plugin
23
+ #
24
+ # @param [Callable] plugin
25
+ def add(plugin)
26
+ unless valid_plugin?(plugin)
27
+ raise ArgumentError,
28
+ 'Plugin must be callable and take one argument (config)'
29
+ end
30
+
31
+ @plugins << plugin
32
+ end
33
+
34
+ alias << add
35
+
36
+ def each(&block)
37
+ @plugins.each(&block)
38
+ end
39
+
40
+ private
41
+
42
+ # plugins must be callable and take exactly 1 argument (config)
43
+ def valid_plugin?(plugin)
44
+ case plugin
45
+ when Proc
46
+ # the arity of a proc.method(:call) is -1, need to special case
47
+ plugin.arity == 1
48
+ else
49
+ plugin.respond_to?(:call) && plugin.method(:call).arity == 1
50
+ end
51
+ end
52
+ end
53
+ end