faraday 0.15.4 → 1.10.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (104) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +380 -0
  3. data/LICENSE.md +1 -1
  4. data/README.md +16 -344
  5. data/Rakefile +7 -0
  6. data/examples/client_spec.rb +97 -0
  7. data/examples/client_test.rb +118 -0
  8. data/lib/faraday/adapter/test.rb +118 -69
  9. data/lib/faraday/adapter/typhoeus.rb +4 -1
  10. data/lib/faraday/adapter.rb +72 -22
  11. data/lib/faraday/adapter_registry.rb +30 -0
  12. data/lib/faraday/autoload.rb +39 -36
  13. data/lib/faraday/connection.rb +343 -185
  14. data/lib/faraday/dependency_loader.rb +37 -0
  15. data/lib/faraday/encoders/flat_params_encoder.rb +105 -0
  16. data/lib/faraday/encoders/nested_params_encoder.rb +176 -0
  17. data/lib/faraday/error.rb +118 -38
  18. data/lib/faraday/logging/formatter.rb +105 -0
  19. data/lib/faraday/methods.rb +6 -0
  20. data/lib/faraday/middleware.rb +19 -25
  21. data/lib/faraday/middleware_registry.rb +129 -0
  22. data/lib/faraday/options/connection_options.rb +22 -0
  23. data/lib/faraday/options/env.rb +181 -0
  24. data/lib/faraday/options/proxy_options.rb +32 -0
  25. data/lib/faraday/options/request_options.rb +22 -0
  26. data/lib/faraday/options/ssl_options.rb +59 -0
  27. data/lib/faraday/options.rb +39 -194
  28. data/lib/faraday/parameters.rb +4 -197
  29. data/lib/faraday/rack_builder.rb +77 -65
  30. data/lib/faraday/request/authorization.rb +51 -30
  31. data/lib/faraday/request/basic_authentication.rb +14 -7
  32. data/lib/faraday/request/instrumentation.rb +45 -27
  33. data/lib/faraday/request/json.rb +55 -0
  34. data/lib/faraday/request/token_authentication.rb +15 -10
  35. data/lib/faraday/request/url_encoded.rb +43 -23
  36. data/lib/faraday/request.rb +83 -42
  37. data/lib/faraday/response/json.rb +54 -0
  38. data/lib/faraday/response/logger.rb +20 -69
  39. data/lib/faraday/response/raise_error.rb +49 -14
  40. data/lib/faraday/response.rb +29 -23
  41. data/lib/faraday/utils/headers.rb +139 -0
  42. data/lib/faraday/utils/params_hash.rb +61 -0
  43. data/lib/faraday/utils.rb +38 -247
  44. data/lib/faraday/version.rb +5 -0
  45. data/lib/faraday.rb +134 -189
  46. data/spec/external_adapters/faraday_specs_setup.rb +14 -0
  47. data/spec/faraday/adapter/em_http_spec.rb +49 -0
  48. data/spec/faraday/adapter/em_synchrony_spec.rb +18 -0
  49. data/spec/faraday/adapter/excon_spec.rb +49 -0
  50. data/spec/faraday/adapter/httpclient_spec.rb +73 -0
  51. data/spec/faraday/adapter/net_http_spec.rb +64 -0
  52. data/spec/faraday/adapter/patron_spec.rb +18 -0
  53. data/spec/faraday/adapter/rack_spec.rb +8 -0
  54. data/spec/faraday/adapter/test_spec.rb +377 -0
  55. data/spec/faraday/adapter/typhoeus_spec.rb +7 -0
  56. data/spec/faraday/adapter_registry_spec.rb +28 -0
  57. data/spec/faraday/adapter_spec.rb +55 -0
  58. data/spec/faraday/composite_read_io_spec.rb +80 -0
  59. data/spec/faraday/connection_spec.rb +736 -0
  60. data/spec/faraday/error_spec.rb +60 -0
  61. data/spec/faraday/middleware_spec.rb +52 -0
  62. data/spec/faraday/options/env_spec.rb +70 -0
  63. data/spec/faraday/options/options_spec.rb +297 -0
  64. data/spec/faraday/options/proxy_options_spec.rb +44 -0
  65. data/spec/faraday/options/request_options_spec.rb +19 -0
  66. data/spec/faraday/params_encoders/flat_spec.rb +42 -0
  67. data/spec/faraday/params_encoders/nested_spec.rb +142 -0
  68. data/spec/faraday/rack_builder_spec.rb +345 -0
  69. data/spec/faraday/request/authorization_spec.rb +96 -0
  70. data/spec/faraday/request/instrumentation_spec.rb +76 -0
  71. data/spec/faraday/request/json_spec.rb +111 -0
  72. data/spec/faraday/request/url_encoded_spec.rb +83 -0
  73. data/spec/faraday/request_spec.rb +120 -0
  74. data/spec/faraday/response/json_spec.rb +119 -0
  75. data/spec/faraday/response/logger_spec.rb +220 -0
  76. data/spec/faraday/response/middleware_spec.rb +68 -0
  77. data/spec/faraday/response/raise_error_spec.rb +169 -0
  78. data/spec/faraday/response_spec.rb +75 -0
  79. data/spec/faraday/utils/headers_spec.rb +82 -0
  80. data/spec/faraday/utils_spec.rb +56 -0
  81. data/spec/faraday_spec.rb +37 -0
  82. data/spec/spec_helper.rb +132 -0
  83. data/spec/support/disabling_stub.rb +14 -0
  84. data/spec/support/fake_safe_buffer.rb +15 -0
  85. data/spec/support/helper_methods.rb +133 -0
  86. data/spec/support/shared_examples/adapter.rb +105 -0
  87. data/spec/support/shared_examples/params_encoder.rb +18 -0
  88. data/spec/support/shared_examples/request_method.rb +262 -0
  89. data/spec/support/streaming_response_checker.rb +35 -0
  90. data/spec/support/webmock_rack_app.rb +68 -0
  91. metadata +222 -29
  92. data/lib/faraday/adapter/em_http.rb +0 -243
  93. data/lib/faraday/adapter/em_http_ssl_patch.rb +0 -56
  94. data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +0 -66
  95. data/lib/faraday/adapter/em_synchrony.rb +0 -106
  96. data/lib/faraday/adapter/excon.rb +0 -82
  97. data/lib/faraday/adapter/httpclient.rb +0 -128
  98. data/lib/faraday/adapter/net_http.rb +0 -152
  99. data/lib/faraday/adapter/net_http_persistent.rb +0 -68
  100. data/lib/faraday/adapter/patron.rb +0 -95
  101. data/lib/faraday/adapter/rack.rb +0 -58
  102. data/lib/faraday/request/multipart.rb +0 -68
  103. data/lib/faraday/request/retry.rb +0 -210
  104. data/lib/faraday/upload_io.rb +0 -67
metadata CHANGED
@@ -1,98 +1,291 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faraday
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.15.4
4
+ version: 1.10.0
5
5
  platform: ruby
6
6
  authors:
7
- - Rick Olson
7
+ - "@technoweenie"
8
+ - "@iMacTia"
9
+ - "@olleolleolle"
8
10
  autorequire:
9
11
  bindir: bin
10
12
  cert_chain: []
11
- date: 2018-11-27 00:00:00.000000000 Z
13
+ date: 2022-02-17 00:00:00.000000000 Z
12
14
  dependencies:
13
15
  - !ruby/object:Gem::Dependency
14
- name: multipart-post
16
+ name: faraday-em_http
15
17
  requirement: !ruby/object:Gem::Requirement
16
18
  requirements:
17
- - - ">="
19
+ - - "~>"
20
+ - !ruby/object:Gem::Version
21
+ version: '1.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - "~>"
27
+ - !ruby/object:Gem::Version
28
+ version: '1.0'
29
+ - !ruby/object:Gem::Dependency
30
+ name: faraday-em_synchrony
31
+ requirement: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - "~>"
34
+ - !ruby/object:Gem::Version
35
+ version: '1.0'
36
+ type: :runtime
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - "~>"
41
+ - !ruby/object:Gem::Version
42
+ version: '1.0'
43
+ - !ruby/object:Gem::Dependency
44
+ name: faraday-excon
45
+ requirement: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '1.1'
50
+ type: :runtime
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - "~>"
55
+ - !ruby/object:Gem::Version
56
+ version: '1.1'
57
+ - !ruby/object:Gem::Dependency
58
+ name: faraday-httpclient
59
+ requirement: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - "~>"
62
+ - !ruby/object:Gem::Version
63
+ version: '1.0'
64
+ type: :runtime
65
+ prerelease: false
66
+ version_requirements: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - "~>"
69
+ - !ruby/object:Gem::Version
70
+ version: '1.0'
71
+ - !ruby/object:Gem::Dependency
72
+ name: faraday-multipart
73
+ requirement: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - "~>"
76
+ - !ruby/object:Gem::Version
77
+ version: '1.0'
78
+ type: :runtime
79
+ prerelease: false
80
+ version_requirements: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - "~>"
83
+ - !ruby/object:Gem::Version
84
+ version: '1.0'
85
+ - !ruby/object:Gem::Dependency
86
+ name: faraday-net_http
87
+ requirement: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - "~>"
90
+ - !ruby/object:Gem::Version
91
+ version: '1.0'
92
+ type: :runtime
93
+ prerelease: false
94
+ version_requirements: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - "~>"
18
97
  - !ruby/object:Gem::Version
