2012-08-23 05:19:40 -04:00
|
|
|
module ApiHelpers
|
2012-08-25 13:31:50 -04:00
|
|
|
# Public: Prepend a request path with the path to the API
|
|
|
|
#
|
|
|
|
# path - Path to append
|
|
|
|
# user - User object - If provided, automatically appends private_token query
|
|
|
|
# string for authenticated requests
|
|
|
|
#
|
|
|
|
# Examples
|
|
|
|
#
|
|
|
|
# >> api('/issues')
|
|
|
|
# => "/api/v2/issues"
|
|
|
|
#
|
|
|
|
# >> api('/issues', User.last)
|
|
|
|
# => "/api/v2/issues?private_token=..."
|
|
|
|
#
|
|
|
|
# >> api('/issues?foo=bar', User.last)
|
|
|
|
# => "/api/v2/issues?foo=bar&private_token=..."
|
|
|
|
#
|
|
|
|
# Returns the relative path to the requested API resource
|
2017-06-20 08:02:25 -04:00
|
|
|
def api(path, user = nil, version: API::API.version, personal_access_token: nil, oauth_access_token: nil)
|
2017-01-30 12:41:56 -05:00
|
|
|
"/api/#{version}#{path}" +
|
2012-08-25 13:31:50 -04:00
|
|
|
|
|
|
|
# Normalize query string
|
|
|
|
(path.index('?') ? '' : '?') +
|
|
|
|
|
2017-06-20 03:40:24 -04:00
|
|
|
if personal_access_token.present?
|
|
|
|
"&private_token=#{personal_access_token.token}"
|
2017-06-20 08:02:25 -04:00
|
|
|
elsif oauth_access_token.present?
|
|
|
|
"&access_token=#{oauth_access_token.token}"
|
2012-08-25 13:31:50 -04:00
|
|
|
# Append private_token if given a User object
|
2017-06-20 03:40:24 -04:00
|
|
|
elsif user.respond_to?(:private_token)
|
2016-05-30 08:06:06 -04:00
|
|
|
"&private_token=#{user.private_token}"
|
|
|
|
else
|
|
|
|
''
|
|
|
|
end
|
2012-08-23 05:19:40 -04:00
|
|
|
end
|
|
|
|
|
2017-01-30 12:41:56 -05:00
|
|
|
# Temporary helper method for simplifying V3 exclusive API specs
|
2017-06-20 08:02:25 -04:00
|
|
|
def v3_api(path, user = nil, personal_access_token: nil, oauth_access_token: nil)
|
|
|
|
api(
|
|
|
|
path,
|
|
|
|
user,
|
|
|
|
version: 'v3',
|
|
|
|
personal_access_token: personal_access_token,
|
|
|
|
oauth_access_token: oauth_access_token
|
|
|
|
)
|
2017-01-30 12:41:56 -05:00
|
|
|
end
|
2012-08-23 05:19:40 -04:00
|
|
|
end
|