BigW Consortium Gitlab

api_helpers.rb 1.72 KB
Newer Older
1
module ApiHelpers
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
  # 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
20
  def api(path, user = nil, version: API::API.version, personal_access_token: nil, oauth_access_token: nil)
21
    "/api/#{version}#{path}" +
22 23 24 25

      # Normalize query string
      (path.index('?') ? '' : '?') +

26 27
      if personal_access_token.present?
        "&private_token=#{personal_access_token.token}"
28 29
      elsif oauth_access_token.present?
        "&access_token=#{oauth_access_token.token}"
30
      # Append private_token if given a User object
31
      elsif user.respond_to?(:private_token)
32 33 34 35
        "&private_token=#{user.private_token}"
      else
        ''
      end
36 37
  end

38
  # Temporary helper method for simplifying V3 exclusive API specs
39 40 41 42 43 44 45 46
  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
    )
47 48
  end

Valery Sizov committed
49 50 51 52 53 54 55
  def ci_api(path, user = nil)
    "/ci/api/v1/#{path}" +

      # Normalize query string
      (path.index('?') ? '' : '?') +

      # Append private_token if given a User object
56 57 58 59 60
      if user.respond_to?(:private_token)
        "&private_token=#{user.private_token}"
      else
        ''
      end
Valery Sizov committed
61
  end
62
end