/usr/lib/ruby/vendor_ruby/faraday/connection.rb is in ruby-faraday 0.9.2-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 | module Faraday
# Public: Connection objects manage the default properties and the middleware
# stack for fulfilling an HTTP request.
#
# Examples
#
# conn = Faraday::Connection.new 'http://sushi.com'
#
# # GET http://sushi.com/nigiri
# conn.get 'nigiri'
# # => #<Faraday::Response>
#
class Connection
# A Set of allowed HTTP verbs.
METHODS = Set.new [:get, :post, :put, :delete, :head, :patch, :options]
# Public: Returns a Hash of URI query unencoded key/value pairs.
attr_reader :params
# Public: Returns a Hash of unencoded HTTP header key/value pairs.
attr_reader :headers
# Public: Returns a URI with the prefix used for all requests from this
# Connection. This includes a default host name, scheme, port, and path.
attr_reader :url_prefix
# Public: Returns the Faraday::Builder for this Connection.
attr_reader :builder
# Public: Returns a Hash of the request options.
attr_reader :options
# Public: Returns a Hash of the SSL options.
attr_reader :ssl
# Public: Returns the parallel manager for this Connection.
attr_reader :parallel_manager
# Public: Sets the default parallel manager for this connection.
attr_writer :default_parallel_manager
# Public: Initializes a new Faraday::Connection.
#
# url - URI or String base URL to use as a prefix for all
# requests (optional).
# options - Hash or Faraday::ConnectionOptions.
# :url - URI or String base URL (default: "http:/").
# :params - Hash of URI query unencoded key/value pairs.
# :headers - Hash of unencoded HTTP header key/value pairs.
# :request - Hash of request options.
# :ssl - Hash of SSL options.
# :proxy - URI, String or Hash of HTTP proxy options
# (default: "http_proxy" environment variable).
# :uri - URI or String
# :user - String (optional)
# :password - String (optional)
def initialize(url = nil, options = nil)
if url.is_a?(Hash)
options = ConnectionOptions.from(url)
url = options.url
else
options = ConnectionOptions.from(options)
end
@parallel_manager = nil
@headers = Utils::Headers.new
@params = Utils::ParamsHash.new
@options = options.request
@ssl = options.ssl
@default_parallel_manager = options.parallel_manager
@builder = options.builder || begin
# pass an empty block to Builder so it doesn't assume default middleware
options.new_builder(block_given? ? Proc.new { |b| } : nil)
end
self.url_prefix = url || 'http:/'
@params.update(options.params) if options.params
@headers.update(options.headers) if options.headers
@proxy = nil
proxy(options.fetch(:proxy) {
uri = ENV['http_proxy']
if uri && !uri.empty?
uri = 'http://' + uri if uri !~ /^http/i
uri
end
})
yield(self) if block_given?
@headers[:user_agent] ||= "Faraday v#{VERSION}"
end
# Public: Sets the Hash of URI query unencoded key/value pairs.
def params=(hash)
@params.replace hash
end
# Public: Sets the Hash of unencoded HTTP header key/value pairs.
def headers=(hash)
@headers.replace hash
end
extend Forwardable
def_delegators :builder, :build, :use, :request, :response, :adapter, :app
# Public: Makes an HTTP request without a body.
#
# url - The optional String base URL to use as a prefix for all
# requests. Can also be the options Hash.
# params - Hash of URI query unencoded key/value pairs.
# headers - Hash of unencoded HTTP header key/value pairs.
#
# Examples
#
# conn.get '/items', {:page => 1}, :accept => 'application/json'
# conn.head '/items/1'
#
# # ElasticSearch example sending a body with GET.
# conn.get '/twitter/tweet/_search' do |req|
# req.headers[:content_type] = 'application/json'
# req.params[:routing] = 'kimchy'
# req.body = JSON.generate(:query => {...})
# end
#
# Yields a Faraday::Response for further request customizations.
# Returns a Faraday::Response.
#
# Signature
#
# <verb>(url = nil, params = nil, headers = nil)
#
# verb - An HTTP verb: get, head, or delete.
%w[get head delete].each do |method|
class_eval <<-RUBY, __FILE__, __LINE__ + 1
def #{method}(url = nil, params = nil, headers = nil)
run_request(:#{method}, url, nil, headers) { |request|
request.params.update(params) if params
yield(request) if block_given?
}
end
RUBY
end
# Public: Makes an HTTP request with a body.
#
# url - The optional String base URL to use as a prefix for all
# requests. Can also be the options Hash.
# body - The String body for the request.
# headers - Hash of unencoded HTTP header key/value pairs.
#
# Examples
#
# conn.post '/items', data, :content_type => 'application/json'
#
# # Simple ElasticSearch indexing sample.
# conn.post '/twitter/tweet' do |req|
# req.headers[:content_type] = 'application/json'
# req.params[:routing] = 'kimchy'
# req.body = JSON.generate(:user => 'kimchy', ...)
# end
#
# Yields a Faraday::Response for further request customizations.
# Returns a Faraday::Response.
#
# Signature
#
# <verb>(url = nil, body = nil, headers = nil)
#
# verb - An HTTP verb: post, put, or patch.
%w[post put patch].each do |method|
class_eval <<-RUBY, __FILE__, __LINE__ + 1
def #{method}(url = nil, body = nil, headers = nil, &block)
run_request(:#{method}, url, body, headers, &block)
end
RUBY
end
# Public: Sets up the Authorization header with these credentials, encoded
# with base64.
#
# login - The authentication login.
# pass - The authentication password.
#
# Examples
#
# conn.basic_auth 'Aladdin', 'open sesame'
# conn.headers['Authorization']
# # => "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="
#
# Returns nothing.
def basic_auth(login, pass)
set_authorization_header(:basic_auth, login, pass)
end
# Public: Sets up the Authorization header with the given token.
#
# token - The String token.
# options - Optional Hash of extra token options.
#
# Examples
#
# conn.token_auth 'abcdef', :foo => 'bar'
# conn.headers['Authorization']
# # => "Token token=\"abcdef\",
# foo=\"bar\""
#
# Returns nothing.
def token_auth(token, options = nil)
set_authorization_header(:token_auth, token, options)
end
# Public: Sets up a custom Authorization header.
#
# type - The String authorization type.
# token - The String or Hash token. A String value is taken literally, and
# a Hash is encoded into comma separated key/value pairs.
#
# Examples
#
# conn.authorization :Bearer, 'mF_9.B5f-4.1JqM'
# conn.headers['Authorization']
# # => "Bearer mF_9.B5f-4.1JqM"
#
# conn.authorization :Token, :token => 'abcdef', :foo => 'bar'
# conn.headers['Authorization']
# # => "Token token=\"abcdef\",
# foo=\"bar\""
#
# Returns nothing.
def authorization(type, token)
set_authorization_header(:authorization, type, token)
end
# Internal: Traverse the middleware stack in search of a
# parallel-capable adapter.
#
# Yields in case of not found.
#
# Returns a parallel manager or nil if not found.
def default_parallel_manager
@default_parallel_manager ||= begin
handler = @builder.handlers.detect do |h|
h.klass.respond_to?(:supports_parallel?) and h.klass.supports_parallel?
end
if handler
handler.klass.setup_parallel_manager
elsif block_given?
yield
end
end
end
# Public: Determine if this Faraday::Connection can make parallel requests.
#
# Returns true or false.
def in_parallel?
!!@parallel_manager
end
# Public: Sets up the parallel manager to make a set of requests.
#
# manager - The parallel manager that this Connection's Adapter uses.
#
# Yields a block to execute multiple requests.
# Returns nothing.
def in_parallel(manager = nil)
@parallel_manager = manager || default_parallel_manager {
warn "Warning: `in_parallel` called but no parallel-capable adapter on Faraday stack"
warn caller[2,10].join("\n")
nil
}
yield
@parallel_manager && @parallel_manager.run
ensure
@parallel_manager = nil
end
# Public: Gets or Sets the Hash proxy options.
def proxy(arg = nil)
return @proxy if arg.nil?
@proxy = ProxyOptions.from(arg)
end
def_delegators :url_prefix, :scheme, :scheme=, :host, :host=, :port, :port=
def_delegator :url_prefix, :path, :path_prefix
# Public: Parses the giving url with URI and stores the individual
# components in this connection. These components serve as defaults for
# requests made by this connection.
#
# url - A String or URI.
#
# Examples
#
# conn = Faraday::Connection.new { ... }
# conn.url_prefix = "https://sushi.com/api"
# conn.scheme # => https
# conn.path_prefix # => "/api"
#
# conn.get("nigiri?page=2") # accesses https://sushi.com/api/nigiri
#
# Returns the parsed URI from teh given input..
def url_prefix=(url, encoder = nil)
uri = @url_prefix = Utils.URI(url)
self.path_prefix = uri.path
params.merge_query(uri.query, encoder)
uri.query = nil
with_uri_credentials(uri) do |user, password|
basic_auth user, password
uri.user = uri.password = nil
end
uri
end
# Public: Sets the path prefix and ensures that it always has a leading
# slash.
#
# value - A String.
#
# Returns the new String path prefix.
def path_prefix=(value)
url_prefix.path = if value
value = '/' + value unless value[0,1] == '/'
value
end
end
# Public: Takes a relative url for a request and combines it with the defaults
# set on the connection instance.
#
# conn = Faraday::Connection.new { ... }
# conn.url_prefix = "https://sushi.com/api?token=abc"
# conn.scheme # => https
# conn.path_prefix # => "/api"
#
# conn.build_url("nigiri?page=2") # => https://sushi.com/api/nigiri?token=abc&page=2
# conn.build_url("nigiri", :page => 2) # => https://sushi.com/api/nigiri?token=abc&page=2
#
def build_url(url = nil, extra_params = nil)
uri = build_exclusive_url(url)
query_values = params.dup.merge_query(uri.query, options.params_encoder)
query_values.update extra_params if extra_params
uri.query = query_values.empty? ? nil : query_values.to_query(options.params_encoder)
uri
end
# Builds and runs the Faraday::Request.
#
# method - The Symbol HTTP method.
# url - The String or URI to access.
# body - The String body
# headers - Hash of unencoded HTTP header key/value pairs.
#
# Returns a Faraday::Response.
def run_request(method, url, body, headers)
if !METHODS.include?(method)
raise ArgumentError, "unknown http method: #{method}"
end
request = build_request(method) do |req|
req.url(url) if url
req.headers.update(headers) if headers
req.body = body if body
yield(req) if block_given?
end
builder.build_response(self, request)
end
# Creates and configures the request object.
#
# Returns the new Request.
def build_request(method)
Request.create(method) do |req|
req.params = self.params.dup
req.headers = self.headers.dup
req.options = self.options.merge(:proxy => self.proxy)
yield(req) if block_given?
end
end
# Internal: Build an absolute URL based on url_prefix.
#
# url - A String or URI-like object
# params - A Faraday::Utils::ParamsHash to replace the query values
# of the resulting url (default: nil).
#
# Returns the resulting URI instance.
def build_exclusive_url(url = nil, params = nil, params_encoder = nil)
url = nil if url.respond_to?(:empty?) and url.empty?
base = url_prefix
if url and base.path and base.path !~ /\/$/
base = base.dup
base.path = base.path + '/' # ensure trailing slash
end
uri = url ? base + url : base
uri.query = params.to_query(params_encoder || options.params_encoder) if params
uri.query = nil if uri.query and uri.query.empty?
uri
end
# Internal: Creates a duplicate of this Faraday::Connection.
#
# Returns a Faraday::Connection.
def dup
self.class.new(build_exclusive_url,
:headers => headers.dup,
:params => params.dup,
:builder => builder.dup,
:ssl => ssl.dup,
:request => options.dup)
end
# Internal: Yields username and password extracted from a URI if they both exist.
def with_uri_credentials(uri)
if uri.user and uri.password
yield(Utils.unescape(uri.user), Utils.unescape(uri.password))
end
end
def set_authorization_header(header_type, *args)
header = Faraday::Request.lookup_middleware(header_type).
header(*args)
headers[Faraday::Request::Authorization::KEY] = header
end
end
end
|