class KPM::NexusFacade::CloudsmithApiCalls

def build_base_path_and_coords(coordinates)

def build_base_path_and_coords(coordinates)
  coords = parse_coordinates(coordinates)
  token_org_and_repo = URI.parse(configuration[:url]).path
  [
    "#{token_org_and_repo}/#{coords[:group_id].gsub('.', '/')}/#{coords[:artifact_id]}",
    "#{coords[:version]}/#{coords[:artifact_id]}-#{coords[:version]}.#{coords[:extension]}",
    coords
  ]
end

def build_query_params(_coordinates, _what_parameters = nil)

def build_query_params(_coordinates, _what_parameters = nil)
  ''
end

def get_artifact_info(coordinates)

def get_artifact_info(coordinates)
  _, versioned_artifact, coords = build_base_path_and_coords(coordinates)
  sha1 = get_sha1(coordinates)
  "<artifact-resolution>
a>
resentLocally>true</presentLocally>
roupId>#{coords[:group_id]}</groupId>
rtifactId>#{coords[:artifact_id]}</artifactId>
ersion>#{coords[:version]}</version>
xtension>#{coords[:packaging]}</extension>
napshot>#{!(coords[:version] =~ /-SNAPSHOT$/).nil?}</snapshot>
ha1>#{sha1}</sha1>
epositoryPath>/#{coords[:group_id].gsub('.', '/')}/#{versioned_artifact}</repositoryPath>
ta>
fact-resolution>"
end

def get_artifact_info_endpoint(coordinates)

def get_artifact_info_endpoint(coordinates)
  base_path, _, coords = build_base_path_and_coords(coordinates)
  # Note: we must retrieve the XML for the version, to support SNAPSHOTs
  "#{base_path}/#{coords[:version]}/maven-metadata.xml"
end

def get_sha1(coordinates)

def get_sha1(coordinates)
  base_path, versioned_artifact, = build_base_path_and_coords(coordinates)
  endpoint = "#{base_path}/#{versioned_artifact}.sha1"
  get_response_with_retries(coordinates, endpoint, nil)
end

def pull_artifact_endpoint(coordinates)

def pull_artifact_endpoint(coordinates)
  version_artifact_details = begin
                               parent_get_artifact_info(coordinates)
                             rescue StandardError
                               ''
                             end
  # For SNAPSHOTs, we need to figure out the version used as part of the filename
  filename_version = begin
                       REXML::Document.new(version_artifact_details).elements['//versioning/snapshotVersions/snapshotVersion[1]/value'].text
                     rescue StandardError
                       nil
                     end
  coords = parse_coordinates(coordinates)
  coords[:version] = filename_version unless filename_version.nil?
  new_coordinates = coords.values.compact.join(':')
  base_path, versioned_artifact, = build_base_path_and_coords(new_coordinates)
  "#{base_path}/#{versioned_artifact}"
end

def search_for_artifact_endpoint(_coordinates)

def search_for_artifact_endpoint(_coordinates)
  raise NoMethodError, 'Cloudsmith has no search support'
end