class Faraday::Connection
# => #<Faraday::Response>
conn.get ‘nigiri’
# GET sushi.com/nigiri<br><br>conn = Faraday::Connection.new ‘sushi.com’
Examples
stack for fulfilling an HTTP request.
Public: Connection objects manage the default properties and the middleware
def authorization(type, token)
foo=\"bar\""
# => "Token token=\"abcdef\",
conn.headers['Authorization']
conn.authorization :Token, :token => 'abcdef', :foo => 'bar'
# => "Bearer mF_9.B5f-4.1JqM"
conn.headers['Authorization']
conn.authorization :Bearer, 'mF_9.B5f-4.1JqM'
Examples
a Hash is encoded into comma separated key/value pairs.
token - The String or Hash token. A String value is taken literally, and
type - The String authorization type.
Public: Sets up a custom Authorization header.
def authorization(type, token) set_authorization_header(:authorization, type, token) end
def basic_auth(login, pass)
# => "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="
conn.headers['Authorization']
conn.basic_auth 'Aladdin', 'open sesame'
Examples
pass - The authentication password.
login - The authentication login.
with base64.
Public: Sets up the Authorization header with these credentials, encoded
def basic_auth(login, pass) set_authorization_header(:basic_auth, login, pass) end
def build_exclusive_url(url = nil, params = nil, params_encoder = nil)
of the resulting url (default: nil).
params - A Faraday::Utils::ParamsHash to replace the query values
url - A String or URI-like object
Internal: Build an absolute URL based on url_prefix.
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
def build_request(method)
Creates and configures the request object.
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
def build_url(url = nil, extra_params = nil)
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
conn.path_prefix # => "/api"
conn.scheme # => https
conn.url_prefix = "https://sushi.com/api?token=abc"
conn = Faraday::Connection.new { ... }
set on the connection instance.
Public: Takes a relative url for a request and combines it with the defaults
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
def default_parallel_manager
Yields in case of not found.
parallel-capable adapter.
Internal: Traverse the middleware stack in search of a
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
def dup
Internal: Creates a duplicate of this 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
def find_default_proxy
def find_default_proxy warn 'no_proxy is unsupported' if ENV['no_proxy'] || ENV['NO_PROXY'] uri = ENV['http_proxy'] if uri && !uri.empty? uri = 'http://' + uri if uri !~ /^http/i uri end end
def headers=(hash)
def headers=(hash) @headers.replace hash end
def in_parallel(manager = nil)
Yields a block to execute multiple requests.
manager - The parallel manager that this Connection's Adapter uses.
Public: Sets up the parallel manager to make a set of requests.
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
def in_parallel?
Public: Determine if this Faraday::Connection can make parallel requests.
def in_parallel? !!@parallel_manager end
def initialize(url = nil, options = nil)
:user - String (optional)
:uri - URI or String
(default: "http_proxy" environment variable).
:proxy - URI, String or Hash of HTTP proxy options
:ssl - Hash of SSL options.
:request - Hash of request options.
:headers - Hash of unencoded HTTP header key/value pairs.
:params - Hash of URI query unencoded key/value pairs.
:url - URI or String base URL (default: "http:/").
options - Hash or Faraday::ConnectionOptions.
requests (optional).
url - URI or String base URL to use as a prefix for all
Public: Initializes a new Faraday::Connection.
def initialize(url = nil, options = nil) options = ConnectionOptions.from(options) if url.is_a?(Hash) options = options.merge(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 @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 = nil if URI.parse("").respond_to?(:find_proxy) case url when String uri = URI.parse(url).find_proxy when URI uri = url.find_proxy when nil uri = find_default_proxy end else uri = find_default_proxy end uri }) yield(self) if block_given? @headers[:user_agent] ||= "Faraday v#{VERSION}" end
def params=(hash)
def params=(hash) @params.replace hash end
def path_prefix=(value)
value - A String.
slash.
Public: Sets the path prefix and ensures that it always has a leading
def path_prefix=(value) url_prefix.path = if value value = '/' + value unless value[0,1] == '/' value end end
def proxy(arg = nil)
def proxy(arg = nil) return @proxy if arg.nil? @proxy = ProxyOptions.from(arg) end
def run_request(method, url, body, headers)
headers - Hash of unencoded HTTP header key/value pairs.
body - The request body that will eventually be converted to a string.
url - The String or URI to access.
method - The Symbol HTTP method.
Builds and runs the Faraday::Request.
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
def set_authorization_header(header_type, *args)
def set_authorization_header(header_type, *args) header = Faraday::Request.lookup_middleware(header_type). header(*args) headers[Faraday::Request::Authorization::KEY] = header end
def token_auth(token, options = nil)
foo=\"bar\""
# => "Token token=\"abcdef\",
conn.headers['Authorization']
conn.token_auth 'abcdef', :foo => 'bar'
Examples
options - Optional Hash of extra token options.
token - The String token.
Public: Sets up the Authorization header with the given token.
def token_auth(token, options = nil) set_authorization_header(:token_auth, token, options) end
def url_prefix=(url, encoder = nil)
conn.get("nigiri?page=2") # accesses https://sushi.com/api/nigiri
conn.path_prefix # => "/api"
conn.scheme # => https
conn.url_prefix = "https://sushi.com/api"
conn = Faraday::Connection.new { ... }
Examples
url - A String or URI.
requests made by this connection.
components in this connection. These components serve as defaults for
Public: Parses the giving url with URI and stores the individual
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
def with_uri_credentials(uri)
def with_uri_credentials(uri) if uri.user and uri.password yield(Utils.unescape(uri.user), Utils.unescape(uri.password)) end end