Class: BulkImports::Clients::HTTP

Inherits:
Object
  • Object
show all
Includes:
Gitlab::Utils::StrongMemoize
Defined in:
lib/bulk_imports/clients/http.rb

Constant Summary collapse

API_VERSION =
'v4'
DEFAULT_PAGE =
1
DEFAULT_PER_PAGE =
30

Instance Method Summary collapse

Methods included from Gitlab::Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Constructor Details

#initialize(url:, token:, page: DEFAULT_PAGE, per_page: DEFAULT_PER_PAGE, api_version: API_VERSION) ⇒ HTTP

Returns a new instance of HTTP.


12
13
14
15
16
17
18
19
# File 'lib/bulk_imports/clients/http.rb', line 12

def initialize(url:, token:, page: DEFAULT_PAGE, per_page: DEFAULT_PER_PAGE, api_version: API_VERSION)
  @url = url
  @token = token&.strip
  @page = page
  @per_page = per_page
  @api_version = api_version
  @compatible_instance_version = false
end

Instance Method Details

#compatible_for_project_migration?Boolean

Returns:

  • (Boolean)

67
68
69
# File 'lib/bulk_imports/clients/http.rb', line 67

def compatible_for_project_migration?
  instance_version >= BulkImport.min_gl_version_for_project_migration
end

#each_page(method, resource, query = {}, &block) ⇒ Object


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/bulk_imports/clients/http.rb', line 37

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

#get(resource, query = {}) ⇒ Object


21
22
23
# File 'lib/bulk_imports/clients/http.rb', line 21

def get(resource, query = {})
  request(:get, resource, query: query.reverse_merge(request_query))
end

#head(resource) ⇒ Object


29
30
31
# File 'lib/bulk_imports/clients/http.rb', line 29

def head(resource)
  request(:head, resource)
end

#instance_versionObject


57
58
59
60
61
62
63
64
65
# File 'lib/bulk_imports/clients/http.rb', line 57

def instance_version
  strong_memoize(:instance_version) do
    response = with_error_handling do
      Gitlab::HTTP.get(resource_url(:version), default_options)
    end

    Gitlab::VersionInfo.parse(response.parsed_response['version'])
  end
end

#post(resource, body = {}) ⇒ Object


25
26
27
# File 'lib/bulk_imports/clients/http.rb', line 25

def post(resource, body = {})
  request(:post, resource, body: body)
end

#resource_url(resource) ⇒ Object


53
54
55
# File 'lib/bulk_imports/clients/http.rb', line 53

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

#stream(resource, &block) ⇒ Object


33
34
35
# File 'lib/bulk_imports/clients/http.rb', line 33

def stream(resource, &block)
  request(:get, resource, stream_body: true, &block)
end