19
- version: '1.2'
20
- - - "<"
98
+ version: '1.0'
99
+ - !ruby/object:Gem::Dependency
100
+ name: faraday-net_http_persistent
101
+ requirement: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - "~>"
21
104
  - !ruby/object:Gem::Version
22
- version: '3'
105
+ version: '1.0'
23
106
  type: :runtime
24
107
  prerelease: false
25
108
  version_requirements: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - "~>"
111
+ - !ruby/object:Gem::Version
112
+ version: '1.0'
113
+ - !ruby/object:Gem::Dependency
114
+ name: faraday-patron
115
+ requirement: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - "~>"
118
+ - !ruby/object:Gem::Version
119
+ version: '1.0'
120
+ type: :runtime
121
+ prerelease: false
122
+ version_requirements: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - "~>"
125
+ - !ruby/object:Gem::Version
126
+ version: '1.0'
127
+ - !ruby/object:Gem::Dependency
128
+ name: faraday-rack
129
+ requirement: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - "~>"
132
+ - !ruby/object:Gem::Version
133
+ version: '1.0'
134
+ type: :runtime
135
+ prerelease: false
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - "~>"
139
+ - !ruby/object:Gem::Version
140
+ version: '1.0'
141
+ - !ruby/object:Gem::Dependency
142
+ name: faraday-retry
143
+ requirement: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - "~>"
146
+ - !ruby/object:Gem::Version
147
+ version: '1.0'
148
+ type: :runtime
149
+ prerelease: false
150
+ version_requirements: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - "~>"
153
+ - !ruby/object:Gem::Version
154
+ version: '1.0'
155
+ - !ruby/object:Gem::Dependency
156
+ name: ruby2_keywords
157
+ requirement: !ruby/object:Gem::Requirement
26
158
  requirements:
27
159
  - - ">="
28
160
  - !ruby/object:Gem::Version
29
- version: '1.2'
30
- - - "<"
161
+ version: 0.0.4
162
+ type: :runtime
163
+ prerelease: false
164
+ version_requirements: !ruby/object:Gem::Requirement
165
+ requirements:
166
+ - - ">="
31
167
  - !ruby/object:Gem::Version
32
- version: '3'
168
+ version: 0.0.4
33
169
  description:
34
170
  email: technoweenie@gmail.com
35
171
  executables: []
36
172
  extensions: []
37
173
  extra_rdoc_files: []
38
174
  files:
175
+ - CHANGELOG.md
39
176
  - LICENSE.md
40
177
  - README.md
178
+ - Rakefile
179
+ - examples/client_spec.rb
180
+ - examples/client_test.rb
41
181
  - lib/faraday.rb
42
182
  - lib/faraday/adapter.rb
43
- - lib/faraday/adapter/em_http.rb
44
- - lib/faraday/adapter/em_http_ssl_patch.rb
45
- - lib/faraday/adapter/em_synchrony.rb
46
- - lib/faraday/adapter/em_synchrony/parallel_manager.rb
47
- - lib/faraday/adapter/excon.rb
48
- - lib/faraday/adapter/httpclient.rb
49
- - lib/faraday/adapter/net_http.rb
50
- - lib/faraday/adapter/net_http_persistent.rb
51
- - lib/faraday/adapter/patron.rb
52
- - lib/faraday/adapter/rack.rb
53
183
  - lib/faraday/adapter/test.rb
