<commit_before>package com.commit451.gitlab.api

import com.apollographql.apollo.ApolloClient
import com.commit451.gitlab.CurrentUserQuery
import com.commit451.gitlab.api.graphql.rxQueryMapErrors
import com.commit451.gitlab.model.Account
import com.squareup.moshi.Moshi
import com.squareup.moshi.Types
import io.reactivex.Single
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
import okhttp3.OkHttpClient

/**
 * Provides access to all the GitLab things. Wraps RSS and the Retrofit service, in
 * case we need to do overrides or global
 */
class GitLab private constructor(
        builder: Builder,
        gitLabService: GitLabService,
        gitLabRss: GitLabRss
) : GitLabService by gitLabService, GitLabRss by gitLabRss {

    val moshi: Moshi
    private val apolloClient: ApolloClient
    val client: OkHttpClient
    val account: Account

    init {
        client = builder.clientBuilder?.build() ?: OkHttpClient()
        account = builder.account
        moshi = MoshiProvider.moshi
        val apolloUrl = account.serverUrl?.toHttpUrlOrNull()?.newBuilder()
                ?.addPathSegment("api")
                ?.addPathSegment("graphql")
                ?.build()
                ?.toString()!!
        apolloClient = ApolloClient.builder()
                .serverUrl(apolloUrl)
                .okHttpClient(client)
                .build()
    }

    inline fun <reified T> loadAny(url: String): Single<retrofit2.Response<T>> {
        return get(url)
                .map {
                    val source = it.body()?.source() ?: throw NullBodyException()
                    val body = moshi.adapter(T::class.java).fromJson(source)
                    retrofit2.Response.success(body)
                }
    }

    inline fun <reified T> loadAnyList(url: String): Single<retrofit2.Response<List<T>>> {
        return get(url)
                .map {
                    val source = it.body()?.source() ?: throw NullBodyException()
                    val type = Types.newParameterizedType(List::class.java, T::class.java)
                    val body = moshi.adapter<List<T>>(type).fromJson(source)
                    retrofit2.Response.success(body)
                }
    }

    fun currentUser(): Single<CurrentUserQuery.CurrentUser> {
        val query = CurrentUserQuery.builder()
                .build()
        return apolloClient.rxQueryMapErrors(query)
                .singleOrError()
                .map { it.currentUser() }
    }

    class Builder(internal val account: Account) {

        internal var clientBuilder: OkHttpClient.Builder? = null

        fun clientBuilder(clientBuilder: OkHttpClient.Builder): Builder {
            this.clientBuilder = clientBuilder
            return this
        }

        fun build(): GitLab {<commit_msg>Headers are important<commit_after>package com.commit451.gitlab.api

import com.apollographql.apollo.ApolloClient
import com.commit451.gitlab.CurrentUserQuery
import com.commit451.gitlab.api.graphql.rxQueryMapErrors
import com.commit451.gitlab.model.Account
import com.squareup.moshi.Moshi
import com.squareup.moshi.Types
import io.reactivex.Single
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
import okhttp3.OkHttpClient

/**
 * Provides access to all the GitLab things. Wraps RSS and the Retrofit service, in
 * case we need to do overrides or global
 */
class GitLab private constructor(
        builder: Builder,
        gitLabService: GitLabService,
        gitLabRss: GitLabRss
) : GitLabService by gitLabService, GitLabRss by gitLabRss {

    val moshi: Moshi
    private val apolloClient: ApolloClient
    val client: OkHttpClient
    val account: Account

    init {
        client = builder.clientBuilder?.build() ?: OkHttpClient()
        account = builder.account
        moshi = MoshiProvider.moshi
        val apolloUrl = account.serverUrl?.toHttpUrlOrNull()?.newBuilder()
                ?.addPathSegment("api")
                ?.addPathSegment("graphql")
                ?.build()
                ?.toString()!!
        apolloClient = ApolloClient.builder()
                .serverUrl(apolloUrl)
                .okHttpClient(client)
                .build()
    }

    inline fun <reified T> loadAny(url: String): Single<retrofit2.Response<T>> {
        return get(url)
                .map {
                    val source = it.body()?.source() ?: throw NullBodyException()
                    val body = moshi.adapter(T::class.java).fromJson(source)
                    retrofit2.Response.success(body, it.raw())
                }
    }

    inline fun <reified T> loadAnyList(url: String): Single<retrofit2.Response<List<T>>> {
        return get(url)
                .map {
                    val source = it.body()?.source() ?: throw NullBodyException()
                    val type = Types.newParameterizedType(List::class.java, T::class.java)
                    val body = moshi.adapter<List<T>>(type).fromJson(source)
                    retrofit2.Response.success(body, it.raw())
                }
    }

    fun currentUser(): Single<CurrentUserQuery.CurrentUser> {
        val query = CurrentUserQuery.builder()
                .build()
        return apolloClient.rxQueryMapErrors(query)
                .singleOrError()
                .map { it.currentUser() }
    }

    class Builder(internal val account: Account) {

        internal var clientBuilder: OkHttpClient.Builder? = null

        fun clientBuilder(clientBuilder: OkHttpClient.Builder): Builder {
            this.clientBuilder = clientBuilder
            return this
        }

        fun build(): GitLab {
{'fraction_non_alphanumeric': 0.09699355762347889, 'fraction_numerical': 0.006442376521116679, 'mean_word_length': 2.2139160437032777, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '8151', 'n_tokens_mistral': 1643, 'n_tokens_neox': 1516, 'n_words': 336}