Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
heptapod
heptapod
Commits
8cd306ff0691
Commit
8cd306ff
authored
Aug 22, 2016
by
Douglas Barbosa Alexandre
Browse files
Add a Bitbucket client for the OAuth2 API
parent
26b0bbb5d869
Changes
2
Hide whitespace changes
Inline
Side-by-side
lib/bitbucket/client.rb
0 → 100644
View file @
8cd306ff
module
Bitbucket
class
Client
def
initialize
(
options
=
{})
@connection
=
options
.
fetch
(
:connection
,
Connection
.
new
(
options
))
end
private
attr_reader
:connection
end
end
lib/bitbucket/connection.rb
0 → 100644
View file @
8cd306ff
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
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment