class Faraday::Connection
Experimental RBS support (using type sampling data from the type_fusion
project).
# sig/faraday/connection.rbs class Faraday::Connection def initialize_proxy: (String url, Faraday::ConnectionOptions options) -> untyped end
# => #<Faraday::Response>
conn.get ‘nigiri’
# GET httpbingo.org/nigiri<br><br>conn = Faraday::Connection.new ‘httpbingo.org’
@example
stack for fulfilling an HTTP request.
Connection objects manage the default properties and the middleware
def build_exclusive_url(url = nil, params = nil, params_encoder = nil)
-
(URI)
-
Parameters:
-
params
(Faraday::Utils::ParamsHash
) -- A Faraday::Utils::ParamsHash to -
url
(String, URI, nil
) --
def build_exclusive_url(url = nil, params = nil, params_encoder = nil) url = nil if url.respond_to?(:empty?) && url.empty? base = url_prefix.dup if url && !base.path.end_with?('/') base.path = "#{base.path}/" # ensure trailing slash end url = url.to_s.gsub(':', '%3A') if URI.parse(url.to_s).opaque uri = url ? base + url : base if params uri.query = params.to_query(params_encoder || options.params_encoder) end uri.query = nil if uri.query && uri.query.empty? uri end
def build_request(method)
-
(Faraday::Request)
-
Other tags:
- Yield: - if block given
Parameters:
-
method
(Symbol
) --
def build_request(method) Request.create(method) do |req| req.params = params.dup req.headers = headers.dup req.options = options.dup yield(req) if block_given? end end
def build_url(url = nil, extra_params = nil)
-
extra_params
(Hash
) -- -
url
(String, URI, nil
) --
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 = if query_values.empty? nil else query_values.to_query(options.params_encoder) end uri end
def close
persistent connections, this closes all currently open connections
Closes the underlying resources and/or connections. In the case of
def close app.close end
def default_parallel_manager
- Api: - private
Returns:
-
(Object, nil)
- a parallel manager or nil if yielded
Other tags:
- Yield: - if the adapter isn't parallel-capable, or if no adapter is set yet.
def default_parallel_manager @default_parallel_manager ||= begin adapter = @builder.adapter.klass if @builder.adapter if support_parallel?(adapter) adapter.setup_parallel_manager elsif block_given? yield end end end
def dup
-
(Faraday::Connection)
-
Other tags:
- Api: - private
def dup self.class.new(build_exclusive_url, headers: headers.dup, params: params.dup, builder: builder.dup, ssl: ssl.dup, request: options.dup) end
def find_default_proxy
def find_default_proxy uri = ENV.fetch('http_proxy', nil) return unless uri && !uri.empty? uri = "http://#{uri}" unless uri.match?(/^http/i) uri end
def headers=(hash)
-
hash
(Hash
) --
def headers=(hash) @headers.replace hash end
def in_parallel(manager = nil)
-
(void)
-
Other tags:
- Yield: - a block to execute multiple requests.
Parameters:
-
manager
(Object
) -- The parallel manager that this Connection's
def in_parallel(manager = nil) @parallel_manager = manager || default_parallel_manager do warn 'Warning: `in_parallel` called but no parallel-capable adapter ' \ 'on Faraday stack' warn caller[2, 10].join("\n") nil end yield @parallel_manager&.run ensure @parallel_manager = nil end
def in_parallel?
-
(Boolean)
-
def in_parallel? !!@parallel_manager end
def initialize(url = nil, options = nil)
- Yield: - after all setup has been done
Options Hash:
(**options)
-
:proxy[:password]
(String
) -- -
:proxy[:user]
(String
) -- -
:proxy[:uri]
(URI, String
) -- -
:proxy
(Hash, URI, String
) -- proxy options, either as a URL -
:ssl
(Hash
) -- Hash of SSL options. -
:request
(Hash
) -- Hash of request options. -
:headers
(Hash
) -- Hash of unencoded HTTPString> -
:params
(Hash
) -- URI query unencodedString> -
:url
(URI, String
) -- URI or String base URL
Parameters:
-
options
(Hash, Faraday::ConnectionOptions
) -- -
url
(URI, String
) -- URI or String base URL to use as a prefix for all
def initialize(url = nil, options = nil) options = ConnectionOptions.from(options) if url.is_a?(Hash) || url.is_a?(ConnectionOptions) options = Utils.deep_merge(options, url) url = options.url end @parallel_manager = nil @headers = Utils::Headers.new @params = Utils::ParamsHash.new @options = options.request @ssl = options.ssl @default_parallel_manager = options.parallel_manager @manual_proxy = nil @builder = options.builder || begin # pass an empty block to Builder so it doesn't assume default middleware options.new_builder(block_given? ? proc { |b| } : nil) end self.url_prefix = url || 'http:/' @params.update(options.params) if options.params @headers.update(options.headers) if options.headers initialize_proxy(url, options) yield(self) if block_given? @headers[:user_agent] ||= USER_AGENT end
def initialize_proxy(url, options)
Experimental RBS support (using type sampling data from the type_fusion
project).
def initialize_proxy: (String url, Faraday::ConnectionOptions options) -> untyped
This signature was generated using 1 sample from 1 application.
def initialize_proxy(url, options) @manual_proxy = !!options.proxy @proxy = if options.proxy ProxyOptions.from(options.proxy) else proxy_from_env(url) end end
def options(*args)
-
(Faraday::Response)
-
Other tags:
- Yield: - for further request customizations
Parameters:
-
headers
(Hash, nil
) -- unencoded HTTP header key/value pairs. -
params
(Hash, nil
) -- Hash of URI query unencoded key/value pairs. -
url
(String, URI, nil
) -- String base URL to sue as a prefix for all requests.
Overloads:
-
options(url, params = nil, headers = nil)
-
options()
def options(*args) return @options if args.empty? url, params, headers = *args run_request(:options, url, nil, headers) do |request| request.params.update(params) if params yield request if block_given? end end
def params=(hash)
-
hash
(Hash
) --
def params=(hash) @params.replace hash end
def path_prefix=(value)
-
(String)
- the new path prefix
Parameters:
-
value
(String
) --
def path_prefix=(value) url_prefix.path = if value value = "/#{value}" unless value[0, 1] == '/' value end end
def proxy=(new_value)
-
new_value
(Object
) --
def proxy=(new_value) @manual_proxy = true @proxy = new_value ? ProxyOptions.from(new_value) : nil end
def proxy_for_request(url)
def proxy_for_request(url) return proxy if @manual_proxy if url && Utils.URI(url).absolute? proxy_from_env(url) else proxy end end
def proxy_from_env(url)
def proxy_from_env(url) return if Faraday.ignore_env_proxy uri = nil if URI.parse('').respond_to?(:find_proxy) case url when String uri = Utils.URI(url) uri = if uri.host.nil? find_default_proxy else URI.parse("#{uri.scheme}://#{uri.host}").find_proxy end when URI uri = url.find_proxy when nil uri = find_default_proxy end else warn 'no_proxy is unsupported' if ENV['no_proxy'] || ENV['NO_PROXY'] uri = find_default_proxy end ProxyOptions.from(uri) if uri end
def run_request(method, url, body, headers)
-
(Faraday::Response)
-
Parameters:
-
headers
(Hash, nil
) -- unencoded HTTP header key/value pairs. -
body
(String, nil
) -- The request body that will eventually be converted to -
url
(String, URI, nil
) -- String or URI to access. -
method
(Symbol
) -- HTTP method.
def run_request(method, url, body, headers) unless METHODS.include?(method) raise ArgumentError, "unknown http method: #{method}" end request = build_request(method) do |req| req.options.proxy = proxy_for_request(url) 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
def set_basic_auth(user, password)
def set_basic_auth(user, password) header = Faraday::Utils.basic_header_from(user, password) headers[Faraday::Request::Authorization::KEY] = header end
def support_parallel?(adapter)
def support_parallel?(adapter) adapter.respond_to?(:supports_parallel?) && adapter&.supports_parallel? end
def url_prefix=(url, encoder = nil)
-
encoder
(Object
) -- -
url
(String, URI
) --
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| set_basic_auth(user, password) uri.user = uri.password = nil end @proxy = proxy_from_env(url) unless @manual_proxy end
def with_uri_credentials(uri)
- Api: - private
Returns:
-
(void)
-
Other tags:
- Yieldparam: password - any password from URI
Yieldparam: username - any username from URI
Other tags:
- Yield: - any username and password
Parameters:
-
uri
(URI
) --
def with_uri_credentials(uri) return unless uri.user && uri.password yield(Utils.unescape(uri.user), Utils.unescape(uri.password)) end