module Octokit::Connection

Network layer for API clients.

Constants

CONVENIENCE_HEADERS

Header keys that can be passed in options hash to {#get},{#head}

Public Instance Methods

agent() click to toggle source

Hypermedia agent for the GitHub API

@return [Sawyer::Agent]

# File lib/octokit/connection.rb, line 104
def agent
  @agent ||= Sawyer::Agent.new(endpoint, sawyer_options) do |http|
    http.headers[:accept] = default_media_type
    http.headers[:content_type] = "application/json"
    http.headers[:user_agent] = user_agent
    if basic_authenticated?
      http.basic_auth(@login, @password)
    elsif token_authenticated?
      http.authorization 'token', @access_token
    elsif bearer_authenticated?
      http.authorization 'Bearer', @bearer_token
    elsif application_authenticated?
      http.basic_auth(@client_id, @client_secret)
    end
  end
end
delete(url, options = {}) click to toggle source

Make a HTTP DELETE request

@param url [String] The path, relative to {#api_endpoint} @param options [Hash] Query and header params for request @return [Sawyer::Resource]

# File lib/octokit/connection.rb, line 54
def delete(url, options = {})
  request :delete, url, options
end
get(url, options = {}) click to toggle source

Make a HTTP GET request

@param url [String] The path, relative to {#api_endpoint} @param options [Hash] Query and header params for request @return [Sawyer::Resource]

# File lib/octokit/connection.rb, line 18
def get(url, options = {})
  request :get, url, parse_query_and_convenience_headers(options)
end
head(url, options = {}) click to toggle source

Make a HTTP HEAD request

@param url [String] The path, relative to {#api_endpoint} @param options [Hash] Query and header params for request @return [Sawyer::Resource]

# File lib/octokit/connection.rb, line 63
def head(url, options = {})
  request :head, url, parse_query_and_convenience_headers(options)
end
last_response() click to toggle source

Response for last HTTP request

@return [Sawyer::Response]

# File lib/octokit/connection.rb, line 131
def last_response
  @last_response if defined? @last_response
end
paginate(url, options = {}) { |data, last_response| ... } click to toggle source

Make one or more HTTP GET requests, optionally fetching the next page of results from URL in Link response header based on value in {#auto_paginate}.

@param url [String] The path, relative to {#api_endpoint} @param options [Hash] Query and header params for request @param block [Block] Block to perform the data concatination of the

multiple requests. The block is called with two parameters, the first
contains the contents of the requests so far and the second parameter
contains the latest response.

@return [Sawyer::Resource]

# File lib/octokit/connection.rb, line 78
def paginate(url, options = {}, &block)
  opts = parse_query_and_convenience_headers(options)
  if @auto_paginate || @per_page
    opts[:query][:per_page] ||=  @per_page || (@auto_paginate ? 100 : nil)
  end

  data = request(:get, url, opts.dup)

  if @auto_paginate
    while @last_response.rels[:next] && rate_limit.remaining > 0
      @last_response = @last_response.rels[:next].get(:headers => opts[:headers])
      if block_given?
        yield(data, @last_response)
      else
        data.concat(@last_response.data) if @last_response.data.is_a?(Array)
      end
    end

  end

  data
end
patch(url, options = {}) click to toggle source

Make a HTTP PATCH request

@param url [String] The path, relative to {#api_endpoint} @param options [Hash] Body and header params for request @return [Sawyer::Resource]

# File lib/octokit/connection.rb, line 45
def patch(url, options = {})
  request :patch, url, options
end
post(url, options = {}) click to toggle source

Make a HTTP POST request

@param url [String] The path, relative to {#api_endpoint} @param options [Hash] Body and header params for request @return [Sawyer::Resource]

# File lib/octokit/connection.rb, line 27
def post(url, options = {})
  request :post, url, options
end
put(url, options = {}) click to toggle source

Make a HTTP PUT request

@param url [String] The path, relative to {#api_endpoint} @param options [Hash] Body and header params for request @return [Sawyer::Resource]

# File lib/octokit/connection.rb, line 36
def put(url, options = {})
  request :put, url, options
end
root() click to toggle source

Fetch the root resource for the API

@return [Sawyer::Resource]

# File lib/octokit/connection.rb, line 124
def root
  get "/"
end

Protected Instance Methods

endpoint() click to toggle source
# File lib/octokit/connection.rb, line 137
def endpoint
  api_endpoint
end

Private Instance Methods

boolean_from_response(method, path, options = {}) click to toggle source

Executes the request, checking if it was successful

@return [Boolean] True on success, false otherwise

# File lib/octokit/connection.rb, line 163
def boolean_from_response(method, path, options = {})
  request(method, path, options)
  @last_response.status == 204
rescue Octokit::NotFound
  false
end
parse_query_and_convenience_headers(options) click to toggle source
# File lib/octokit/connection.rb, line 192
def parse_query_and_convenience_headers(options)
  options = options.dup
  headers = options.delete(:headers) { Hash.new }
  CONVENIENCE_HEADERS.each do |h|
    if header = options.delete(h)
      headers[h] = header
    end
  end
  query = options.delete(:query)
  opts = {:query => options}
  opts[:query].merge!(query) if query && query.is_a?(Hash)
  opts[:headers] = headers unless headers.empty?

  opts
end
request(method, path, data, options = {}) click to toggle source
# File lib/octokit/connection.rb, line 147
def request(method, path, data, options = {})
  if data.is_a?(Hash)
    options[:query]   = data.delete(:query) || {}
    options[:headers] = data.delete(:headers) || {}
    if accept = data.delete(:accept)
      options[:headers][:accept] = accept
    end
  end

  @last_response = response = agent.call(method, Addressable::URI.parse(path.to_s).normalize.to_s, data, options)
  response.data
end
reset_agent() click to toggle source
# File lib/octokit/connection.rb, line 143
def reset_agent
  @agent = nil
end
sawyer_options() click to toggle source
# File lib/octokit/connection.rb, line 171
def sawyer_options
  opts = {
    :links_parser => Sawyer::LinkParsers::Simple.new
  }
  conn_opts = @connection_options
  conn_opts[:builder] = @middleware if @middleware
  conn_opts[:proxy] = @proxy if @proxy
  if conn_opts[:ssl].nil?
    conn_opts[:ssl] = { :verify_mode => @ssl_verify_mode } if @ssl_verify_mode
  else 
    if @connection_options[:ssl][:verify] == false
      conn_opts[:ssl] = { :verify_mode => 0}
    else
      conn_opts[:ssl] = { :verify_mode => @ssl_verify_mode }
    end
  end
  opts[:faraday] = Faraday.new(conn_opts)

  opts
end