54
184
  - lib/faraday/adapter/typhoeus.rb
185
+ - lib/faraday/adapter_registry.rb
55
186
  - lib/faraday/autoload.rb
56
187
  - lib/faraday/connection.rb
188
+ - lib/faraday/dependency_loader.rb
189
+ - lib/faraday/encoders/flat_params_encoder.rb
190
+ - lib/faraday/encoders/nested_params_encoder.rb
57
191
  - lib/faraday/error.rb
192
+ - lib/faraday/logging/formatter.rb
193
+ - lib/faraday/methods.rb
58
194
  - lib/faraday/middleware.rb
195
+ - lib/faraday/middleware_registry.rb
59
196
  - lib/faraday/options.rb
197
+ - lib/faraday/options/connection_options.rb
198
+ - lib/faraday/options/env.rb
199
+ - lib/faraday/options/proxy_options.rb
200
+ - lib/faraday/options/request_options.rb
201
+ - lib/faraday/options/ssl_options.rb
60
202
  - lib/faraday/parameters.rb
61
203
  - lib/faraday/rack_builder.rb
62
204
  - lib/faraday/request.rb
63
205
  - lib/faraday/request/authorization.rb
64
206
  - lib/faraday/request/basic_authentication.rb
65
207
  - lib/faraday/request/instrumentation.rb
66
- - lib/faraday/request/multipart.rb
67
- - lib/faraday/request/retry.rb
208
+ - lib/faraday/request/json.rb
68
209
  - lib/faraday/request/token_authentication.rb
69
210
  - lib/faraday/request/url_encoded.rb
70
211
  - lib/faraday/response.rb
212
+ - lib/faraday/response/json.rb
71
213
  - lib/faraday/response/logger.rb
72
214
  - lib/faraday/response/raise_error.rb
73
- - lib/faraday/upload_io.rb
74
215
  - lib/faraday/utils.rb
75
- homepage: https://github.com/lostisland/faraday
216
+ - lib/faraday/utils/headers.rb
217
+ - lib/faraday/utils/params_hash.rb
218
+ - lib/faraday/version.rb
219
+ - spec/external_adapters/faraday_specs_setup.rb
220
+ - spec/faraday/adapter/em_http_spec.rb
221
+ - spec/faraday/adapter/em_synchrony_spec.rb
222
+ - spec/faraday/adapter/excon_spec.rb
223
+ - spec/faraday/adapter/httpclient_spec.rb
224
+ - spec/faraday/adapter/net_http_spec.rb
225
+ - spec/faraday/adapter/patron_spec.rb
226
+ - spec/faraday/adapter/rack_spec.rb
227
+ - spec/faraday/adapter/test_spec.rb
228
+ - spec/faraday/adapter/typhoeus_spec.rb
229
+ - spec/faraday/adapter_registry_spec.rb
230
+ - spec/faraday/adapter_spec.rb
231
+ - spec/faraday/composite_read_io_spec.rb
232
+ - spec/faraday/connection_spec.rb
233
+ - spec/faraday/error_spec.rb
234
+ - spec/faraday/middleware_spec.rb
235
+ - spec/faraday/options/env_spec.rb
236
+ - spec/faraday/options/options_spec.rb
237
+ - spec/faraday/options/proxy_options_spec.rb
238
+ - spec/faraday/options/request_options_spec.rb
239
+ - spec/faraday/params_encoders/flat_spec.rb
240
+ - spec/faraday/params_encoders/nested_spec.rb
241
+ - spec/faraday/rack_builder_spec.rb
242
+ - spec/faraday/request/authorization_spec.rb
243
+ - spec/faraday/request/instrumentation_spec.rb
244
+ - spec/faraday/request/json_spec.rb
245
+ - spec/faraday/request/url_encoded_spec.rb
246
+ - spec/faraday/request_spec.rb
247
+ - spec/faraday/response/json_spec.rb
248
+ - spec/faraday/response/logger_spec.rb
249
+ - spec/faraday/response/middleware_spec.rb
250
+ - spec/faraday/response/raise_error_spec.rb
251
+ - spec/faraday/response_spec.rb
252
+ - spec/faraday/utils/headers_spec.rb
253
+ - spec/faraday/utils_spec.rb
254
+ - spec/faraday_spec.rb
255
+ - spec/spec_helper.rb
256
+ - spec/support/disabling_stub.rb
257
+ - spec/support/fake_safe_buffer.rb
258
+ - spec/support/helper_methods.rb
259
+ - spec/support/shared_examples/adapter.rb
260
+ - spec/support/shared_examples/params_encoder.rb
261
+ - spec/support/shared_examples/request_method.rb
262
+ - spec/support/streaming_response_checker.rb
263
+ - spec/support/webmock_rack_app.rb
264
+ homepage: https://lostisland.github.io/faraday
76
265
  licenses:
