Module: Octokit::Connection

Includes:
Authentication
Included in:
Client, EnterpriseAdminClient, EnterpriseManagementConsoleClient
Defined in:
lib/octokit/connection.rb

Overview

Network layer for API clients.

Constant Summary collapse

CONVENIENCE_HEADERS =

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

Set.new(%i[accept content_type])

Constants included from Authentication

Authentication::FARADAY_BASIC_AUTH_KEYS

Instance Method Summary collapse

Methods included from Authentication

#application_authenticated?, #basic_authenticated?, #bearer_authenticated?, #token_authenticated?, #user_authenticated?

Instance Method Details

#agentSawyer::Agent

Hypermedia agent for the GitHub API

Returns:

  • (Sawyer::Agent)


104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# 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.request(*FARADAY_BASIC_AUTH_KEYS, @login, @password)
    elsif token_authenticated?
      http.request :authorization, 'token', @access_token
    elsif bearer_authenticated?
      http.request :authorization, 'Bearer', @bearer_token
    elsif application_authenticated?
      http.request(*FARADAY_BASIC_AUTH_KEYS, @client_id, @client_secret)
    end
  end
end

#delete(url, options = {}) ⇒ Sawyer::Resource

Make a HTTP DELETE request

Parameters:

Returns:

  • (Sawyer::Resource)


54
55
56
# File 'lib/octokit/connection.rb', line 54

def delete(url, options = {})
  request :delete, url, options
end

#get(url, options = {}) ⇒ Sawyer::Resource

Make a HTTP GET request

Parameters:

Returns:

  • (Sawyer::Resource)


18
19
20
# File 'lib/octokit/connection.rb', line 18

def get(url, options = {})
  request :get, url, parse_query_and_convenience_headers(options)
end

#head(url, options = {}) ⇒ Sawyer::Resource

Make a HTTP HEAD request

Parameters:

Returns:

  • (Sawyer::Resource)


63
64
65
# File 'lib/octokit/connection.rb', line 63

def head(url, options = {})
  request :head, url, parse_query_and_convenience_headers(options)
end

#last_responseSawyer::Response

Response for last HTTP request

Returns:

  • (Sawyer::Response)


131
132
133
# File 'lib/octokit/connection.rb', line 131

def last_response
  @last_response if defined? @last_response
end

#paginate(url, options = {}) ⇒ Sawyer::Resource

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

Parameters:

  • url (String)

    The path, relative to Octokit::Configurable#api_endpoint

  • options (Hash) (defaults to: {})

    Query and header params for request

  • 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.

Returns:

  • (Sawyer::Resource)


78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/octokit/connection.rb', line 78

def paginate(url, options = {})
  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 = {}) ⇒ Sawyer::Resource

Make a HTTP PATCH request

Parameters:

Returns:

  • (Sawyer::Resource)


45
46
47
# File 'lib/octokit/connection.rb', line 45

def patch(url, options = {})
  request :patch, url, options
end

#post(url, options = {}) ⇒ Sawyer::Resource

Make a HTTP POST request

Parameters:

Returns:

  • (Sawyer::Resource)


27
28
29
# File 'lib/octokit/connection.rb', line 27

def post(url, options = {})
  request :post, url, options
end

#put(url, options = {}) ⇒ Sawyer::Resource

Make a HTTP PUT request

Parameters:

Returns:

  • (Sawyer::Resource)


36
37
38
# File 'lib/octokit/connection.rb', line 36

def put(url, options = {})
  request :put, url, options
end

#rootSawyer::Resource

Fetch the root resource for the API

Returns:

  • (Sawyer::Resource)


124
125
126
# File 'lib/octokit/connection.rb', line 124

def root
  get '/'
end