diff options
author | Douglas Barbosa Alexandre <dbalexandre@gmail.com> | 2016-08-22 15:42:26 -0300 |
---|---|---|
committer | Stan Hu <stanhu@gmail.com> | 2016-11-21 16:47:24 -0800 |
commit | fc34c9560324b7db5bdaf205cbdf46a339102af2 (patch) | |
tree | 43fb34121cf343dbc2a08dd8e8d90a4a9f62d945 /lib/bitbucket | |
parent | a2ef52b32bd3d1ee70ea333566e330c44e6c9170 (diff) | |
download | gitlab-ce-fc34c9560324b7db5bdaf205cbdf46a339102af2.tar.gz |
Add a Bitbucket client for the OAuth2 API
Diffstat (limited to 'lib/bitbucket')
-rw-r--r-- | lib/bitbucket/client.rb | 11 | ||||
-rw-r--r-- | lib/bitbucket/connection.rb | 69 |
2 files changed, 80 insertions, 0 deletions
diff --git a/lib/bitbucket/client.rb b/lib/bitbucket/client.rb new file mode 100644 index 00000000000..5f48e52da98 --- /dev/null +++ b/lib/bitbucket/client.rb @@ -0,0 +1,11 @@ +module Bitbucket + class Client + def initialize(options = {}) + @connection = options.fetch(:connection, Connection.new(options)) + end + + private + + attr_reader :connection + end +end diff --git a/lib/bitbucket/connection.rb b/lib/bitbucket/connection.rb new file mode 100644 index 00000000000..00f127f9507 --- /dev/null +++ b/lib/bitbucket/connection.rb @@ -0,0 +1,69 @@ +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 |