<commit_before>package org.ccci.gto.android.common.api;

import static java.net.HttpURLConnection.HTTP_UNAUTHORIZED;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import org.ccci.gto.android.common.api.AbstractTheKeyApi.ExecutionContext;
import org.ccci.gto.android.common.api.AbstractTheKeyApi.Request;
import org.ccci.gto.android.common.api.AbstractTheKeyApi.Session;
import org.ccci.gto.android.common.util.HttpHeaderUtils;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.Locale;

import me.thekey.android.TheKey;

public abstract class AbstractTheKeyApi<R extends Request<C, S>, C extends ExecutionContext<S>, S extends Session>
        extends AbstractApi<R, C, S> {
    private static final String PREF_CACHED_SERVICE = "service";

    @NonNull
    protected final TheKey mTheKey;
    @Nullable
    protected final String mGuid;

    protected AbstractTheKeyApi(@NonNull final Context context, @NonNull final TheKey theKey,
                                @NonNull final String baseUri) {
        this(context, theKey, baseUri, null, null);
    }

    protected AbstractTheKeyApi(@NonNull final Context context, @NonNull final TheKey theKey,
                                @NonNull final String baseUri, @Nullable final String prefFile) {
        this(context, theKey, baseUri, prefFile, null);
    }

    protected AbstractTheKeyApi(@NonNull final Context context, @NonNull final TheKey theKey,
                                @NonNull final String baseUri, @Nullable final String prefFile,
                                @Nullable final String guid) {
        super(context, baseUri, prefFile);
        mTheKey = theKey;
        mGuid = guid;
        init();
    }

    protected AbstractTheKeyApi(@NonNull final Context context, @NonNull final TheKey theKey,
                                @NonNull final Uri baseUri) {
        this(context, theKey, baseUri, null, null);
    }

    protected AbstractTheKeyApi(@NonNull final Context context, @NonNull final TheKey theKey,
                                @NonNull final Uri baseUri, @Nullable final String prefFile,
                                @Nullable final String guid) {
        super(context, baseUri, prefFile);
        mTheKey = theKey;
        mGuid = guid;
        init();
    }

    private void init() {
        // initialize service if there isn't one already defined
        if (getCachedService() == null) {
            setCachedService(getDefaultService());
        }
    }

    @Nullable
    protected String getActiveGuid() {
        String guid = mGuid;
        if (guid == null) {
            guid = mTheKey.getDefaultSessionGuid();
        }

        return guid;
    }

    @Nullable
    protected String getService() throws ApiException {
        // short-circuit if we have a cached service
        String service = getCachedService();
        if (service != null) {<commit_msg>avoid intermediate object for getActiveGuid<commit_after>package org.ccci.gto.android.common.api;

import static java.net.HttpURLConnection.HTTP_UNAUTHORIZED;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import org.ccci.gto.android.common.api.AbstractTheKeyApi.ExecutionContext;
import org.ccci.gto.android.common.api.AbstractTheKeyApi.Request;
import org.ccci.gto.android.common.api.AbstractTheKeyApi.Session;
import org.ccci.gto.android.common.util.HttpHeaderUtils;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.Locale;

import me.thekey.android.TheKey;

public abstract class AbstractTheKeyApi<R extends Request<C, S>, C extends ExecutionContext<S>, S extends Session>
        extends AbstractApi<R, C, S> {
    private static final String PREF_CACHED_SERVICE = "service";

    @NonNull
    protected final TheKey mTheKey;
    @Nullable
    protected final String mGuid;

    protected AbstractTheKeyApi(@NonNull final Context context, @NonNull final TheKey theKey,
                                @NonNull final String baseUri) {
        this(context, theKey, baseUri, null, null);
    }

    protected AbstractTheKeyApi(@NonNull final Context context, @NonNull final TheKey theKey,
                                @NonNull final String baseUri, @Nullable final String prefFile) {
        this(context, theKey, baseUri, prefFile, null);
    }

    protected AbstractTheKeyApi(@NonNull final Context context, @NonNull final TheKey theKey,
                                @NonNull final String baseUri, @Nullable final String prefFile,
                                @Nullable final String guid) {
        super(context, baseUri, prefFile);
        mTheKey = theKey;
        mGuid = guid;
        init();
    }

    protected AbstractTheKeyApi(@NonNull final Context context, @NonNull final TheKey theKey,
                                @NonNull final Uri baseUri) {
        this(context, theKey, baseUri, null, null);
    }

    protected AbstractTheKeyApi(@NonNull final Context context, @NonNull final TheKey theKey,
                                @NonNull final Uri baseUri, @Nullable final String prefFile,
                                @Nullable final String guid) {
        super(context, baseUri, prefFile);
        mTheKey = theKey;
        mGuid = guid;
        init();
    }

    private void init() {
        // initialize service if there isn't one already defined
        if (getCachedService() == null) {
            setCachedService(getDefaultService());
        }
    }

    @Nullable
    protected String getActiveGuid() {
        return mGuid != null ? mGuid : mTheKey.getDefaultSessionGuid();
    }

    @Nullable
    protected String getService() throws ApiException {
        // short-circuit if we have a cached service
        String service = getCachedService();
        if (service != null) {
{'fraction_non_alphanumeric': 0.08237392573374412, 'fraction_numerical': 0.0, 'mean_word_length': 2.673615247170935, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '380110', 'n_tokens_mistral': 1621, 'n_tokens_neox': 1642, 'n_words': 463}