<commit_before>/*
 * Copyright (c) 2016 NECTEC
 *   National Electronics and Computer Technology Center, Thailand
 *
 * 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 th.or.nectec.tanrabad.survey.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import th.or.nectec.tanrabad.entity.User;
import th.or.nectec.tanrabad.survey.BuildConfig;
import th.or.nectec.tanrabad.survey.presenter.AccountUtils;
import th.or.nectec.tanrabad.survey.service.http.Status;

import static java.util.concurrent.TimeUnit.SECONDS;
import static th.or.nectec.tanrabad.survey.service.http.Header.ACCEPT;
import static th.or.nectec.tanrabad.survey.service.http.Header.ACCEPT_CHARSET;
import static th.or.nectec.tanrabad.survey.service.http.Header.IF_MODIFIED_SINCE;
import static th.or.nectec.tanrabad.survey.service.http.Header.LAST_MODIFIED;
import static th.or.nectec.tanrabad.survey.service.http.Header.LINK;

public abstract class AbsRestService<T> implements RestService<T> {

    static final String BASE_API = BuildConfig.API_URL;
    private static final int READ_WRITE_TIMEOUT = 10; //second
    private static final int CONNECT_TIMEOUT = 5; //second

    final OkHttpClient client;
    String baseApi;
    private ServiceLastUpdate serviceLastUpdate;
    private User user;
    private String nextUrl = null;
    private List<T> deletedData;

    AbsRestService(String baseApi, ServiceLastUpdate serviceLastUpdate) {
        this(baseApi, serviceLastUpdate, AccountUtils.getUser());
    }

    private AbsRestService(String baseApi, ServiceLastUpdate serviceLastUpdate, User user) {
        this.baseApi = baseApi;
        this.serviceLastUpdate = serviceLastUpdate;
        this.user = user;
        client = new OkHttpClient.Builder()
                .readTimeout(READ_WRITE_TIMEOUT, SECONDS)
                .writeTimeout(READ_WRITE_TIMEOUT, SECONDS)
                .connectTimeout(CONNECT_TIMEOUT, SECONDS)
                .build();
        deletedData = new ArrayList<>();
    }

    String getApiFilterParam() {
        return user.getApiFilter();
    }

    @Override
    public List<T> getUpdate() throws IOException {
        Request request = makeRequest();
        Response response = client.newCall(request).execute();
        getNextRequest(response);

        if (isNotModified(response))
            return new ArrayList<>();
        if (isNotSuccess(response))
            throw new RestServiceException(response);
        if (!hasNextRequest())
            serviceLastUpdate.save(response.header(LAST_MODIFIED));
<commit_msg>สร้าง method ไว้สำหรับ set api<commit_after>/*
 * Copyright (c) 2016 NECTEC
 *   National Electronics and Computer Technology Center, Thailand
 *
 * 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 th.or.nectec.tanrabad.survey.service;

import android.text.TextUtils;
import android.util.Patterns;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import th.or.nectec.tanrabad.entity.User;
import th.or.nectec.tanrabad.survey.BuildConfig;
import th.or.nectec.tanrabad.survey.presenter.AccountUtils;
import th.or.nectec.tanrabad.survey.service.http.Status;

import static java.util.concurrent.TimeUnit.SECONDS;
import static th.or.nectec.tanrabad.survey.service.http.Header.*;

public abstract class AbsRestService<T> implements RestService<T> {

    private static final int READ_WRITE_TIMEOUT = 10; //second
    private static final int CONNECT_TIMEOUT = 5; //second
    protected static String BASE_API = BuildConfig.API_URL;
    protected final OkHttpClient client;
    protected String baseApi;
    private ServiceLastUpdate serviceLastUpdate;
    private User user;
    private String nextUrl = null;
    private List<T> deletedData;

    AbsRestService(String baseApi, ServiceLastUpdate serviceLastUpdate) {
        this(baseApi, serviceLastUpdate, AccountUtils.getUser());
    }

    private AbsRestService(String baseApi, ServiceLastUpdate serviceLastUpdate, User user) {
        this.serviceLastUpdate = serviceLastUpdate;
        this.user = user;
        this.baseApi = baseApi;

        client = new OkHttpClient.Builder()
                .readTimeout(READ_WRITE_TIMEOUT, SECONDS)
                .writeTimeout(READ_WRITE_TIMEOUT, SECONDS)
                .connectTimeout(CONNECT_TIMEOUT, SECONDS)
                .build();
        deletedData = new ArrayList<>();
    }

    public static void setBaseApi(String baseApi) {
        if (TextUtils.isEmpty(baseApi))
            return;

        if (!Patterns.WEB_URL.matcher(baseApi).matches())
            return;

        BASE_API = baseApi;
    }

    public static String getBaseApi() {
        return BASE_API;
    }

    String getApiFilterParam() {
        return user.getApiFilter();
    }

    @Override
    public List<T> getUpdate() throws IOException {
        Request request = makeRequest();
        Response response = client.newCall(request).execute();
        getNextRequest(response);

        if (isNotModified(response))
            return new ArrayList<>();
        if (isNotSuccess(response))
            throw new RestServiceException(response);
        if (!hasNextRequest())
            serviceLastUpdate.save(response.header(LAST_MODIFIED));

{'fraction_non_alphanumeric': 0.0882307092751364, 'fraction_numerical': 0.00436477007014809, 'mean_word_length': 3.418732782369146, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '4942186', 'n_tokens_mistral': 1836, 'n_tokens_neox': 1801, 'n_words': 489}