<commit_before>/*
 * Copyright (C) 2015 Appformation sp. z o.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package pl.appformation.smash;

import android.support.annotation.NonNull;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;
import java.io.IOException;
import okio.BufferedSource;
import pl.appformation.smash.errors.SmashError;
import static pl.appformation.smash.SmashRequest.Method.DELETE;
import static pl.appformation.smash.SmashRequest.Method.GET;
import static pl.appformation.smash.SmashRequest.Method.HEAD;
import static pl.appformation.smash.SmashRequest.Method.PATCH;
import static pl.appformation.smash.SmashRequest.Method.POST;
import static pl.appformation.smash.SmashRequest.Method.PUT;

public class SmashOkHttp
{

    private static OkHttpClient sHttpClient = new OkHttpClient();

    public static @NonNull SmashNetworkData perform(SmashRequest<?> request) throws SmashError
    {
        SmashNetworkData data = new SmashNetworkData();

        try
        {
            Request.Builder okBuilder = new Request.Builder().url(request.getUrl());
            okBuilder.removeHeader("User-Agent").addHeader("User-Agent", Smash.getUserAgent());

            BufferedSource body = getBody(request);
            switch (request.getMethod())
            {
                case GET: okBuilder = okBuilder.get(); break;
                case POST: okBuilder = okBuilder.post(convertBody(request, body)); break;
                case PUT: okBuilder = okBuilder.put(convertBody(request, body)); break;
                case DELETE: okBuilder = okBuilder.delete(convertBody(request, body)); break;
                case HEAD: okBuilder = okBuilder.head(); break;
                case PATCH: okBuilder = okBuilder.patch(convertBody(request, body)); break;
            }

            Request okRequest = okBuilder.build();
            Response okResponse = sHttpClient.newCall(okRequest).execute();

            if (body != null)
            {
                body.close();
            }

            data.code = okResponse.code();
            data.headers = okResponse.headers();
            data.source = okResponse.body().source();
            data.length = okResponse.body().contentLength();
        }
        catch (IOException ioe)
        {
            throw new SmashError(ioe);
        }

        return data;
    }

    private static RequestBody convertBody(SmashRequest request, BufferedSource body) throws SmashError
    {
        try
        {
            return RequestBody.create(MediaType.parse(request.getBodyContentType()), body.readUtf8());
        }
        catch (IOException ioe)
        {
            throw new SmashError(ioe);
        }
    }

    private static BufferedSource getBody(SmashRequest request)
    {
        if (request.getMethod() == GET || request.getMethod() == HEAD)
        {
            return null;
        }

        return request.getBody();
    }

}<commit_msg>Allowing to add and remove OkHttp Interceptors. Useful in integration with Stetho.<commit_after>/*
 * Copyright (C) 2015 Appformation sp. z o.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package pl.appformation.smash;

import android.support.annotation.NonNull;
import com.squareup.okhttp.Interceptor;
import com.squareup.okhttp.MediaType;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.RequestBody;
import com.squareup.okhttp.Response;
import java.io.IOException;
import okio.BufferedSource;
import pl.appformation.smash.errors.SmashError;
import static pl.appformation.smash.SmashRequest.Method.DELETE;
import static pl.appformation.smash.SmashRequest.Method.GET;
import static pl.appformation.smash.SmashRequest.Method.HEAD;
import static pl.appformation.smash.SmashRequest.Method.PATCH;
import static pl.appformation.smash.SmashRequest.Method.POST;
import static pl.appformation.smash.SmashRequest.Method.PUT;

public class SmashOkHttp
{

    /** Default OkHttpClient instance */
    private static OkHttpClient sHttpClient = new OkHttpClient();

    /**
     * Adds {@link Interceptor} object to default {@link OkHttpClient} instance.
     *
     * @param interceptor Interceptor object
     */
    public static void addNetworkInterceptor(@NonNull Interceptor interceptor)
    {
        sHttpClient.networkInterceptors().add(interceptor);
    }

    private static RequestBody convertBody(SmashRequest request, BufferedSource body) throws SmashError
    {
        try
        {
            return RequestBody.create(MediaType.parse(request.getBodyContentType()), body.readUtf8());
        }
        catch (IOException ioe)
        {
            throw new SmashError(ioe);
        }
    }

    private static BufferedSource getBody(SmashRequest request)
    {
        if (request.getMethod() == GET || request.getMethod() == HEAD)
        {
            return null;
        }

        return request.getBody();
    }

    static @NonNull SmashNetworkData perform(SmashRequest<?> request) throws SmashError
    {
        SmashNetworkData data = new SmashNetworkData();

        try
        {
            Request.Builder okBuilder = new Request.Builder().url(request.getUrl());
            okBuilder.removeHeader("User-Agent").addHeader("User-Agent", Smash.getUserAgent());

            BufferedSource body = getBody(request);
            switch (request.getMethod())
            {
                case GET:
                {
                    okBuilder = okBuilder.get();
                    break;
                }
                case POST:
                {
                    okBuilder = okBuilder.post(convertBody(request, body));
                    break;
                }
                case PUT:
                {
                    okBuilder = okBuilder.put(convertBody(request, body));
                    break;
                }
                case DELETE:
                {
                    okBuilder = okBuilder.delete(convertBody(request, body));
                    break;
                }
                case HEAD:
                {
                    okBuilder = okBuilder.head();
                    break;
                }
                case PATCH:
                {
                    okBuilder = okBuilder.patch(convertBody(request, body));
                    break;
                }
            }

            Request okRequest = okBuilder.build();
            Response okResponse = sHttpClient.newCall(okRequest).execute();

            if (body != null)
            {
                body.close();
            }

            data.code = okResponse.code();
            data.headers = okResponse.headers();
            data.source = okResponse.body().source();
            data.length = okResponse.body().contentLength();
        }
        catch (IOException ioe)
        {
            throw new SmashError(ioe);
        }

        return data;
    }

    /**
     * Removes {@link Interceptor} object from default {@link OkHttpClient} instance.
     *
     * @param interceptor Interceptor object
     */
    public static void removeNetworkInterceptor(@NonNull Interceptor interceptor)
    {
        sHttpClient.networkInterceptors().remove(interceptor);
    }

}
{'fraction_non_alphanumeric': 0.0930704898446834, 'fraction_numerical': 0.002150537634408602, 'mean_word_length': 2.2534006995724836, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '3953397', 'n_tokens_mistral': 2221, 'n_tokens_neox': 2146, 'n_words': 555}