Welcome to mirror list, hosted at ThFree Co, Russian Federation.

http.rb « clients « bulk_imports « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ef99122cdfde645c2b0a10b27e39722cb091c973 (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# frozen_string_literal: true

module BulkImports
  module Clients
    class Http
      API_VERSION = 'v4'
      DEFAULT_PAGE = 1
      DEFAULT_PER_PAGE = 30

      ConnectionError = Class.new(StandardError)

      def initialize(uri:, token:, page: DEFAULT_PAGE, per_page: DEFAULT_PER_PAGE, api_version: API_VERSION)
        @uri = URI.parse(uri)
        @token = token&.strip
        @page = page
        @per_page = per_page
        @api_version = api_version
      end

      def get(resource, query = {})
        with_error_handling do
          Gitlab::HTTP.get(
            resource_url(resource),
            headers: request_headers,
            follow_redirects: false,
            query: query.reverse_merge(request_query)
          )
        end
      end

      def each_page(method, resource, query = {}, &block)
        return to_enum(__method__, method, resource, query) unless block_given?

        next_page = @page

        while next_page
          @page = next_page.to_i

          response = self.public_send(method, resource, query) # rubocop: disable GitlabSecurity/PublicSend
          collection = response.parsed_response
          next_page = response.headers['x-next-page'].presence

          yield collection
        end
      end

      private

      def request_query
        {
          page: @page,
          per_page: @per_page
        }
      end

      def request_headers
        {
          'Content-Type' => 'application/json',
          'Authorization' => "Bearer #{@token}"
        }
      end

      def with_error_handling
        response = yield

        raise ConnectionError.new("Error #{response.code}") unless response.success?

        response
      rescue *Gitlab::HTTP::HTTP_ERRORS => e
        raise ConnectionError, e
      end

      def base_uri
        @base_uri ||= "#{@uri.scheme}://#{@uri.host}:#{@uri.port}"
      end

      def api_url
        Gitlab::Utils.append_path(base_uri, "/api/#{@api_version}")
      end

      def resource_url(resource)
        Gitlab::Utils.append_path(api_url, resource)
      end
    end
  end
end