oso-cloud 1.8.0 → 1.9.1.pre.vendored.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +0 -1
- data/Gemfile +5 -0
- data/Gemfile.lock +31 -12
- data/README.md +1 -1
- data/lib/oso/api.rb +18 -2
- data/lib/oso/oso.rb +10 -7
- data/lib/oso/version.rb +1 -1
- data/vendor/gems/faraday-2.5.2/CHANGELOG.md +574 -0
- data/vendor/gems/faraday-2.5.2/LICENSE.md +20 -0
- data/vendor/gems/faraday-2.5.2/README.md +55 -0
- data/vendor/gems/faraday-2.5.2/Rakefile +7 -0
- data/vendor/gems/faraday-2.5.2/examples/client_spec.rb +119 -0
- data/vendor/gems/faraday-2.5.2/examples/client_test.rb +144 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/adapter/test.rb +298 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/adapter.rb +102 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/adapter_registry.rb +30 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/connection.rb +561 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/encoders/flat_params_encoder.rb +105 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/encoders/nested_params_encoder.rb +183 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/error.rb +147 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/logging/formatter.rb +106 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/methods.rb +6 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/middleware.rb +30 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/middleware_registry.rb +83 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/options/connection_options.rb +22 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/options/env.rb +199 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/options/proxy_options.rb +32 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/options/request_options.rb +22 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/options/ssl_options.rb +69 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/options.rb +218 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/parameters.rb +5 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/rack_builder.rb +252 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/request/authorization.rb +49 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/request/instrumentation.rb +56 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/request/json.rb +55 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/request/url_encoded.rb +60 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/request.rb +136 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/response/json.rb +54 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/response/logger.rb +33 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/response/raise_error.rb +64 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/response.rb +90 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/utils/headers.rb +139 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/utils/params_hash.rb +61 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/utils.rb +122 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday/version.rb +5 -0
- data/vendor/gems/faraday-2.5.2/lib/faraday.rb +157 -0
- data/vendor/gems/faraday-2.5.2/spec/external_adapters/faraday_specs_setup.rb +14 -0
- data/vendor/gems/faraday-2.5.2/spec/faraday/adapter/test_spec.rb +413 -0
- data/vendor/gems/faraday-2.5.2/spec/faraday/adapter_registry_spec.rb +28 -0
- data/vendor/gems/faraday-2.5.2/spec/faraday/adapter_spec.rb +55 -0
- data/vendor/gems/faraday-2.5.2/spec/faraday/connection_spec.rb +793 -0
- data/vendor/gems/faraday-2.5.2/spec/faraday/error_spec.rb +60 -0
- data/vendor/gems/faraday-2.5.2/spec/faraday/middleware_registry_spec.rb +31 -0
- data/vendor/gems/faraday-2.5.2/spec/faraday/middleware_spec.rb +52 -0
- data/vendor/gems/faraday-2.5.2/spec/faraday/options/env_spec.rb +76 -0
- data/vendor/gems/faraday-2.5.2/spec/faraday/options/options_spec.rb +297 -0
- data/vendor/gems/faraday-2.5.2/spec/faraday/options/proxy_options_spec.rb +44 -0
- data/vendor/gems/faraday-2.5.2/spec/faraday/options/request_options_spec.rb +19 -0
- data/vendor/gems/faraday-2.5.2/spec/faraday/params_encoders/flat_spec.rb +42 -0
- data/vendor/gems/faraday-2.5.2/spec/faraday/params_encoders/nested_spec.rb +150 -0
- data/vendor/gems/faraday-2.5.2/spec/faraday/rack_builder_spec.rb +317 -0
- data/vendor/gems/faraday-2.5.2/spec/faraday/request/authorization_spec.rb +83 -0
- data/vendor/gems/faraday-2.5.2/spec/faraday/request/instrumentation_spec.rb +74 -0
- data/vendor/gems/faraday-2.5.2/spec/faraday/request/json_spec.rb +111 -0
- data/vendor/gems/faraday-2.5.2/spec/faraday/request/url_encoded_spec.rb +93 -0
- data/vendor/gems/faraday-2.5.2/spec/faraday/request_spec.rb +110 -0
- data/vendor/gems/faraday-2.5.2/spec/faraday/response/json_spec.rb +117 -0
- data/vendor/gems/faraday-2.5.2/spec/faraday/response/logger_spec.rb +220 -0
- data/vendor/gems/faraday-2.5.2/spec/faraday/response/raise_error_spec.rb +172 -0
- data/vendor/gems/faraday-2.5.2/spec/faraday/response_spec.rb +75 -0
- data/vendor/gems/faraday-2.5.2/spec/faraday/utils/headers_spec.rb +82 -0
- data/vendor/gems/faraday-2.5.2/spec/faraday/utils_spec.rb +118 -0
- data/vendor/gems/faraday-2.5.2/spec/faraday_spec.rb +37 -0
- data/vendor/gems/faraday-2.5.2/spec/spec_helper.rb +132 -0
- data/vendor/gems/faraday-2.5.2/spec/support/disabling_stub.rb +14 -0
- data/vendor/gems/faraday-2.5.2/spec/support/fake_safe_buffer.rb +15 -0
- data/vendor/gems/faraday-2.5.2/spec/support/helper_methods.rb +96 -0
- data/vendor/gems/faraday-2.5.2/spec/support/shared_examples/adapter.rb +105 -0
- data/vendor/gems/faraday-2.5.2/spec/support/shared_examples/params_encoder.rb +18 -0
- data/vendor/gems/faraday-2.5.2/spec/support/shared_examples/request_method.rb +263 -0
- data/vendor/gems/faraday-2.5.2/spec/support/streaming_response_checker.rb +35 -0
- data/vendor/gems/faraday-net_http-3.0.2/LICENSE.md +21 -0
- data/vendor/gems/faraday-net_http-3.0.2/README.md +57 -0
- data/vendor/gems/faraday-net_http-3.0.2/lib/faraday/adapter/net_http.rb +208 -0
- data/vendor/gems/faraday-net_http-3.0.2/lib/faraday/net_http/version.rb +7 -0
- data/vendor/gems/faraday-net_http-3.0.2/lib/faraday/net_http.rb +10 -0
- data/vendor/gems/faraday-net_http_persistent-2.3.0/LICENSE.md +21 -0
- data/vendor/gems/faraday-net_http_persistent-2.3.0/README.md +66 -0
- data/vendor/gems/faraday-net_http_persistent-2.3.0/lib/faraday/adapter/net_http_persistent.rb +234 -0
- data/vendor/gems/faraday-net_http_persistent-2.3.0/lib/faraday/net_http_persistent/version.rb +7 -0
- data/vendor/gems/faraday-net_http_persistent-2.3.0/lib/faraday/net_http_persistent.rb +18 -0
- data/vendor/gems/faraday-retry-2.0.0/CHANGELOG.md +24 -0
- data/vendor/gems/faraday-retry-2.0.0/LICENSE.md +21 -0
- data/vendor/gems/faraday-retry-2.0.0/README.md +169 -0
- data/vendor/gems/faraday-retry-2.0.0/lib/faraday/retriable_response.rb +8 -0
- data/vendor/gems/faraday-retry-2.0.0/lib/faraday/retry/middleware.rb +254 -0
- data/vendor/gems/faraday-retry-2.0.0/lib/faraday/retry/version.rb +7 -0
- data/vendor/gems/faraday-retry-2.0.0/lib/faraday/retry.rb +13 -0
- data/vendor/gems/net-http-persistent-4.0.5/.autotest +9 -0
- data/vendor/gems/net-http-persistent-4.0.5/.gemtest +0 -0
- data/vendor/gems/net-http-persistent-4.0.5/Gemfile +14 -0
- data/vendor/gems/net-http-persistent-4.0.5/History.txt +460 -0
- data/vendor/gems/net-http-persistent-4.0.5/Manifest.txt +13 -0
- data/vendor/gems/net-http-persistent-4.0.5/README.rdoc +82 -0
- data/vendor/gems/net-http-persistent-4.0.5/Rakefile +25 -0
- data/vendor/gems/net-http-persistent-4.0.5/lib/net/http/persistent/connection.rb +41 -0
- data/vendor/gems/net-http-persistent-4.0.5/lib/net/http/persistent/pool.rb +65 -0
- data/vendor/gems/net-http-persistent-4.0.5/lib/net/http/persistent/timed_stack_multi.rb +79 -0
- data/vendor/gems/net-http-persistent-4.0.5/lib/net/http/persistent.rb +1158 -0
- data/vendor/gems/net-http-persistent-4.0.5/test/test_net_http_persistent.rb +1512 -0
- data/vendor/gems/net-http-persistent-4.0.5/test/test_net_http_persistent_timed_stack_multi.rb +151 -0
- metadata +112 -8
@@ -0,0 +1,252 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'ruby2_keywords'
|
4
|
+
require 'faraday/adapter_registry'
|
5
|
+
|
6
|
+
module Faraday
|
7
|
+
# A Builder that processes requests into responses by passing through an inner
|
8
|
+
# middleware stack (heavily inspired by Rack).
|
9
|
+
#
|
10
|
+
# @example
|
11
|
+
# Faraday::Connection.new(url: 'http://httpbingo.org') do |builder|
|
12
|
+
# builder.request :url_encoded # Faraday::Request::UrlEncoded
|
13
|
+
# builder.adapter :net_http # Faraday::Adapter::NetHttp
|
14
|
+
# end
|
15
|
+
class RackBuilder
|
16
|
+
# Used to detect missing arguments
|
17
|
+
NO_ARGUMENT = Object.new
|
18
|
+
|
19
|
+
attr_accessor :handlers
|
20
|
+
|
21
|
+
# Error raised when trying to modify the stack after calling `lock!`
|
22
|
+
class StackLocked < RuntimeError; end
|
23
|
+
|
24
|
+
# borrowed from ActiveSupport::Dependencies::Reference &
|
25
|
+
# ActionDispatch::MiddlewareStack::Middleware
|
26
|
+
class Handler
|
27
|
+
REGISTRY = Faraday::AdapterRegistry.new
|
28
|
+
|
29
|
+
attr_reader :name
|
30
|
+
|
31
|
+
ruby2_keywords def initialize(klass, *args, &block)
|
32
|
+
@name = klass.to_s
|
33
|
+
REGISTRY.set(klass) if klass.respond_to?(:name)
|
34
|
+
@args = args
|
35
|
+
@block = block
|
36
|
+
end
|
37
|
+
|
38
|
+
def klass
|
39
|
+
REGISTRY.get(@name)
|
40
|
+
end
|
41
|
+
|
42
|
+
def inspect
|
43
|
+
@name
|
44
|
+
end
|
45
|
+
|
46
|
+
def ==(other)
|
47
|
+
if other.is_a? Handler
|
48
|
+
name == other.name
|
49
|
+
elsif other.respond_to? :name
|
50
|
+
klass == other
|
51
|
+
else
|
52
|
+
@name == other.to_s
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def build(app = nil)
|
57
|
+
klass.new(app, *@args, &@block)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def initialize(&block)
|
62
|
+
@adapter = nil
|
63
|
+
@handlers = []
|
64
|
+
build(&block)
|
65
|
+
end
|
66
|
+
|
67
|
+
def initialize_dup(original)
|
68
|
+
super
|
69
|
+
@adapter = original.adapter
|
70
|
+
@handlers = original.handlers.dup
|
71
|
+
end
|
72
|
+
|
73
|
+
def build
|
74
|
+
raise_if_locked
|
75
|
+
block_given? ? yield(self) : request(:url_encoded)
|
76
|
+
adapter(Faraday.default_adapter, **Faraday.default_adapter_options) unless @adapter
|
77
|
+
end
|
78
|
+
|
79
|
+
def [](idx)
|
80
|
+
@handlers[idx]
|
81
|
+
end
|
82
|
+
|
83
|
+
# Locks the middleware stack to ensure no further modifications are made.
|
84
|
+
def lock!
|
85
|
+
@handlers.freeze
|
86
|
+
end
|
87
|
+
|
88
|
+
def locked?
|
89
|
+
@handlers.frozen?
|
90
|
+
end
|
91
|
+
|
92
|
+
ruby2_keywords def use(klass, *args, &block)
|
93
|
+
if klass.is_a? Symbol
|
94
|
+
use_symbol(Faraday::Middleware, klass, *args, &block)
|
95
|
+
else
|
96
|
+
raise_if_locked
|
97
|
+
raise_if_adapter(klass)
|
98
|
+
@handlers << self.class::Handler.new(klass, *args, &block)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
ruby2_keywords def request(key, *args, &block)
|
103
|
+
use_symbol(Faraday::Request, key, *args, &block)
|
104
|
+
end
|
105
|
+
|
106
|
+
ruby2_keywords def response(key, *args, &block)
|
107
|
+
use_symbol(Faraday::Response, key, *args, &block)
|
108
|
+
end
|
109
|
+
|
110
|
+
ruby2_keywords def adapter(klass = NO_ARGUMENT, *args, &block)
|
111
|
+
return @adapter if klass == NO_ARGUMENT || klass.nil?
|
112
|
+
|
113
|
+
klass = Faraday::Adapter.lookup_middleware(klass) if klass.is_a?(Symbol)
|
114
|
+
@adapter = self.class::Handler.new(klass, *args, &block)
|
115
|
+
end
|
116
|
+
|
117
|
+
## methods to push onto the various positions in the stack:
|
118
|
+
|
119
|
+
ruby2_keywords def insert(index, *args, &block)
|
120
|
+
raise_if_locked
|
121
|
+
index = assert_index(index)
|
122
|
+
handler = self.class::Handler.new(*args, &block)
|
123
|
+
@handlers.insert(index, handler)
|
124
|
+
end
|
125
|
+
|
126
|
+
alias insert_before insert
|
127
|
+
|
128
|
+
ruby2_keywords def insert_after(index, *args, &block)
|
129
|
+
index = assert_index(index)
|
130
|
+
insert(index + 1, *args, &block)
|
131
|
+
end
|
132
|
+
|
133
|
+
ruby2_keywords def swap(index, *args, &block)
|
134
|
+
raise_if_locked
|
135
|
+
index = assert_index(index)
|
136
|
+
@handlers.delete_at(index)
|
137
|
+
insert(index, *args, &block)
|
138
|
+
end
|
139
|
+
|
140
|
+
def delete(handler)
|
141
|
+
raise_if_locked
|
142
|
+
@handlers.delete(handler)
|
143
|
+
end
|
144
|
+
|
145
|
+
# Processes a Request into a Response by passing it through this Builder's
|
146
|
+
# middleware stack.
|
147
|
+
#
|
148
|
+
# @param connection [Faraday::Connection]
|
149
|
+
# @param request [Faraday::Request]
|
150
|
+
#
|
151
|
+
# @return [Faraday::Response]
|
152
|
+
def build_response(connection, request)
|
153
|
+
app.call(build_env(connection, request))
|
154
|
+
end
|
155
|
+
|
156
|
+
# The "rack app" wrapped in middleware. All requests are sent here.
|
157
|
+
#
|
158
|
+
# The builder is responsible for creating the app object. After this,
|
159
|
+
# the builder gets locked to ensure no further modifications are made
|
160
|
+
# to the middleware stack.
|
161
|
+
#
|
162
|
+
# Returns an object that responds to `call` and returns a Response.
|
163
|
+
def app
|
164
|
+
@app ||= begin
|
165
|
+
lock!
|
166
|
+
ensure_adapter!
|
167
|
+
to_app
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
def to_app
|
172
|
+
# last added handler is the deepest and thus closest to the inner app
|
173
|
+
# adapter is always the last one
|
174
|
+
@handlers.reverse.inject(@adapter.build) do |app, handler|
|
175
|
+
handler.build(app)
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
def ==(other)
|
180
|
+
other.is_a?(self.class) &&
|
181
|
+
@handlers == other.handlers &&
|
182
|
+
@adapter == other.adapter
|
183
|
+
end
|
184
|
+
|
185
|
+
# ENV Keys
|
186
|
+
# :http_method - a symbolized request HTTP method (:get, :post)
|
187
|
+
# :body - the request body that will eventually be converted to a string.
|
188
|
+
# :url - URI instance for the current request.
|
189
|
+
# :status - HTTP response status code
|
190
|
+
# :request_headers - hash of HTTP Headers to be sent to the server
|
191
|
+
# :response_headers - Hash of HTTP headers from the server
|
192
|
+
# :parallel_manager - sent if the connection is in parallel mode
|
193
|
+
# :request - Hash of options for configuring the request.
|
194
|
+
# :timeout - open/read timeout Integer in seconds
|
195
|
+
# :open_timeout - read timeout Integer in seconds
|
196
|
+
# :proxy - Hash of proxy options
|
197
|
+
# :uri - Proxy Server URI
|
198
|
+
# :user - Proxy server username
|
199
|
+
# :password - Proxy server password
|
200
|
+
# :ssl - Hash of options for configuring SSL requests.
|
201
|
+
def build_env(connection, request)
|
202
|
+
exclusive_url = connection.build_exclusive_url(
|
203
|
+
request.path, request.params,
|
204
|
+
request.options.params_encoder
|
205
|
+
)
|
206
|
+
|
207
|
+
Env.new(request.http_method, request.body, exclusive_url,
|
208
|
+
request.options, request.headers, connection.ssl,
|
209
|
+
connection.parallel_manager)
|
210
|
+
end
|
211
|
+
|
212
|
+
private
|
213
|
+
|
214
|
+
LOCK_ERR = "can't modify middleware stack after making a request"
|
215
|
+
MISSING_ADAPTER_ERROR = "An attempt to run a request with a Faraday::Connection without adapter has been made.\n" \
|
216
|
+
"Please set Faraday.default_adapter or provide one when initializing the connection.\n" \
|
217
|
+
'For more info, check https://lostisland.github.io/faraday/usage/.'
|
218
|
+
|
219
|
+
def raise_if_locked
|
220
|
+
raise StackLocked, LOCK_ERR if locked?
|
221
|
+
end
|
222
|
+
|
223
|
+
def raise_if_adapter(klass)
|
224
|
+
return unless is_adapter?(klass)
|
225
|
+
|
226
|
+
raise 'Adapter should be set using the `adapter` method, not `use`'
|
227
|
+
end
|
228
|
+
|
229
|
+
def ensure_adapter!
|
230
|
+
raise MISSING_ADAPTER_ERROR unless @adapter
|
231
|
+
end
|
232
|
+
|
233
|
+
def adapter_set?
|
234
|
+
!@adapter.nil?
|
235
|
+
end
|
236
|
+
|
237
|
+
def is_adapter?(klass) # rubocop:disable Naming/PredicateName
|
238
|
+
klass <= Faraday::Adapter
|
239
|
+
end
|
240
|
+
|
241
|
+
ruby2_keywords def use_symbol(mod, key, *args, &block)
|
242
|
+
use(mod.lookup_middleware(key), *args, &block)
|
243
|
+
end
|
244
|
+
|
245
|
+
def assert_index(index)
|
246
|
+
idx = index.is_a?(Integer) ? index : @handlers.index(index)
|
247
|
+
raise "No such handler: #{index.inspect}" unless idx
|
248
|
+
|
249
|
+
idx
|
250
|
+
end
|
251
|
+
end
|
252
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Faraday
|
4
|
+
class Request
|
5
|
+
# Request middleware for the Authorization HTTP header
|
6
|
+
class Authorization < Faraday::Middleware
|
7
|
+
KEY = 'Authorization'
|
8
|
+
|
9
|
+
# @param app [#call]
|
10
|
+
# @param type [String, Symbol] Type of Authorization
|
11
|
+
# @param params [Array<String, Proc, #call>] parameters to build the Authorization header.
|
12
|
+
# If the type is `:basic`, then these can be a login and password pair.
|
13
|
+
# Otherwise, a single value is expected that will be appended after the type.
|
14
|
+
# This value can be a proc or an object responding to `.call`, in which case
|
15
|
+
# it will be invoked on each request.
|
16
|
+
def initialize(app, type, *params)
|
17
|
+
@type = type
|
18
|
+
@params = params
|
19
|
+
super(app)
|
20
|
+
end
|
21
|
+
|
22
|
+
# @param env [Faraday::Env]
|
23
|
+
def on_request(env)
|
24
|
+
return if env.request_headers[KEY]
|
25
|
+
|
26
|
+
env.request_headers[KEY] = header_from(@type, *@params)
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
# @param type [String, Symbol]
|
32
|
+
# @param params [Array]
|
33
|
+
# @return [String] a header value
|
34
|
+
def header_from(type, *params)
|
35
|
+
if type.to_s.casecmp('basic').zero? && params.size == 2
|
36
|
+
Utils.basic_header_from(*params)
|
37
|
+
elsif params.size != 1
|
38
|
+
raise ArgumentError, "Unexpected params received (got #{params.size} instead of 1)"
|
39
|
+
else
|
40
|
+
value = params.first
|
41
|
+
value = value.call if value.is_a?(Proc) || value.respond_to?(:call)
|
42
|
+
"#{type} #{value}"
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
Faraday::Request.register_middleware(authorization: Faraday::Request::Authorization)
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Faraday
|
4
|
+
class Request
|
5
|
+
# Middleware for instrumenting Requests.
|
6
|
+
class Instrumentation < Faraday::Middleware
|
7
|
+
# Options class used in Request::Instrumentation class.
|
8
|
+
class Options < Faraday::Options.new(:name, :instrumenter)
|
9
|
+
# @return [String]
|
10
|
+
def name
|
11
|
+
self[:name] ||= 'request.faraday'
|
12
|
+
end
|
13
|
+
|
14
|
+
# @return [Class]
|
15
|
+
def instrumenter
|
16
|
+
self[:instrumenter] ||= ActiveSupport::Notifications
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
# Instruments requests using Active Support.
|
21
|
+
#
|
22
|
+
# Measures time spent only for synchronous requests.
|
23
|
+
#
|
24
|
+
# @example Using ActiveSupport::Notifications to measure time spent
|
25
|
+
# for Faraday requests.
|
26
|
+
# ActiveSupport::Notifications
|
27
|
+
# .subscribe('request.faraday') do |name, starts, ends, _, env|
|
28
|
+
# url = env[:url]
|
29
|
+
# http_method = env[:method].to_s.upcase
|
30
|
+
# duration = ends - starts
|
31
|
+
# $stderr.puts '[%s] %s %s (%.3f s)' %
|
32
|
+
# [url.host, http_method, url.request_uri, duration]
|
33
|
+
# end
|
34
|
+
# @param app [#call]
|
35
|
+
# @param options [nil, Hash] Options hash
|
36
|
+
# @option options [String] :name ('request.faraday')
|
37
|
+
# Name of the instrumenter
|
38
|
+
# @option options [Class] :instrumenter (ActiveSupport::Notifications)
|
39
|
+
# Active Support instrumenter class.
|
40
|
+
def initialize(app, options = nil)
|
41
|
+
super(app)
|
42
|
+
@name, @instrumenter = Options.from(options)
|
43
|
+
.values_at(:name, :instrumenter)
|
44
|
+
end
|
45
|
+
|
46
|
+
# @param env [Faraday::Env]
|
47
|
+
def call(env)
|
48
|
+
@instrumenter.instrument(@name, env) do
|
49
|
+
@app.call(env)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
Faraday::Request.register_middleware(instrumentation: Faraday::Request::Instrumentation)
|
@@ -0,0 +1,55 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
module Faraday
|
6
|
+
class Request
|
7
|
+
# Request middleware that encodes the body as JSON.
|
8
|
+
#
|
9
|
+
# Processes only requests with matching Content-type or those without a type.
|
10
|
+
# If a request doesn't have a type but has a body, it sets the Content-type
|
11
|
+
# to JSON MIME-type.
|
12
|
+
#
|
13
|
+
# Doesn't try to encode bodies that already are in string form.
|
14
|
+
class Json < Middleware
|
15
|
+
MIME_TYPE = 'application/json'
|
16
|
+
MIME_TYPE_REGEX = %r{^application/(vnd\..+\+)?json$}.freeze
|
17
|
+
|
18
|
+
def on_request(env)
|
19
|
+
match_content_type(env) do |data|
|
20
|
+
env[:body] = encode(data)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def encode(data)
|
27
|
+
::JSON.generate(data)
|
28
|
+
end
|
29
|
+
|
30
|
+
def match_content_type(env)
|
31
|
+
return unless process_request?(env)
|
32
|
+
|
33
|
+
env[:request_headers][CONTENT_TYPE] ||= MIME_TYPE
|
34
|
+
yield env[:body] unless env[:body].respond_to?(:to_str)
|
35
|
+
end
|
36
|
+
|
37
|
+
def process_request?(env)
|
38
|
+
type = request_type(env)
|
39
|
+
body?(env) && (type.empty? || type.match?(MIME_TYPE_REGEX))
|
40
|
+
end
|
41
|
+
|
42
|
+
def body?(env)
|
43
|
+
(body = env[:body]) && !(body.respond_to?(:to_str) && body.empty?)
|
44
|
+
end
|
45
|
+
|
46
|
+
def request_type(env)
|
47
|
+
type = env[:request_headers][CONTENT_TYPE].to_s
|
48
|
+
type = type.split(';', 2).first if type.index(';')
|
49
|
+
type
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
Faraday::Request.register_middleware(json: Faraday::Request::Json)
|
@@ -0,0 +1,60 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Faraday
|
4
|
+
class Request
|
5
|
+
# Middleware for supporting urlencoded requests.
|
6
|
+
class UrlEncoded < Faraday::Middleware
|
7
|
+
unless defined?(::Faraday::Request::UrlEncoded::CONTENT_TYPE)
|
8
|
+
CONTENT_TYPE = 'Content-Type'
|
9
|
+
end
|
10
|
+
|
11
|
+
class << self
|
12
|
+
attr_accessor :mime_type
|
13
|
+
end
|
14
|
+
self.mime_type = 'application/x-www-form-urlencoded'
|
15
|
+
|
16
|
+
# Encodes as "application/x-www-form-urlencoded" if not already encoded or
|
17
|
+
# of another type.
|
18
|
+
#
|
19
|
+
# @param env [Faraday::Env]
|
20
|
+
def call(env)
|
21
|
+
match_content_type(env) do |data|
|
22
|
+
params = Faraday::Utils::ParamsHash[data]
|
23
|
+
env.body = params.to_query(env.params_encoder)
|
24
|
+
end
|
25
|
+
@app.call env
|
26
|
+
end
|
27
|
+
|
28
|
+
# @param env [Faraday::Env]
|
29
|
+
# @yield [request_body] Body of the request
|
30
|
+
def match_content_type(env)
|
31
|
+
return unless process_request?(env)
|
32
|
+
|
33
|
+
env.request_headers[CONTENT_TYPE] ||= self.class.mime_type
|
34
|
+
return if env.body.respond_to?(:to_str) || env.body.respond_to?(:read)
|
35
|
+
|
36
|
+
yield(env.body)
|
37
|
+
end
|
38
|
+
|
39
|
+
# @param env [Faraday::Env]
|
40
|
+
#
|
41
|
+
# @return [Boolean] True if the request has a body and its Content-Type is
|
42
|
+
# urlencoded.
|
43
|
+
def process_request?(env)
|
44
|
+
type = request_type(env)
|
45
|
+
env.body && (type.empty? || (type == self.class.mime_type))
|
46
|
+
end
|
47
|
+
|
48
|
+
# @param env [Faraday::Env]
|
49
|
+
#
|
50
|
+
# @return [String]
|
51
|
+
def request_type(env)
|
52
|
+
type = env.request_headers[CONTENT_TYPE].to_s
|
53
|
+
type = type.split(';', 2).first if type.index(';')
|
54
|
+
type
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
Faraday::Request.register_middleware(url_encoded: Faraday::Request::UrlEncoded)
|
@@ -0,0 +1,136 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Faraday
|
4
|
+
# Used to setup URLs, params, headers, and the request body in a sane manner.
|
5
|
+
#
|
6
|
+
# @example
|
7
|
+
# @connection.post do |req|
|
8
|
+
# req.url 'http://localhost', 'a' => '1' # 'http://localhost?a=1'
|
9
|
+
# req.headers['b'] = '2' # Header
|
10
|
+
# req.params['c'] = '3' # GET Param
|
11
|
+
# req['b'] = '2' # also Header
|
12
|
+
# req.body = 'abc'
|
13
|
+
# end
|
14
|
+
#
|
15
|
+
# @!attribute http_method
|
16
|
+
# @return [Symbol] the HTTP method of the Request
|
17
|
+
# @!attribute path
|
18
|
+
# @return [URI, String] the path
|
19
|
+
# @!attribute params
|
20
|
+
# @return [Hash] query parameters
|
21
|
+
# @!attribute headers
|
22
|
+
# @return [Faraday::Utils::Headers] headers
|
23
|
+
# @!attribute body
|
24
|
+
# @return [Hash] body
|
25
|
+
# @!attribute options
|
26
|
+
# @return [RequestOptions] options
|
27
|
+
#
|
28
|
+
# rubocop:disable Style/StructInheritance
|
29
|
+
class Request < Struct.new(:http_method, :path, :params, :headers, :body, :options)
|
30
|
+
# rubocop:enable Style/StructInheritance
|
31
|
+
|
32
|
+
extend MiddlewareRegistry
|
33
|
+
|
34
|
+
# @param request_method [String]
|
35
|
+
# @yield [request] for block customization, if block given
|
36
|
+
# @yieldparam request [Request]
|
37
|
+
# @return [Request]
|
38
|
+
def self.create(request_method)
|
39
|
+
new(request_method).tap do |request|
|
40
|
+
yield(request) if block_given?
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
# Replace params, preserving the existing hash type.
|
45
|
+
#
|
46
|
+
# @param hash [Hash] new params
|
47
|
+
def params=(hash)
|
48
|
+
if params
|
49
|
+
params.replace hash
|
50
|
+
else
|
51
|
+
super
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
# Replace request headers, preserving the existing hash type.
|
56
|
+
#
|
57
|
+
# @param hash [Hash] new headers
|
58
|
+
def headers=(hash)
|
59
|
+
if headers
|
60
|
+
headers.replace hash
|
61
|
+
else
|
62
|
+
super
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
# Update path and params.
|
67
|
+
#
|
68
|
+
# @param path [URI, String]
|
69
|
+
# @param params [Hash, nil]
|
70
|
+
# @return [void]
|
71
|
+
def url(path, params = nil)
|
72
|
+
if path.respond_to? :query
|
73
|
+
if (query = path.query)
|
74
|
+
path = path.dup
|
75
|
+
path.query = nil
|
76
|
+
end
|
77
|
+
else
|
78
|
+
anchor_index = path.index('#')
|
79
|
+
path = path.slice(0, anchor_index) unless anchor_index.nil?
|
80
|
+
path, query = path.split('?', 2)
|
81
|
+
end
|
82
|
+
self.path = path
|
83
|
+
self.params.merge_query query, options.params_encoder
|
84
|
+
self.params.update(params) if params
|
85
|
+
end
|
86
|
+
|
87
|
+
# @param key [Object] key to look up in headers
|
88
|
+
# @return [Object] value of the given header name
|
89
|
+
def [](key)
|
90
|
+
headers[key]
|
91
|
+
end
|
92
|
+
|
93
|
+
# @param key [Object] key of header to write
|
94
|
+
# @param value [Object] value of header
|
95
|
+
def []=(key, value)
|
96
|
+
headers[key] = value
|
97
|
+
end
|
98
|
+
|
99
|
+
# Marshal serialization support.
|
100
|
+
#
|
101
|
+
# @return [Hash] the hash ready to be serialized in Marshal.
|
102
|
+
def marshal_dump
|
103
|
+
{
|
104
|
+
http_method: http_method,
|
105
|
+
body: body,
|
106
|
+
headers: headers,
|
107
|
+
path: path,
|
108
|
+
params: params,
|
109
|
+
options: options
|
110
|
+
}
|
111
|
+
end
|
112
|
+
|
113
|
+
# Marshal serialization support.
|
114
|
+
# Restores the instance variables according to the +serialised+.
|
115
|
+
# @param serialised [Hash] the serialised object.
|
116
|
+
def marshal_load(serialised)
|
117
|
+
self.http_method = serialised[:http_method]
|
118
|
+
self.body = serialised[:body]
|
119
|
+
self.headers = serialised[:headers]
|
120
|
+
self.path = serialised[:path]
|
121
|
+
self.params = serialised[:params]
|
122
|
+
self.options = serialised[:options]
|
123
|
+
end
|
124
|
+
|
125
|
+
# @return [Env] the Env for this Request
|
126
|
+
def to_env(connection)
|
127
|
+
Env.new(http_method, body, connection.build_exclusive_url(path, params),
|
128
|
+
options, headers, connection.ssl, connection.parallel_manager)
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
require 'faraday/request/authorization'
|
134
|
+
require 'faraday/request/instrumentation'
|
135
|
+
require 'faraday/request/json'
|
136
|
+
require 'faraday/request/url_encoded'
|
@@ -0,0 +1,54 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
module Faraday
|
6
|
+
class Response
|
7
|
+
# Parse response bodies as JSON.
|
8
|
+
class Json < Middleware
|
9
|
+
def initialize(app = nil, parser_options: nil, content_type: /\bjson$/, preserve_raw: false)
|
10
|
+
super(app)
|
11
|
+
@parser_options = parser_options
|
12
|
+
@content_types = Array(content_type)
|
13
|
+
@preserve_raw = preserve_raw
|
14
|
+
end
|
15
|
+
|
16
|
+
def on_complete(env)
|
17
|
+
process_response(env) if parse_response?(env)
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def process_response(env)
|
23
|
+
env[:raw_body] = env[:body] if @preserve_raw
|
24
|
+
env[:body] = parse(env[:body])
|
25
|
+
rescue StandardError, SyntaxError => e
|
26
|
+
raise Faraday::ParsingError.new(e, env[:response])
|
27
|
+
end
|
28
|
+
|
29
|
+
def parse(body)
|
30
|
+
::JSON.parse(body, @parser_options || {}) unless body.strip.empty?
|
31
|
+
end
|
32
|
+
|
33
|
+
def parse_response?(env)
|
34
|
+
process_response_type?(env) &&
|
35
|
+
env[:body].respond_to?(:to_str)
|
36
|
+
end
|
37
|
+
|
38
|
+
def process_response_type?(env)
|
39
|
+
type = response_type(env)
|
40
|
+
@content_types.empty? || @content_types.any? do |pattern|
|
41
|
+
pattern.is_a?(Regexp) ? type.match?(pattern) : type == pattern
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def response_type(env)
|
46
|
+
type = env[:response_headers][CONTENT_TYPE].to_s
|
47
|
+
type = type.split(';', 2).first if type.index(';')
|
48
|
+
type
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
Faraday::Response.register_middleware(json: Faraday::Response::Json)
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'forwardable'
|
4
|
+
require 'logger'
|
5
|
+
require 'faraday/logging/formatter'
|
6
|
+
|
7
|
+
module Faraday
|
8
|
+
class Response
|
9
|
+
# Logger is a middleware that logs internal events in the HTTP request
|
10
|
+
# lifecycle to a given Logger object. By default, this logs to STDOUT. See
|
11
|
+
# Faraday::Logging::Formatter to see specifically what is logged.
|
12
|
+
class Logger < Middleware
|
13
|
+
def initialize(app, logger = nil, options = {})
|
14
|
+
super(app)
|
15
|
+
logger ||= ::Logger.new($stdout)
|
16
|
+
formatter_class = options.delete(:formatter) || Logging::Formatter
|
17
|
+
@formatter = formatter_class.new(logger: logger, options: options)
|
18
|
+
yield @formatter if block_given?
|
19
|
+
end
|
20
|
+
|
21
|
+
def call(env)
|
22
|
+
@formatter.request(env)
|
23
|
+
super
|
24
|
+
end
|
25
|
+
|
26
|
+
def on_complete(env)
|
27
|
+
@formatter.response(env)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
Faraday::Response.register_middleware(logger: Faraday::Response::Logger)
|