Commit fc34c956 authored by Douglas Barbosa Alexandre's avatar Douglas Barbosa Alexandre Committed by Stan Hu

Add a Bitbucket client for the OAuth2 API

parent a2ef52b3
module Bitbucket
class Client
def initialize(options = {})
@connection = options.fetch(:connection, Connection.new(options))
end
private
attr_reader :connection
end
end
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)
@client = OAuth2::Client.new(provider.app_id, provider.app_secret, options)
@connection = OAuth2::AccessToken.new(@client, @token, refresh_token: @refresh_token, expires_at: @expires_at, expires_in: @expires_in)
end
def query(params = {})
@query.update(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 :connection, :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
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment