summaryrefslogtreecommitdiff
path: root/qa/qa/runtime/api/request.rb
blob: 724b499d32f93f8daa39909bcdf8b2ac19602f7f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# frozen_string_literal: true

module QA
  module Runtime
    module API
      class Request
        API_VERSION = 'v4'.freeze

        def initialize(api_client, path, **query_string)
          query_string[:private_token] ||= api_client.personal_access_token unless query_string[:oauth_access_token]
          request_path = request_path(path, **query_string)
          @session_address = Runtime::Address.new(api_client.address, request_path)
        end

        def mask_url
          @session_address.address.sub(/private_token=.*/, "private_token=[****]")
        end

        def url
          @session_address.address
        end

        # Prepend a request path with the path to the API
        #
        # path - Path to append
        #
        # Examples
        #
        #   >> request_path('/issues')
        #   => "/api/v4/issues"
        #
        #   >> request_path('/issues', private_token: 'sometoken)
        #   => "/api/v4/issues?private_token=..."
        #
        # Returns the relative path to the requested API resource
        def request_path(path, version: API_VERSION, **query_string)
          full_path = ::File.join('/api', version, path)

          if query_string.any?
            full_path << (path.include?('?') ? '&' : '?')
            full_path << query_string.map { |k, v| "#{k}=#{CGI.escape(v)}" }.join('&')
          end

          full_path
        end
      end
    end
  end
end