summaryrefslogtreecommitdiff
path: root/lib/bitbucket/connection.rb
blob: e9cbf36a44b5323f8d7fa17bb94ab9e39580b78e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
module Bitbucket
  class Connection
    DEFAULT_API_VERSION = '2.0'
    DEFAULT_BASE_URI    = 'https://api.bitbucket.org/'
    DEFAULT_QUERY       = {}

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

      @token         = options.fetch(:token)
      @expires_at    = options.fetch(:expires_at)
      @expires_in    = options.fetch(:expires_in)
      @refresh_token = options.fetch(:refresh_token)
    end

    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 query(params = {})
      @query.merge!(params)
    end

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

      response = connection.get(build_url(path), params: @query.merge(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 = OAuth2::AccessToken.new(@client, @token, refresh_token: @refresh_token, expires_at: @expires_at, expires_in: @expires_in)
    end

    private

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

    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.config.omniauth.providers.find { |provider| provider.name == 'bitbucket' }
    end

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