77
266
  - MIT
78
- metadata: {}
267
+ metadata:
268
+ homepage_uri: https://lostisland.github.io/faraday
269
+ changelog_uri: https://github.com/lostisland/faraday/releases/tag/v1.10.0
270
+ source_code_uri: https://github.com/lostisland/faraday
271
+ bug_tracker_uri: https://github.com/lostisland/faraday/issues
79
272
  post_install_message:
80
273
  rdoc_options: []
81
274
  require_paths:
82
275
  - lib
276
+ - spec/external_adapters
83
277
  required_ruby_version: !ruby/object:Gem::Requirement
84
278
  requirements:
85
279
  - - ">="
86
280
  - !ruby/object:Gem::Version
87
- version: '1.9'
281
+ version: '2.4'
88
282
  required_rubygems_version: !ruby/object:Gem::Requirement
89
283
  requirements:
90
284
  - - ">="
91
285
  - !ruby/object:Gem::Version
92
286
  version: '0'
93
287
  requirements: []
94
- rubyforge_project:
95
- rubygems_version: 2.7.7
288
+ rubygems_version: 3.0.3.1
96
289
  signing_key:
97
290
  specification_version: 4
98
291
  summary: HTTP/REST API client library.
@@ -1,243 +0,0 @@
1
- module Faraday
2
- class Adapter
3
- # EventMachine adapter is useful for either asynchronous requests
4
- # when in EM reactor loop or for making parallel requests in
5
- # synchronous code.
6
- class EMHttp < Faraday::Adapter
7
- module Options
8
- def connection_config(env)
9
- options = {}
10
- configure_proxy(options, env)
11
- configure_timeout(options, env)
12
- configure_socket(options, env)
13
- configure_ssl(options, env)
14
- options
15
- end
16
-
17
- def request_config(env)
18
- options = {
19
- :body => read_body(env),
20
- :head => env[:request_headers],
21
- # :keepalive => true,
22
- # :file => 'path/to/file', # stream data off disk
23
- }
24
- configure_compression(options, env)
25
- options
26
- end
27
-
28
- def read_body(env)
29
- body = env[:body]
30
- body.respond_to?(:read) ? body.read : body
31
- end
32
-
33
- def configure_proxy(options, env)
34
- if proxy = request_options(env)[:proxy]
35
- options[:proxy] = {
36
- :host => proxy[:uri].host,
37
- :port => proxy[:uri].port,
38
- :authorization => [proxy[:user], proxy[:password]]
39
- }
40
- end
41
- end
42
-
43
- def configure_socket(options, env)
44
- if bind = request_options(env)[:bind]
45
- options[:bind] = {
46
- :host => bind[:host],
47
- :port => bind[:port]
48
- }
49
- end
50
- end
51
-
52
- def configure_ssl(options, env)
53
- if env[:url].scheme == 'https' && env[:ssl]
54
- options[:ssl] = {
55
- :cert_chain_file => env[:ssl][:ca_file],
56
- :verify_peer => env[:ssl].fetch(:verify, true)
57
- }
58
- end
59
- end
60
-
61
- def configure_timeout(options, env)
62
- timeout, open_timeout = request_options(env).values_at(:timeout, :open_timeout)
63
- options[:connect_timeout] = options[:inactivity_timeout] = timeout
64
- options[:connect_timeout] = open_timeout if open_timeout
65
- end
66
-
67
- def configure_compression(options, env)
68
- if env[:method] == :get and not options[:head].key? 'accept-encoding'
69
- options[:head]['accept-encoding'] = 'gzip, compressed'
70
- end
71
- end
72
-
73
- def request_options(env)
74
- env[:request]
75
- end
76
- end
77
-
78
- include Options
79
-
80
- dependency 'em-http'
81
-
82
- self.supports_parallel = true
83
-
84
- def self.setup_parallel_manager(options = nil)
85
- Manager.new
86
- end
87
-
88
- def call(env)
89
- super
90
- perform_request env
91
- @app.call env
92
- end
93
-
94
- def perform_request(env)
95
- if parallel?(env)
96
- manager = env[:parallel_manager]
97
- manager.add {
98
- perform_single_request(env).
99
- callback { env[:response].finish(env) }
100
- }
101
- else
102
- unless EventMachine.reactor_running?
103
- error = nil
104
- # start EM, block until request is completed
105
- EventMachine.run do
106
- perform_single_request(env).
107
- callback { EventMachine.stop }.
108
- errback { |client|
109
- error = error_message(client)
110
- EventMachine.stop
111
- }
112
- end
113
- raise_error(error) if error
114
- else
115
- # EM is running: instruct upstream that this is an async request
116
- env[:parallel_manager] = true
117
- perform_single_request(env).
118
- callback { env[:response].finish(env) }.
119
- errback {
120
- # TODO: no way to communicate the error in async mode
121
- raise NotImplementedError
122
- }
123
- end
124
- end
125
- rescue EventMachine::Connectify::CONNECTError => err
126
- if err.message.include?("Proxy Authentication Required")
127
- raise Error::ConnectionFailed, %{407 "Proxy Authentication Required "}
128
- else
129
- raise Error::ConnectionFailed, err
130
- end
131
- rescue => err
132
- if defined?(OpenSSL) && OpenSSL::SSL::SSLError === err
133
- raise Faraday::SSLError, err
134
- else
135
- raise
136
- end
137
- end
138
-
139
- # TODO: reuse the connection to support pipelining
140
- def perform_single_request(env)
141
- req = create_request(env)
142
- req.setup_request(env[:method], request_config(env)).callback { |client|
143
- status = client.response_header.status
144
- reason = client.response_header.http_reason
145
- save_response(env, status, client.response, nil, reason) do |resp_headers|
146
- client.response_header.each do |name, value|
147
- resp_headers[name.to_sym] = value
148
- end
149
- end
150
- }
151
- end
152
-
153
- def create_request(env)
154
- EventMachine::HttpRequest.new(env[:url], connection_config(env).merge(@connection_options))
155
- end
156
-
157
- def error_message(client)
158
- client.error or "request failed"
159
- end
160
-
161
- def raise_error(msg)
162
- errklass = Faraday::Error::ClientError
163
- if msg == Errno::ETIMEDOUT
164
- errklass = Faraday::Error::TimeoutError
165
- msg = "request timed out"
166
- elsif msg == Errno::ECONNREFUSED
167
- errklass = Faraday::Error::ConnectionFailed
168
- msg = "connection refused"
169
- elsif msg == "connection closed by server"
170
- errklass = Faraday::Error::ConnectionFailed
171
- end
172
- raise errklass, msg
173
- end
174
-
175
- def parallel?(env)
176
- !!env[:parallel_manager]
177
- end
178
-
179
- # The parallel manager is designed to start an EventMachine loop
180
- # and block until all registered requests have been completed.
181
- class Manager
182
- def initialize
183
- reset
184
- end
185
-
186
- def reset
187
- @registered_procs = []
188
- @num_registered = 0
189
- @num_succeeded = 0
190
- @errors = []
191
- @running = false
192
- end
193
-
194
- def running?() @running end
195
-
196
- def add
197
- if running?
198
- perform_request { yield }
199
- else
200
- @registered_procs << Proc.new
201
- end
202
- @num_registered += 1
203
- end
204
-
205
- def run
206
- if @num_registered > 0
207
- @running = true
208
- EventMachine.run do
209
- @registered_procs.each do |proc|
210
- perform_request(&proc)
211
- end
212
- end
213
- if @errors.size > 0
214
- raise Faraday::Error::ClientError, @errors.first || "connection failed"
215
- end
216
- end
217
- ensure
218
- reset
219
- end
220
-
221
- def perform_request
222
- client = yield
223
- client.callback { @num_succeeded += 1; check_finished }
224
- client.errback { @errors << client.error; check_finished }
225
- end
226
-
227
- def check_finished
228
- if @num_succeeded + @errors.size == @num_registered
229
- EventMachine.stop
230
- end
231
- end
232
- end
233
- end
234
- end
235
- end
236
-
237
- begin
238
- require 'openssl'
239
- rescue LoadError
240
- warn "Warning: no such file to load -- openssl. Make sure it is installed if you want HTTPS support"
241
- else
242
- require 'faraday/adapter/em_http_ssl_patch'
243
- end if Faraday::Adapter::EMHttp.loaded?
@@ -1,56 +0,0 @@
1
- require 'openssl'
2
- require 'em-http'
3
-
4
- module EmHttpSslPatch
5
- def ssl_verify_peer(cert_string)
6
- cert = nil
7
- begin
8
- cert = OpenSSL::X509::Certificate.new(cert_string)
9
- rescue OpenSSL::X509::CertificateError
10
- return false
11
- end
12
-
13
- @last_seen_cert = cert
14
-
15
- if certificate_store.verify(@last_seen_cert)
16
- begin
17
- certificate_store.add_cert(@last_seen_cert)
18
- rescue OpenSSL::X509::StoreError => e
19
- raise e unless e.message == 'cert already in hash table'
20
- end
21
- true
22
- else
23
- raise OpenSSL::SSL::SSLError.new(%(unable to verify the server certificate for "#{host}"))
24
- end
25
- end
26
-
27
- def ssl_handshake_completed
28
- return true unless verify_peer?
29
-
30
- unless OpenSSL::SSL.verify_certificate_identity(@last_seen_cert, host)
31
- raise OpenSSL::SSL::SSLError.new(%(host "#{host}" does not match the server certificate))
32
- else
33
- true
34
- end
35
- end
36
-
37
- def verify_peer?
38
- parent.connopts.tls[:verify_peer]
39
- end
40
-
41
- def host
42
- parent.uri.host
43
- end
44
-
45
- def certificate_store
46
- @certificate_store ||= begin
47
- store = OpenSSL::X509::Store.new
48
- store.set_default_paths
49
- ca_file = parent.connopts.tls[:cert_chain_file]
50
- store.add_file(ca_file) if ca_file
51
- store
52
- end
53
- end
54
- end
55
-
56
- EventMachine::HttpStubConnection.send(:include, EmHttpSslPatch)
@@ -1,66 +0,0 @@
1
- module Faraday
2
- class Adapter
3
- class EMSynchrony < Faraday::Adapter
4
- class ParallelManager
5
-
6
- # Add requests to queue. The `request` argument should be a
7
- # `EM::HttpRequest` object.
8
- def add(request, method, *args, &block)
9
- queue << {
10
- :request => request,
11
- :method => method,
12
- :args => args,
13
- :block => block
14
- }
15
- end
16
-
17
- # Run all requests on queue with `EM::Synchrony::Multi`, wrapping
18
- # it in a reactor and fiber if needed.
19
- def run
20
- result = nil
21
- if !EM.reactor_running?
22
- EM.run {
23
- Fiber.new do
24
- result = perform
25
- EM.stop
26
- end.resume
27
- }
28
- else
29
- result = perform
30
- end
31
- result
32
- end
33
-
34
-
35
- private
36
-
37
- # The request queue.
38
- def queue
39
- @queue ||= []
40
- end
41
-
42
- # Main `EM::Synchrony::Multi` performer.
43
- def perform
44
- multi = ::EM::Synchrony::Multi.new
45
-
46
- queue.each do |item|
47
- method = "a#{item[:method]}".to_sym
48
-
49
- req = item[:request].send(method, *item[:args])
50
- req.callback(&item[:block])
51
-
52
- req_name = "req_#{multi.requests.size}".to_sym
53
- multi.add(req_name, req)
54
- end
55
-
56
- # Clear the queue, so parallel manager objects can be reused.
57
- @queue = []
58
-
59
- # Block fiber until all requests have returned.
60
- multi.perform
61
- end
62
-
63
- end # ParallelManager
64
- end # EMSynchrony
65
- end # Adapter
66
- end # Faraday