debian-mirror-gitlab/lib/bulk_imports/clients/graphql.rb

76 lines
2.1 KiB
Ruby
Raw Normal View History

2021-01-29 00:20:46 +05:30
# frozen_string_literal: true
module BulkImports
module Clients
class Graphql
class HTTP < Graphlient::Adapters::HTTP::Adapter
def execute(document:, operation_name: nil, variables: {}, context: {})
response = ::Gitlab::HTTP.post(
url,
headers: headers,
follow_redirects: false,
body: {
query: document.to_query_string,
operationName: operation_name,
variables: variables
}.to_json
)
2023-04-23 21:23:45 +05:30
unless response.success?
raise ::BulkImports::NetworkError.new(
"Unsuccessful response #{response.code} from #{response.request.path.path}",
response: response
)
end
2021-01-29 00:20:46 +05:30
::Gitlab::Json.parse(response.body)
2023-04-23 21:23:45 +05:30
rescue *Gitlab::HTTP::HTTP_ERRORS, JSON::ParserError => e
2021-11-18 22:05:49 +05:30
raise ::BulkImports::NetworkError, e
2021-01-29 00:20:46 +05:30
end
end
private_constant :HTTP
attr_reader :client
2021-09-30 23:02:18 +05:30
delegate :query, :parse, to: :client
2021-01-29 00:20:46 +05:30
2021-09-04 01:27:46 +05:30
def initialize(url: Gitlab::Saas.com_url, token: nil)
2021-01-29 00:20:46 +05:30
@url = Gitlab::Utils.append_path(url, '/api/graphql')
@token = token
2021-09-30 23:02:18 +05:30
@client = Graphlient::Client.new(@url, options(http: HTTP))
@compatible_instance_version = false
end
2023-04-23 21:23:45 +05:30
def execute(...)
2021-09-30 23:02:18 +05:30
validate_instance_version!
2023-04-23 21:23:45 +05:30
client.execute(...)
2021-01-29 00:20:46 +05:30
end
def options(extra = {})
return extra unless @token
{
headers: {
'Content-Type' => 'application/json',
'Authorization' => "Bearer #{@token}"
}
}.merge(extra)
end
2021-09-30 23:02:18 +05:30
def validate_instance_version!
return if @compatible_instance_version
response = client.execute('{ metadata { version } }')
version = Gitlab::VersionInfo.parse(response.data.metadata.version)
2021-11-18 22:05:49 +05:30
if version.major < BulkImport::MIN_MAJOR_VERSION
2021-09-30 23:02:18 +05:30
raise ::BulkImports::Error.unsupported_gitlab_version
else
@compatible_instance_version = true
end
end
2021-01-29 00:20:46 +05:30
end
end
end