Module: Octokit::Connection
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Included In:
| |
Super Chains via Extension / Inclusion / Inheritance | |
Instance Chain:
self,
Authentication
|
|
Defined in: | lib/octokit/connection.rb |
Overview
Network layer for API clients.
Constant Summary
-
CONVENIENCE_HEADERS =
# File 'lib/octokit/connection.rb', line 11
Set.new(%i[accept content_type])
Authentication
- Included
Instance Attribute Summary
Authentication
- Included
#application_authenticated? | Indicates if the client has OAuth Application client_id and secret credentials to make anonymous requests at a higher rate limit. |
#basic_authenticated? | Indicates if the client was supplied Basic Auth username and password. |
#bearer_authenticated? | Indicates if the client was supplied a bearer token. |
#token_authenticated? | Indicates if the client was supplied an OAuth access token. |
#user_authenticated? | Indicates if the client was supplied an OAuth access token or Basic Auth username and password. |
Instance Method Summary
-
#agent ⇒ Sawyer::Agent
Hypermedia agent for the GitHub API.
-
#delete(url, options = {}) ⇒ Sawyer::Resource
Make a HTTP DELETE request.
-
#get(url, options = {}) ⇒ Sawyer::Resource
Make a HTTP GET request.
-
#head(url, options = {}) ⇒ Sawyer::Resource
Make a HTTP HEAD request.
-
#last_response ⇒ Sawyer::Response
Response
for last HTTP request. -
#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
#auto_paginate
. -
#patch(url, options = {}) ⇒ Sawyer::Resource
Make a HTTP PATCH request.
-
#post(url, options = {}) ⇒ Sawyer::Resource
Make a HTTP POST request.
-
#put(url, options = {}) ⇒ Sawyer::Resource
Make a HTTP PUT request.
-
#root ⇒ Sawyer::Resource
Fetch the root resource for the API.
-
#boolean_from_response(method, path, options = {}) ⇒ Boolean
private
Executes the request, checking if it was successful.
- #parse_query_and_convenience_headers(options) private
- #request(method, path, data, options = {}) private
- #reset_agent private
- #response_data_correctly_encoded(response) private
- #sawyer_options private
Authentication
- Included
Instance Method Details
#agent ⇒ Sawyer::Agent
Hypermedia agent for the GitHub API
# File 'lib/octokit/connection.rb', line 104
def agent @agent ||= Sawyer::Agent.new(endpoint, ) do |http| http.headers[:accept] = default_media_type http.headers[:content_type] = 'application/json' http.headers[:user_agent] = user_agent http_cache_middleware = http.builder.handlers.delete(Faraday::HttpCache) if Faraday.const_defined?(:HttpCache) if basic_authenticated? http.request(*FARADAY_BASIC_AUTH_KEYS, @login, @password) elsif token_authenticated? http.request :, 'token', @access_token elsif bearer_authenticated? http.request :, 'Bearer', @bearer_token elsif application_authenticated? http.request(*FARADAY_BASIC_AUTH_KEYS, @client_id, @client_secret) end http.builder.handlers.push(http_cache_middleware) unless http_cache_middleware.nil? end end
#boolean_from_response(method, path, options = {}) ⇒ Boolean
(private)
Executes the request, checking if it was successful
#delete(url, options = {}) ⇒ Sawyer::Resource
Make a HTTP DELETE request
# File 'lib/octokit/connection.rb', line 54
def delete(url, = {}) request :delete, url, end
#get(url, options = {}) ⇒ Sawyer::Resource
Make a HTTP GET request
# File 'lib/octokit/connection.rb', line 18
def get(url, = {}) request :get, url, parse_query_and_convenience_headers( ) end
#head(url, options = {}) ⇒ Sawyer::Resource
Make a HTTP HEAD request
# File 'lib/octokit/connection.rb', line 63
def head(url, = {}) request :head, url, parse_query_and_convenience_headers( ) end
#last_response ⇒ Sawyer::Response
Response
for last HTTP request
# File 'lib/octokit/connection.rb', line 133
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 #auto_paginate
.
# File 'lib/octokit/connection.rb', line 78
def paginate(url, = {}) opts = parse_query_and_convenience_headers( ) 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
#parse_query_and_convenience_headers(options) (private)
[ GitHub ]# File 'lib/octokit/connection.rb', line 196
def parse_query_and_convenience_headers( ) = .dup headers = .delete(:headers) { {} } CONVENIENCE_HEADERS.each do |h| if header = .delete(h) headers[h] = header end end query = .delete(:query) opts = { query: } opts[:query].merge!(query) if query.is_a?(Hash) opts[:headers] = headers unless headers.empty? opts end
#patch(url, options = {}) ⇒ Sawyer::Resource
Make a HTTP PATCH request
# File 'lib/octokit/connection.rb', line 45
def patch(url, = {}) request :patch, url, end
#post(url, options = {}) ⇒ Sawyer::Resource
Make a HTTP POST request
# File 'lib/octokit/connection.rb', line 27
def post(url, = {}) request :post, url, end
#put(url, options = {}) ⇒ Sawyer::Resource
Make a HTTP PUT request
# File 'lib/octokit/connection.rb', line 36
def put(url, = {}) request :put, url, end
#request(method, path, data, options = {}) (private)
[ GitHub ]# File 'lib/octokit/connection.rb', line 149
def request(method, path, data, = {}) if data.is_a?(Hash) [:query] = data.delete(:query) || {} [:headers] = data.delete(:headers) || {} if accept = data.delete(:accept) [:headers][:accept] = accept end end @last_response = response = agent.call(method, Addressable::URI.parse(path.to_s).normalize.to_s, data, ) response_data_correctly_encoded(response) rescue Octokit::Error => e @last_response = nil raise e end
#reset_agent (private)
[ GitHub ]# File 'lib/octokit/connection.rb', line 145
def reset_agent @agent = nil end
#response_data_correctly_encoded(response) (private)
[ GitHub ]# File 'lib/octokit/connection.rb', line 212
def response_data_correctly_encoded(response) content_type = response.headers.fetch('content-type', '') return response.data unless content_type.include?('charset') && response.data.is_a?(String) reported_encoding = content_type.match(/charset=([^ ]+)/)[1] response.data.force_encoding(reported_encoding) end
#root ⇒ Sawyer::Resource
Fetch the root resource for the API
#sawyer_options (private)
[ GitHub ]# File 'lib/octokit/connection.rb', line 175
def opts = { links_parser: Sawyer::LinkParsers::Simple.new } conn_opts = @connection_options conn_opts[:builder] = @middleware.dup 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 verify = @connection_options[:ssl][:verify] conn_opts[:ssl] = { verify: verify, verify_mode: verify == false ? 0 : @ssl_verify_mode } end opts[:faraday] = Faraday.new(conn_opts) opts end