<commit_before>package me.scana.okgradle.data.repository

import io.reactivex.Single
import okhttp3.HttpUrl
import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.Request
import javax.xml.stream.XMLInputFactory

class GoogleRepository(private val networkClient: NetworkClient) : ArtifactRepository {

    private val xmlInputFactory = XMLInputFactory.newInstance()

    override fun search(query: String): Single<SearchResult> {
        return Single.create {
            val result = when {
                query.isEmpty() -> SearchResult.Success()
                else -> findArtifacts(query)
            }
            it.onSuccess(result)
        }
    }

    private fun findArtifacts(query: String): SearchResult {
        val requestedArtifacts = ARTIFACT_NAMES.filter { it.contains(query) }
        requestedArtifacts.firstOrNull()?.let {
            val version = getLatestVersion(it)
            return SearchResult.Success(
                    requestedArtifacts.map {
                        val (groupId, name) = it.split(":".toRegex(), 2)
                        Artifact(groupId, name, version)
                    }
            )
        }
        return SearchResult.Success()
    }

    private fun getLatestVersion(artifactId: String): String {
        val path = artifactId
                .replace('.', '/')
                .replace(':', '/')

        val url = GOOGLE_MAVEN_URL.newBuilder()
                .addPathSegment(path)
                .addPathSegment(MAVEN_METADATA)
                .build()

        val request = Request.Builder()
                .url(url)
                .build()
<commit_msg>Fetch androidx artifact version for first 10 packages<commit_after>package me.scana.okgradle.data.repository

import io.reactivex.Single
import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.Request
import javax.xml.stream.XMLInputFactory

class GoogleRepository(private val networkClient: NetworkClient) : ArtifactRepository {

    private val xmlInputFactory = XMLInputFactory.newInstance()

    override fun search(query: String): Single<SearchResult> {
        return Single.create {
            val result = when {
                query.isEmpty() -> SearchResult.Success()
                else -> findArtifacts(query)
            }
            it.onSuccess(result)
        }
    }

    private fun findArtifacts(query: String): SearchResult {
        val artifactIds = ARTIFACT_NAMES.filter { it.contains(query) }
        val artifacts = artifactIds
                .mapIndexed { index, artifactId ->
                    // check version for only first couple of artifacts,
                    // to avoid making too many requests at once
                    val version = if (index < 10)  {
                        getLatestVersion(artifactId)
                    } else {
                        "+"
                    }
                    val (groupId, name) = artifactId.split(":".toRegex(), 2)
                    Artifact(groupId, name, version)
                }
        return SearchResult.Success(artifacts)
    }

    private fun getLatestVersion(artifactId: String): String {
        val path = artifactId
                .replace('.', '/')
                .replace(':', '/')

        val url = GOOGLE_MAVEN_URL.newBuilder()
                .addPathSegment(path)
                .addPathSegment(MAVEN_METADATA)
                .build()

        val request = Request.Builder()
                .url(url)
                .build()

{'fraction_non_alphanumeric': 0.09080459770114943, 'fraction_numerical': 0.003160919540229885, 'mean_word_length': 1.7781324820430966, 'pattern_counts': {'":': 2, '<': 6, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5713378', 'n_tokens_mistral': 937, 'n_tokens_neox': 850, 'n_words': 195}