BigW Consortium Gitlab

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

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

      # Append private_token if given a User object
      (user.respond_to?(:private_token) ?
        "&private_token=#{user.private_token}" : "")
29 30
  end

Valery Sizov committed
31 32 33 34 35 36 37 38 39 40 41
  def ci_api(path, user = nil)
    "/ci/api/v1/#{path}" +

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

      # Append private_token if given a User object
      (user.respond_to?(:private_token) ?
        "&private_token=#{user.private_token}" : "")
  end

42
  def json_response
43
    @_json_response ||= JSON.parse(response.body)
44 45
  end
end