module Octokit::Connection
def agent
-
(Sawyer::Agent)
-
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 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 :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 http.builder.handlers.push(http_cache_middleware) unless http_cache_middleware.nil? end end
def boolean_from_response(method, path, options = {})
-
(Boolean)
- True on success, false otherwise
def boolean_from_response(method, path, options = {}) request(method, path, options) [201, 202, 204].include? @last_response.status rescue Octokit::NotFound false end
def delete(url, options = {})
-
(Sawyer::Resource)
-
Parameters:
-
options
(Hash
) -- Query and header params for request -
url
(String
) -- The path, relative to {#api_endpoint}
def delete(url, options = {}) request :delete, url, options end
def endpoint
def endpoint api_endpoint end
def get(url, options = {})
-
(Sawyer::Resource)
-
Parameters:
-
options
(Hash
) -- Query and header params for request -
url
(String
) -- The path, relative to {#api_endpoint}
def get(url, options = {}) request :get, url, parse_query_and_convenience_headers(options) end
def head(url, options = {})
-
(Sawyer::Resource)
-
Parameters:
-
options
(Hash
) -- Query and header params for request -
url
(String
) -- The path, relative to {#api_endpoint}
def head(url, options = {}) request :head, url, parse_query_and_convenience_headers(options) end
def last_response
-
(Sawyer::Response)
-
def last_response @last_response if defined? @last_response end
def paginate(url, options = {})
-
(Sawyer::Resource)
-
Parameters:
-
block
(Block
) -- Block to perform the data concatination of the -
options
(Hash
) -- Query and header params for request -
url
(String
) -- The path, relative to {#api_endpoint}
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
def parse_query_and_convenience_headers(options)
def parse_query_and_convenience_headers(options) options = options.dup headers = options.delete(:headers) { {} } 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.is_a?(Hash) opts[:headers] = headers unless headers.empty? opts end
def patch(url, options = {})
-
(Sawyer::Resource)
-
Parameters:
-
options
(Hash
) -- Body and header params for request -
url
(String
) -- The path, relative to {#api_endpoint}
def patch(url, options = {}) request :patch, url, options end
def post(url, options = {})
-
(Sawyer::Resource)
-
Parameters:
-
options
(Hash
) -- Body and header params for request -
url
(String
) -- The path, relative to {#api_endpoint}
def post(url, options = {}) request :post, url, options end
def put(url, options = {})
-
(Sawyer::Resource)
-
Parameters:
-
options
(Hash
) -- Body and header params for request -
url
(String
) -- The path, relative to {#api_endpoint}
def put(url, options = {}) request :put, url, options end
def request(method, path, data, options = {})
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_correctly_encoded(response) rescue Octokit::Error => e @last_response = nil raise e end
def reset_agent
def reset_agent @agent = nil end
def response_data_correctly_encoded(response)
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
def root
-
(Sawyer::Resource)
-
def root get '/' end
def sawyer_options
def sawyer_options 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