- # @return [Faraday::Request] The signed or otherwise authenticated request.
- def generate_authenticated_request(options={})
- return authorization.generate_authenticated_request(options)
- end
-
- ##
- # Transmits the request using the current HTTP adapter.
- #
- # @option options [Array, Faraday::Request] :request
- # The HTTP request to transmit.
- # @option options [String, Symbol] :method
- # The method for the HTTP request.
- # @option options [String, Addressable::URI] :uri
- # The URI for the HTTP request.
- # @option options [Array, Hash] :headers
- # The headers for the HTTP request.
- # @option options [String] :body
- # The body for the HTTP request.
- # @option options [Faraday::Connection] :connection
- # The HTTP connection to use.
- #
- # @return [Faraday::Response] The response from the server.
- def transmit(options={})
- options[:connection] ||= Faraday.default_connection
- if options[:request]
- if options[:request].kind_of?(Array)
- method, uri, headers, body = options[:request]
- elsif options[:request].kind_of?(Faraday::Request)
- unless options[:connection]
- raise ArgumentError,
- "Faraday::Request used, requires a connection to be provided."
- end
- method = options[:request].method.to_s.downcase.to_sym
- uri = options[:connection].build_url(
- options[:request].path, options[:request].params
- )
- headers = options[:request].headers || {}
- body = options[:request].body || ''
- end
- else
- method = options[:method] || :get
- uri = options[:uri]
- headers = options[:headers] || []
- body = options[:body] || ''
- end
- headers = headers.to_a if headers.kind_of?(Hash)
- request_components = {
- :method => method,
- :uri => uri,
- :headers => headers,
- :body => body
- }
- # Verify that we have all pieces required to transmit an HTTP request
- request_components.each do |(key, value)|
- unless value
- raise ArgumentError, "Missing :#{key} parameter."
- end
- end
-
- if self.user_agent != nil
- # If there's no User-Agent header, set one.
- unless headers.kind_of?(Enumerable)
- # We need to use some Enumerable methods, relying on the presence of
- # the #each method.
- class <<headers
- include Enumerable
- end
- end
- if self.user_agent.kind_of?(String)
- unless headers.any? { |k, v| k.downcase == 'User-Agent'.downcase }
- headers = headers.to_a.insert(0, ['User-Agent', self.user_agent])
- end
- elsif self.user_agent != nil
- raise TypeError,
- "Expected User-Agent to be String, got #{self.user_agent.class}"
- end
- end
-
- request = Faraday::Request.create(method.to_s.downcase.to_sym) do |req|
- req.url(Addressable::URI.parse(uri))
- req.headers = Faraday::Utils::Headers.new(headers)
- req.body = body
- end
- request_env = request.to_env(options[:connection])
- response = options[:connection].app.call(request_env)
- return response
- end
-
- ##
- # Executes a request, wrapping it in a Result object.