2014-09-02 18:07:02 +05:30
|
|
|
module ApiHelpers
|
|
|
|
# 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-09-10 17:25:29 +05:30
|
|
|
def api(path, user = nil, version: API::API.version, personal_access_token: nil, oauth_access_token: nil)
|
2018-03-17 18:26:18 +05:30
|
|
|
full_path = "/api/#{version}#{path}"
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
if oauth_access_token
|
|
|
|
query_string = "access_token=#{oauth_access_token.token}"
|
|
|
|
elsif personal_access_token
|
|
|
|
query_string = "private_token=#{personal_access_token.token}"
|
|
|
|
elsif user
|
|
|
|
personal_access_token = create(:personal_access_token, user: user)
|
|
|
|
query_string = "private_token=#{personal_access_token.token}"
|
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
if query_string
|
|
|
|
full_path << (path.index('?') ? '&' : '?')
|
|
|
|
full_path << query_string
|
|
|
|
end
|
|
|
|
|
|
|
|
full_path
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
# Temporary helper method for simplifying V3 exclusive API specs
|
2017-09-10 17:25:29 +05:30
|
|
|
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-08-17 22:00:37 +05:30
|
|
|
end
|
2014-09-02 18:07:02 +05:30
|
|
|
end
|