BigW Consortium Gitlab

client.rb 1.36 KB
Newer Older
1 2
module Github
  class Client
3 4
    TIMEOUT = 60

5
    attr_reader :connection, :rate_limit
6

7
    def initialize(options)
8
      @connection = Faraday.new(url: options.fetch(:url, root_endpoint)) do |faraday|
9 10
        faraday.options.open_timeout = options.fetch(:timeout, TIMEOUT)
        faraday.options.timeout = options.fetch(:timeout, TIMEOUT)
11
        faraday.authorization 'token', options.fetch(:token)
12
        faraday.adapter :net_http
13
        faraday.ssl.verify = verify_ssl
14
      end
15 16

      @rate_limit = RateLimit.new(connection)
17 18 19
    end

    def get(url, query = {})
20 21
      exceed, reset_in = rate_limit.get
      sleep reset_in if exceed
22

23
      Github::Response.new(connection.get(url, query))
24
    end
25 26 27 28

    private

    def root_endpoint
29
      custom_endpoint || github_endpoint
30 31 32
    end

    def custom_endpoint
33 34 35 36 37 38 39
      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)
40 41 42 43 44
    end

    def github_endpoint
      OmniAuth::Strategies::GitHub.default_options[:client_options][:site]
    end
45 46 47 48 49 50 51

    def github_omniauth_provider
      @github_omniauth_provider ||=
        Gitlab.config.omniauth.providers
              .find { |provider| provider.name == 'github' }
              .to_h
    end
52 53
  end
end