class Octokit::Client

Client for the GitHub API

@see developer.github.com

Constants

CONVENIENCE_HEADERS

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

Public Class Methods

new(options = {}) click to toggle source
# File lib/octokit/client.rb, line 141
def initialize(options = {})
  # Use options passed in, but fall back to module defaults
  #
  # rubocop:disable Style/HashEachMethods
  #
  # This may look like a `.keys.each` which should be replaced with `#each_key`, but
  # this doesn't actually work, since `#keys` is just a method we've defined ourselves.
  # The class doesn't fulfill the whole `Enumerable` contract.
  Octokit::Configurable.keys.each do |key|
    # rubocop:enable Style/HashEachMethods
    value = options[key].nil? ? Octokit.instance_variable_get(:"@#{key}") : options[key]
    instance_variable_set(:"@#{key}", value)
  end

  login_from_netrc unless user_authenticated? || application_authenticated?
end

Public Instance Methods

access_token=(value) click to toggle source

Set OAuth access token for authentication

@param value [String] 40 character GitHub OAuth access token

# File lib/octokit/client.rb, line 226
def access_token=(value)
  reset_agent
  @access_token = value
end
as_app(key = client_id, secret = client_secret) { |app_client| ... } click to toggle source

Duplicate client using client_id and client_secret as Basic Authentication credentials. @example

Octokit.client_id = "foo"
Octokit.client_secret = "bar"

# GET https://api.github.com/?client_id=foo&client_secret=bar
Octokit.get "/"

Octokit.client.as_app do |client|
  # GET https://foo:bar@api.github.com/
  client.get "/"
end
# File lib/octokit/client.rb, line 194
def as_app(key = client_id, secret = client_secret)
  if key.to_s.empty? || secret.to_s.empty?
    raise ApplicationCredentialsRequired, 'client_id and client_secret required'
  end

  app_client = dup
  app_client.client_id = app_client.client_secret = nil
  app_client.login    = key
  app_client.password = secret

  yield app_client if block_given?
end
bearer_token=(value) click to toggle source

Set Bearer Token for authentication

@param value [String] JWT

# File lib/octokit/client.rb, line 234
def bearer_token=(value)
  reset_agent
  @bearer_token = value
end
client_id=(value) click to toggle source

Set OAuth app client_id

@param value [String] 20 character GitHub OAuth app client_id

# File lib/octokit/client.rb, line 242
def client_id=(value)
  reset_agent
  @client_id = value
end
client_secret=(value) click to toggle source

Set OAuth app client_secret

@param value [String] 40 character GitHub OAuth app client_secret

# File lib/octokit/client.rb, line 250
def client_secret=(value)
  reset_agent
  @client_secret = value
end
client_without_redirects(options = {}) click to toggle source
# File lib/octokit/client.rb, line 255
def client_without_redirects(options = {})
  conn_opts = @connection_options
  conn_opts[:url] = @api_endpoint
  conn_opts[:builder] = @middleware.dup if @middleware
  conn_opts[:proxy] = @proxy if @proxy
  conn_opts[:ssl] = { verify_mode: @ssl_verify_mode } if @ssl_verify_mode
  conn = Faraday.new(conn_opts) do |http|
    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
    end
    http.builder.handlers.push(http_cache_middleware) unless http_cache_middleware.nil?
    http.headers['accept'] = options[:accept] if options.key?(:accept)
  end
  conn.builder.delete(Octokit::Middleware::FollowRedirects)

  conn
end
inspect() click to toggle source

Text representation of the client, masking tokens and passwords

@return [String]

Calls superclass method
# File lib/octokit/client.rb, line 161
def inspect
  inspected = super

  # mask password
  inspected.gsub! @password, '*******' if @password
  if @management_console_password
    inspected.gsub! @management_console_password, '*******'
  end
  inspected.gsub! @bearer_token, '********' if @bearer_token
  # Only show last 4 of token, secret
  if @access_token
    inspected.gsub! @access_token, "#{'*' * 36}#{@access_token[36..]}"
  end
  if @client_secret
    inspected.gsub! @client_secret, "#{'*' * 36}#{@client_secret[36..]}"
  end

  inspected
end
login=(value) click to toggle source

Set username for authentication

@param value [String] GitHub username

# File lib/octokit/client.rb, line 210
def login=(value)
  reset_agent
  @login = value
end
password=(value) click to toggle source

Set password for authentication

@param value [String] GitHub password

# File lib/octokit/client.rb, line 218
def password=(value)
  reset_agent
  @password = value
end

Private Instance Methods

user_path(user, path) click to toggle source

convenience method for constructing a user specific path, if the user is logged in

# File lib/octokit/client/users.rb, line 454
def user_path(user, path)
  if user == login && user_authenticated?
    "user/#{path}"
  else
    "#{User.path user}/#{path}"
  end
end