Skip to content
Snippets Groups Projects
Select Git revision
  • move-gl-dropdown
  • improve-table-pagination-spec
  • move-markdown-preview
  • winh-fix-merge-request-spec
  • master default
  • index-namespaces-lower-name
  • winh-single-karma-test
  • 10-3-stable
  • 36782-replace-team-user-role-with-add_role-user-in-specs
  • winh-modal-internal-state
  • tz-ide-file-icons
  • 38869-milestone-select
  • update-autodevops-template
  • jivl-activate-repo-cookie-preferences
  • qa-add-deploy-key
  • docs-move-article-ldap
  • 40780-choose-file
  • 22643-manual-job-page
  • refactor-cluster-show-page-conservative
  • dm-sidekiq-versioning
  • v10.4.0.pre
  • v10.3.0
  • v10.3.0-rc5
  • v10.3.0-rc4
  • v10.3.0-rc3
  • v10.3.0-rc2
  • v10.2.5
  • v10.3.0-rc1
  • v10.0.7
  • v10.1.5
  • v10.2.4
  • v10.2.3
  • v10.2.2
  • v10.2.1
  • v10.3.0.pre
  • v10.2.0
  • v10.2.0-rc4
  • v10.2.0-rc3
  • v10.1.4
  • v10.2.0-rc2
40 results

connection.rb

Forked from GitLab.org / GitLab FOSS
5912 commits behind the upstream repository.
connection.rb 1.79 KiB
module Bitbucket
  class Connection
    DEFAULT_API_VERSION = '2.0'.freeze
    DEFAULT_BASE_URI    = 'https://api.bitbucket.org/'.freeze
    DEFAULT_QUERY       = {}.freeze

    attr_reader :expires_at, :expires_in, :refresh_token, :token

    def initialize(options = {})
      @api_version   = options.fetch(:api_version, DEFAULT_API_VERSION)
      @base_uri      = options.fetch(:base_uri, DEFAULT_BASE_URI)
      @default_query = options.fetch(:query, DEFAULT_QUERY)

      @token         = options[:token]
      @expires_at    = options[:expires_at]
      @expires_in    = options[:expires_in]
      @refresh_token = options[:refresh_token]
    end

    def get(path, extra_query = {})
      refresh! if expired?

      response = connection.get(build_url(path), params: @default_query.merge(extra_query))
      response.parsed
    end

    def expired?
      connection.expired?
    end

    def refresh!
      response = connection.refresh!

      @token         = response.token
      @expires_at    = response.expires_at
      @expires_in    = response.expires_in
      @refresh_token = response.refresh_token
      @connection    = nil
    end

    private

    def client
      @client ||= OAuth2::Client.new(provider.app_id, provider.app_secret, options)
    end

    def connection
      @connection ||= OAuth2::AccessToken.new(client, @token, refresh_token: @refresh_token, expires_at: @expires_at, expires_in: @expires_in)
    end

    def build_url(path)
      return path if path.starts_with?(root_url)

      "#{root_url}#{path}"
    end

    def root_url
      @root_url ||= "#{@base_uri}#{@api_version}"
    end

    def provider
      Gitlab::OAuth::Provider.config_for('bitbucket')
    end

    def options
      OmniAuth::Strategies::Bitbucket.default_options[:client_options].deep_symbolize_keys
    end
  end
end