2017-04-11 21:33:54 -04:00
|
|
|
module Github
|
|
|
|
class Client
|
2017-08-03 16:06:27 -04:00
|
|
|
TIMEOUT = 60
|
2017-09-21 09:30:12 -04:00
|
|
|
DEFAULT_PER_PAGE = 100
|
2017-08-03 16:06:27 -04:00
|
|
|
|
2017-04-24 18:49:17 -04:00
|
|
|
attr_reader :connection, :rate_limit
|
2017-04-11 21:33:54 -04:00
|
|
|
|
2017-04-19 19:04:58 -04:00
|
|
|
def initialize(options)
|
2017-07-17 19:19:22 -04:00
|
|
|
@connection = Faraday.new(url: options.fetch(:url, root_endpoint)) do |faraday|
|
2017-08-03 16:06:27 -04:00
|
|
|
faraday.options.open_timeout = options.fetch(:timeout, TIMEOUT)
|
|
|
|
faraday.options.timeout = options.fetch(:timeout, TIMEOUT)
|
2017-04-19 19:04:58 -04:00
|
|
|
faraday.authorization 'token', options.fetch(:token)
|
2017-04-12 21:44:55 -04:00
|
|
|
faraday.adapter :net_http
|
2017-08-07 13:11:38 -04:00
|
|
|
faraday.ssl.verify = verify_ssl
|
2017-04-11 21:33:54 -04:00
|
|
|
end
|
2017-04-24 18:49:17 -04:00
|
|
|
|
|
|
|
@rate_limit = RateLimit.new(connection)
|
2017-04-11 21:33:54 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def get(url, query = {})
|
2017-04-24 18:49:17 -04:00
|
|
|
exceed, reset_in = rate_limit.get
|
|
|
|
sleep reset_in if exceed
|
2017-04-11 21:33:54 -04:00
|
|
|
|
2017-09-21 09:30:12 -04:00
|
|
|
Github::Response.new(connection.get(url, { per_page: DEFAULT_PER_PAGE }.merge(query)))
|
2017-04-11 21:33:54 -04:00
|
|
|
end
|
2017-07-17 19:19:22 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def root_endpoint
|
2017-07-17 20:18:47 -04:00
|
|
|
custom_endpoint || github_endpoint
|
2017-07-17 19:19:22 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def custom_endpoint
|
2017-08-07 13:11:38 -04:00
|
|
|
github_omniauth_provider.dig('args', 'client_options', 'site')
|
|
|
|
end
|
|
|
|
|
|
|
|
def verify_ssl
|
|
|
|
# If there is no config, we're connecting to github.com
|
|
|
|
# and we should verify ssl.
|
|
|
|
github_omniauth_provider.fetch('verify_ssl', true)
|
2017-07-17 19:19:22 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def github_endpoint
|
|
|
|
OmniAuth::Strategies::GitHub.default_options[:client_options][:site]
|
|
|
|
end
|
2017-08-07 13:11:38 -04:00
|
|
|
|
|
|
|
def github_omniauth_provider
|
|
|
|
@github_omniauth_provider ||=
|
|
|
|
Gitlab.config.omniauth.providers
|
|
|
|
.find { |provider| provider.name == 'github' }
|
|
|
|
.to_h
|
|
|
|
end
|
2017-04-11 21:33:54 -04:00
|
|
|
end
|
|
|
|
end
|