/**
 * Copyright (c) Microsoft Corporation. All rights reserved.
 * Licensed under the MIT License. See License.txt in the project root for
 * license information.
 *
 * Code generated by Microsoft (R) AutoRest Code Generator.
 */

package com.microsoft.azure.cognitiveservices.knowledge.qnamaker.implementation;

import com.microsoft.azure.cognitiveservices.knowledge.qnamaker.models.DownloadOptionalParameter;
import com.microsoft.azure.cognitiveservices.knowledge.qnamaker.models.TrainOptionalParameter;
import retrofit2.Retrofit;
import com.microsoft.azure.cognitiveservices.knowledge.qnamaker.Knowledgebases;
import com.google.common.base.Joiner;
import com.google.common.reflect.TypeToken;
import com.microsoft.azure.cognitiveservices.knowledge.qnamaker.models.CreateKbDTO;
import com.microsoft.azure.cognitiveservices.knowledge.qnamaker.models.EnvironmentType;
import com.microsoft.azure.cognitiveservices.knowledge.qnamaker.models.ErrorResponseException;
import com.microsoft.azure.cognitiveservices.knowledge.qnamaker.models.FeedbackRecordDTO;
import com.microsoft.azure.cognitiveservices.knowledge.qnamaker.models.FeedbackRecordsDTO;
import com.microsoft.azure.cognitiveservices.knowledge.qnamaker.models.KnowledgebaseDTO;
import com.microsoft.azure.cognitiveservices.knowledge.qnamaker.models.KnowledgebasesDTO;
import com.microsoft.azure.cognitiveservices.knowledge.qnamaker.models.KnowledgebaseUpdateHeaders;
import com.microsoft.azure.cognitiveservices.knowledge.qnamaker.models.Operation;
import com.microsoft.azure.cognitiveservices.knowledge.qnamaker.models.QnADocumentsDTO;
import com.microsoft.azure.cognitiveservices.knowledge.qnamaker.models.QnADTO;
import com.microsoft.azure.cognitiveservices.knowledge.qnamaker.models.QnASearchResultList;
import com.microsoft.azure.cognitiveservices.knowledge.qnamaker.models.QueryDTO;
import com.microsoft.azure.cognitiveservices.knowledge.qnamaker.models.ReplaceKbDTO;
import com.microsoft.azure.cognitiveservices.knowledge.qnamaker.models.UpdateKbOperationDTO;
import com.microsoft.rest.ServiceCallback;
import com.microsoft.rest.ServiceFuture;
import com.microsoft.rest.ServiceResponse;
import com.microsoft.rest.ServiceResponseWithHeaders;
import com.microsoft.rest.Validator;
import java.io.IOException;
import java.util.List;
import okhttp3.ResponseBody;
import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.Header;
import retrofit2.http.Headers;
import retrofit2.http.HTTP;
import retrofit2.http.PATCH;
import retrofit2.http.Path;
import retrofit2.http.POST;
import retrofit2.http.PUT;
import retrofit2.http.Query;
import retrofit2.Response;
import rx.functions.Func1;
import rx.Observable;

/**
 * An instance of this class provides access to all the operations defined
 * in Knowledgebases.
 */
public class KnowledgebasesImpl implements Knowledgebases {
    /** The Retrofit service to perform REST calls. */
    private KnowledgebasesService service;
    /** The service client containing this operation class. */
    private QnAMakerClientImpl client;

    /**
     * Initializes an instance of KnowledgebasesImpl.
     *
     * @param retrofit the Retrofit instance built from a Retrofit Builder.
     * @param client the instance of the service client containing this operation class.
     */
    public KnowledgebasesImpl(Retrofit retrofit, QnAMakerClientImpl client) {
        this.service = retrofit.create(KnowledgebasesService.class);
        this.client = client;
    }

    /**
     * The interface defining all the services for Knowledgebases to be
     * used by Retrofit to perform actually REST calls.
     */
    interface KnowledgebasesService {
        @Headers({ "Content-Type: application/json; charset=utf-8", "x-ms-logging-context: com.microsoft.azure.cognitiveservices.knowledge.qnamaker.Knowledgebases listAll" })
        @GET("knowledgebases")
        Observable<Response<ResponseBody>> listAll(@Header("accept-language") String acceptLanguage, @Header("x-ms-parameterized-host") String parameterizedHost, @Header("User-Agent") String userAgent);

        @Headers({ "Content-Type: application/json; charset=utf-8", "x-ms-logging-context: com.microsoft.azure.cognitiveservices.knowledge.qnamaker.Knowledgebases getDetails" })
        @GET("knowledgebases/{kbId}")
        Observable<Response<ResponseBody>> getDetails(@Path("kbId") String kbId, @Header("accept-language") String acceptLanguage, @Header("x-ms-parameterized-host") String parameterizedHost, @Header("User-Agent") String userAgent);

        @Headers({ "Content-Type: application/json; charset=utf-8", "x-ms-logging-context: com.microsoft.azure.cognitiveservices.knowledge.qnamaker.Knowledgebases delete" })
        @HTTP(path = "knowledgebases/{kbId}", method = "DELETE", hasBody = true)
        Observable<Response<ResponseBody>> delete(@Path("kbId") String kbId, @Header("accept-language") String acceptLanguage, @Header("x-ms-parameterized-host") String parameterizedHost, @Header("User-Agent") String userAgent);

        @Headers({ "Content-Type: application/json; charset=utf-8", "x-ms-logging-context: com.microsoft.azure.cognitiveservices.knowledge.qnamaker.Knowledgebases publish" })
        @POST("knowledgebases/{kbId}")
        Observable<Response<ResponseBody>> publish(@Path("kbId") String kbId, @Header("accept-language") String acceptLanguage, @Header("x-ms-parameterized-host") String parameterizedHost, @Header("User-Agent") String userAgent);

        @Headers({ "Content-Type: application/json; charset=utf-8", "x-ms-logging-context: com.microsoft.azure.cognitiveservices.knowledge.qnamaker.Knowledgebases replace" })
        @PUT("knowledgebases/{kbId}")
        Observable<Response<ResponseBody>> replace(@Path("kbId") String kbId, @Header("accept-language") String acceptLanguage, @Body ReplaceKbDTO replaceKb, @Header("x-ms-parameterized-host") String parameterizedHost, @Header("User-Agent") String userAgent);

        @Headers({ "Content-Type: application/json; charset=utf-8", "x-ms-logging-context: com.microsoft.azure.cognitiveservices.knowledge.qnamaker.Knowledgebases update" })
        @PATCH("knowledgebases/{kbId}")
        Observable<Response<ResponseBody>> update(@Path("kbId") String kbId, @Body UpdateKbOperationDTO updateKb, @Header("accept-language") String acceptLanguage, @Header("x-ms-parameterized-host") String parameterizedHost, @Header("User-Agent") String userAgent);

        @Headers({ "Content-Type: application/json; charset=utf-8", "x-ms-logging-context: com.microsoft.azure.cognitiveservices.knowledge.qnamaker.Knowledgebases create" })
        @POST("knowledgebases/create")
        Observable<Response<ResponseBody>> create(@Body CreateKbDTO createKbPayload, @Header("accept-language") String acceptLanguage, @Header("x-ms-parameterized-host") String parameterizedHost, @Header("User-Agent") String userAgent);

        @Headers({ "Content-Type: application/json; charset=utf-8", "x-ms-logging-context: com.microsoft.azure.cognitiveservices.knowledge.qnamaker.Knowledgebases download" })
        @GET("knowledgebases/{kbId}/{environment}/qna")
        Observable<Response<ResponseBody>> download(@Path("kbId") String kbId, @Path("environment") EnvironmentType environment, @Query("source") String source, @Query("changedSince") String changedSince, @Header("accept-language") String acceptLanguage, @Header("x-ms-parameterized-host") String parameterizedHost, @Header("User-Agent") String userAgent);

        @Headers({ "Content-Type: application/json; charset=utf-8", "x-ms-logging-context: com.microsoft.azure.cognitiveservices.knowledge.qnamaker.Knowledgebases generateAnswer" })
        @POST("knowledgebases/{kbId}/generateAnswer")
        Observable<Response<ResponseBody>> generateAnswer(@Path("kbId") String kbId, @Body QueryDTO generateAnswerPayload, @Header("accept-language") String acceptLanguage, @Header("x-ms-parameterized-host") String parameterizedHost, @Header("User-Agent") String userAgent);

        @Headers({ "Content-Type: application/json; charset=utf-8", "x-ms-logging-context: com.microsoft.azure.cognitiveservices.knowledge.qnamaker.Knowledgebases train" })
        @POST("knowledgebases/{kbId}/train")
        Observable<Response<ResponseBody>> train(@Path("kbId") String kbId, @Header("accept-language") String acceptLanguage, @Body FeedbackRecordsDTO trainPayload, @Header("x-ms-parameterized-host") String parameterizedHost, @Header("User-Agent") String userAgent);

    }

    /**
     * Gets all knowledgebases for a user.
     *
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @throws ErrorResponseException thrown if the request is rejected by server
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent
     * @return the KnowledgebasesDTO object if successful.
     */
    public KnowledgebasesDTO listAll() {
        return listAllWithServiceResponseAsync().toBlocking().single().body();
    }

    /**
     * Gets all knowledgebases for a user.
     *
     * @param serviceCallback the async ServiceCallback to handle successful and failed responses.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the {@link ServiceFuture} object
     */
    public ServiceFuture<KnowledgebasesDTO> listAllAsync(final ServiceCallback<KnowledgebasesDTO> serviceCallback) {
        return ServiceFuture.fromResponse(listAllWithServiceResponseAsync(), serviceCallback);
    }

    /**
     * Gets all knowledgebases for a user.
     *
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the observable to the KnowledgebasesDTO object
     */
    public Observable<KnowledgebasesDTO> listAllAsync() {
        return listAllWithServiceResponseAsync().map(new Func1<ServiceResponse<KnowledgebasesDTO>, KnowledgebasesDTO>() {
            @Override
            public KnowledgebasesDTO call(ServiceResponse<KnowledgebasesDTO> response) {
                return response.body();
            }
        });
    }

    /**
     * Gets all knowledgebases for a user.
     *
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the observable to the KnowledgebasesDTO object
     */
    public Observable<ServiceResponse<KnowledgebasesDTO>> listAllWithServiceResponseAsync() {
        if (this.client.endpoint() == null) {
            throw new IllegalArgumentException("Parameter this.client.endpoint() is required and cannot be null.");
        }
        String parameterizedHost = Joiner.on(", ").join("{Endpoint}", this.client.endpoint());
        return service.listAll(this.client.acceptLanguage(), parameterizedHost, this.client.userAgent())
            .flatMap(new Func1<Response<ResponseBody>, Observable<ServiceResponse<KnowledgebasesDTO>>>() {
                @Override
                public Observable<ServiceResponse<KnowledgebasesDTO>> call(Response<ResponseBody> response) {
                    try {
                        ServiceResponse<KnowledgebasesDTO> clientResponse = listAllDelegate(response);
                        return Observable.just(clientResponse);
                    } catch (Throwable t) {
                        return Observable.error(t);
                    }
                }
            });
    }

    private ServiceResponse<KnowledgebasesDTO> listAllDelegate(Response<ResponseBody> response) throws ErrorResponseException, IOException, IllegalArgumentException {
        return this.client.restClient().responseBuilderFactory().<KnowledgebasesDTO, ErrorResponseException>newInstance(this.client.serializerAdapter())
                .register(200, new TypeToken<KnowledgebasesDTO>() { }.getType())
                .registerError(ErrorResponseException.class)
                .build(response);
    }

    /**
     * Gets details of a specific knowledgebase.
     *
     * @param kbId Knowledgebase id.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @throws ErrorResponseException thrown if the request is rejected by server
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent
     * @return the KnowledgebaseDTO object if successful.
     */
    public KnowledgebaseDTO getDetails(String kbId) {
        return getDetailsWithServiceResponseAsync(kbId).toBlocking().single().body();
    }

    /**
     * Gets details of a specific knowledgebase.
     *
     * @param kbId Knowledgebase id.
     * @param serviceCallback the async ServiceCallback to handle successful and failed responses.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the {@link ServiceFuture} object
     */
    public ServiceFuture<KnowledgebaseDTO> getDetailsAsync(String kbId, final ServiceCallback<KnowledgebaseDTO> serviceCallback) {
        return ServiceFuture.fromResponse(getDetailsWithServiceResponseAsync(kbId), serviceCallback);
    }

    /**
     * Gets details of a specific knowledgebase.
     *
     * @param kbId Knowledgebase id.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the observable to the KnowledgebaseDTO object
     */
    public Observable<KnowledgebaseDTO> getDetailsAsync(String kbId) {
        return getDetailsWithServiceResponseAsync(kbId).map(new Func1<ServiceResponse<KnowledgebaseDTO>, KnowledgebaseDTO>() {
            @Override
            public KnowledgebaseDTO call(ServiceResponse<KnowledgebaseDTO> response) {
                return response.body();
            }
        });
    }

    /**
     * Gets details of a specific knowledgebase.
     *
     * @param kbId Knowledgebase id.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the observable to the KnowledgebaseDTO object
     */
    public Observable<ServiceResponse<KnowledgebaseDTO>> getDetailsWithServiceResponseAsync(String kbId) {
        if (this.client.endpoint() == null) {
            throw new IllegalArgumentException("Parameter this.client.endpoint() is required and cannot be null.");
        }
        if (kbId == null) {
            throw new IllegalArgumentException("Parameter kbId is required and cannot be null.");
        }
        String parameterizedHost = Joiner.on(", ").join("{Endpoint}", this.client.endpoint());
        return service.getDetails(kbId, this.client.acceptLanguage(), parameterizedHost, this.client.userAgent())
            .flatMap(new Func1<Response<ResponseBody>, Observable<ServiceResponse<KnowledgebaseDTO>>>() {
                @Override
                public Observable<ServiceResponse<KnowledgebaseDTO>> call(Response<ResponseBody> response) {
                    try {
                        ServiceResponse<KnowledgebaseDTO> clientResponse = getDetailsDelegate(response);
                        return Observable.just(clientResponse);
                    } catch (Throwable t) {
                        return Observable.error(t);
                    }
                }
            });
    }

    private ServiceResponse<KnowledgebaseDTO> getDetailsDelegate(Response<ResponseBody> response) throws ErrorResponseException, IOException, IllegalArgumentException {
        return this.client.restClient().responseBuilderFactory().<KnowledgebaseDTO, ErrorResponseException>newInstance(this.client.serializerAdapter())
                .register(200, new TypeToken<KnowledgebaseDTO>() { }.getType())
                .registerError(ErrorResponseException.class)
                .build(response);
    }

    /**
     * Deletes the knowledgebase and all its data.
     *
     * @param kbId Knowledgebase id.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @throws ErrorResponseException thrown if the request is rejected by server
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent
     */
    public void delete(String kbId) {
        deleteWithServiceResponseAsync(kbId).toBlocking().single().body();
    }

    /**
     * Deletes the knowledgebase and all its data.
     *
     * @param kbId Knowledgebase id.
     * @param serviceCallback the async ServiceCallback to handle successful and failed responses.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the {@link ServiceFuture} object
     */
    public ServiceFuture<Void> deleteAsync(String kbId, final ServiceCallback<Void> serviceCallback) {
        return ServiceFuture.fromResponse(deleteWithServiceResponseAsync(kbId), serviceCallback);
    }

    /**
     * Deletes the knowledgebase and all its data.
     *
     * @param kbId Knowledgebase id.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the {@link ServiceResponse} object if successful.
     */
    public Observable<Void> deleteAsync(String kbId) {
        return deleteWithServiceResponseAsync(kbId).map(new Func1<ServiceResponse<Void>, Void>() {
            @Override
            public Void call(ServiceResponse<Void> response) {
                return response.body();
            }
        });
    }

    /**
     * Deletes the knowledgebase and all its data.
     *
     * @param kbId Knowledgebase id.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the {@link ServiceResponse} object if successful.
     */
    public Observable<ServiceResponse<Void>> deleteWithServiceResponseAsync(String kbId) {
        if (this.client.endpoint() == null) {
            throw new IllegalArgumentException("Parameter this.client.endpoint() is required and cannot be null.");
        }
        if (kbId == null) {
            throw new IllegalArgumentException("Parameter kbId is required and cannot be null.");
        }
        String parameterizedHost = Joiner.on(", ").join("{Endpoint}", this.client.endpoint());
        return service.delete(kbId, this.client.acceptLanguage(), parameterizedHost, this.client.userAgent())
            .flatMap(new Func1<Response<ResponseBody>, Observable<ServiceResponse<Void>>>() {
                @Override
                public Observable<ServiceResponse<Void>> call(Response<ResponseBody> response) {
                    try {
                        ServiceResponse<Void> clientResponse = deleteDelegate(response);
                        return Observable.just(clientResponse);
                    } catch (Throwable t) {
                        return Observable.error(t);
                    }
                }
            });
    }

    private ServiceResponse<Void> deleteDelegate(Response<ResponseBody> response) throws ErrorResponseException, IOException, IllegalArgumentException {
        return this.client.restClient().responseBuilderFactory().<Void, ErrorResponseException>newInstance(this.client.serializerAdapter())
                .register(204, new TypeToken<Void>() { }.getType())
                .registerError(ErrorResponseException.class)
                .build(response);
    }

    /**
     * Publishes all changes in test index of a knowledgebase to its prod index.
     *
     * @param kbId Knowledgebase id.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @throws ErrorResponseException thrown if the request is rejected by server
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent
     */
    public void publish(String kbId) {
        publishWithServiceResponseAsync(kbId).toBlocking().single().body();
    }

    /**
     * Publishes all changes in test index of a knowledgebase to its prod index.
     *
     * @param kbId Knowledgebase id.
     * @param serviceCallback the async ServiceCallback to handle successful and failed responses.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the {@link ServiceFuture} object
     */
    public ServiceFuture<Void> publishAsync(String kbId, final ServiceCallback<Void> serviceCallback) {
        return ServiceFuture.fromResponse(publishWithServiceResponseAsync(kbId), serviceCallback);
    }

    /**
     * Publishes all changes in test index of a knowledgebase to its prod index.
     *
     * @param kbId Knowledgebase id.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the {@link ServiceResponse} object if successful.
     */
    public Observable<Void> publishAsync(String kbId) {
        return publishWithServiceResponseAsync(kbId).map(new Func1<ServiceResponse<Void>, Void>() {
            @Override
            public Void call(ServiceResponse<Void> response) {
                return response.body();
            }
        });
    }

    /**
     * Publishes all changes in test index of a knowledgebase to its prod index.
     *
     * @param kbId Knowledgebase id.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the {@link ServiceResponse} object if successful.
     */
    public Observable<ServiceResponse<Void>> publishWithServiceResponseAsync(String kbId) {
        if (this.client.endpoint() == null) {
            throw new IllegalArgumentException("Parameter this.client.endpoint() is required and cannot be null.");
        }
        if (kbId == null) {
            throw new IllegalArgumentException("Parameter kbId is required and cannot be null.");
        }
        String parameterizedHost = Joiner.on(", ").join("{Endpoint}", this.client.endpoint());
        return service.publish(kbId, this.client.acceptLanguage(), parameterizedHost, this.client.userAgent())
            .flatMap(new Func1<Response<ResponseBody>, Observable<ServiceResponse<Void>>>() {
                @Override
                public Observable<ServiceResponse<Void>> call(Response<ResponseBody> response) {
                    try {
                        ServiceResponse<Void> clientResponse = publishDelegate(response);
                        return Observable.just(clientResponse);
                    } catch (Throwable t) {
                        return Observable.error(t);
                    }
                }
            });
    }

    private ServiceResponse<Void> publishDelegate(Response<ResponseBody> response) throws ErrorResponseException, IOException, IllegalArgumentException {
        return this.client.restClient().responseBuilderFactory().<Void, ErrorResponseException>newInstance(this.client.serializerAdapter())
                .register(204, new TypeToken<Void>() { }.getType())
                .registerError(ErrorResponseException.class)
                .build(response);
    }

    /**
     * Replace knowledgebase contents.
     *
     * @param kbId Knowledgebase id.
     * @param qnAList List of Q-A (QnADTO) to be added to the knowledgebase. Q-A Ids are assigned by the service and should be omitted.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @throws ErrorResponseException thrown if the request is rejected by server
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent
     */
    public void replace(String kbId, List<QnADTO> qnAList) {
        replaceWithServiceResponseAsync(kbId, qnAList).toBlocking().single().body();
    }

    /**
     * Replace knowledgebase contents.
     *
     * @param kbId Knowledgebase id.
     * @param qnAList List of Q-A (QnADTO) to be added to the knowledgebase. Q-A Ids are assigned by the service and should be omitted.
     * @param serviceCallback the async ServiceCallback to handle successful and failed responses.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the {@link ServiceFuture} object
     */
    public ServiceFuture<Void> replaceAsync(String kbId, List<QnADTO> qnAList, final ServiceCallback<Void> serviceCallback) {
        return ServiceFuture.fromResponse(replaceWithServiceResponseAsync(kbId, qnAList), serviceCallback);
    }

    /**
     * Replace knowledgebase contents.
     *
     * @param kbId Knowledgebase id.
     * @param qnAList List of Q-A (QnADTO) to be added to the knowledgebase. Q-A Ids are assigned by the service and should be omitted.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the {@link ServiceResponse} object if successful.
     */
    public Observable<Void> replaceAsync(String kbId, List<QnADTO> qnAList) {
        return replaceWithServiceResponseAsync(kbId, qnAList).map(new Func1<ServiceResponse<Void>, Void>() {
            @Override
            public Void call(ServiceResponse<Void> response) {
                return response.body();
            }
        });
    }

    /**
     * Replace knowledgebase contents.
     *
     * @param kbId Knowledgebase id.
     * @param qnAList List of Q-A (QnADTO) to be added to the knowledgebase. Q-A Ids are assigned by the service and should be omitted.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the {@link ServiceResponse} object if successful.
     */
    public Observable<ServiceResponse<Void>> replaceWithServiceResponseAsync(String kbId, List<QnADTO> qnAList) {
        if (this.client.endpoint() == null) {
            throw new IllegalArgumentException("Parameter this.client.endpoint() is required and cannot be null.");
        }
        if (kbId == null) {
            throw new IllegalArgumentException("Parameter kbId is required and cannot be null.");
        }
        if (qnAList == null) {
            throw new IllegalArgumentException("Parameter qnAList is required and cannot be null.");
        }
        Validator.validate(qnAList);
        ReplaceKbDTO replaceKb = new ReplaceKbDTO();
        replaceKb.withQnAList(qnAList);
        String parameterizedHost = Joiner.on(", ").join("{Endpoint}", this.client.endpoint());
        return service.replace(kbId, this.client.acceptLanguage(), replaceKb, parameterizedHost, this.client.userAgent())
            .flatMap(new Func1<Response<ResponseBody>, Observable<ServiceResponse<Void>>>() {
                @Override
                public Observable<ServiceResponse<Void>> call(Response<ResponseBody> response) {
                    try {
                        ServiceResponse<Void> clientResponse = replaceDelegate(response);
                        return Observable.just(clientResponse);
                    } catch (Throwable t) {
                        return Observable.error(t);
                    }
                }
            });
    }

    private ServiceResponse<Void> replaceDelegate(Response<ResponseBody> response) throws ErrorResponseException, IOException, IllegalArgumentException {
        return this.client.restClient().responseBuilderFactory().<Void, ErrorResponseException>newInstance(this.client.serializerAdapter())
                .register(204, new TypeToken<Void>() { }.getType())
                .registerError(ErrorResponseException.class)
                .build(response);
    }

    /**
     * Asynchronous operation to modify a knowledgebase.
     *
     * @param kbId Knowledgebase id.
     * @param updateKb Post body of the request.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @throws ErrorResponseException thrown if the request is rejected by server
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent
     * @return the Operation object if successful.
     */
    public Operation update(String kbId, UpdateKbOperationDTO updateKb) {
        return updateWithServiceResponseAsync(kbId, updateKb).toBlocking().single().body();
    }

    /**
     * Asynchronous operation to modify a knowledgebase.
     *
     * @param kbId Knowledgebase id.
     * @param updateKb Post body of the request.
     * @param serviceCallback the async ServiceCallback to handle successful and failed responses.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the {@link ServiceFuture} object
     */
    public ServiceFuture<Operation> updateAsync(String kbId, UpdateKbOperationDTO updateKb, final ServiceCallback<Operation> serviceCallback) {
        return ServiceFuture.fromHeaderResponse(updateWithServiceResponseAsync(kbId, updateKb), serviceCallback);
    }

    /**
     * Asynchronous operation to modify a knowledgebase.
     *
     * @param kbId Knowledgebase id.
     * @param updateKb Post body of the request.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the observable to the Operation object
     */
    public Observable<Operation> updateAsync(String kbId, UpdateKbOperationDTO updateKb) {
        return updateWithServiceResponseAsync(kbId, updateKb).map(new Func1<ServiceResponseWithHeaders<Operation, KnowledgebaseUpdateHeaders>, Operation>() {
            @Override
            public Operation call(ServiceResponseWithHeaders<Operation, KnowledgebaseUpdateHeaders> response) {
                return response.body();
            }
        });
    }

    /**
     * Asynchronous operation to modify a knowledgebase.
     *
     * @param kbId Knowledgebase id.
     * @param updateKb Post body of the request.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the observable to the Operation object
     */
    public Observable<ServiceResponseWithHeaders<Operation, KnowledgebaseUpdateHeaders>> updateWithServiceResponseAsync(String kbId, UpdateKbOperationDTO updateKb) {
        if (this.client.endpoint() == null) {
            throw new IllegalArgumentException("Parameter this.client.endpoint() is required and cannot be null.");
        }
        if (kbId == null) {
            throw new IllegalArgumentException("Parameter kbId is required and cannot be null.");
        }
        if (updateKb == null) {
            throw new IllegalArgumentException("Parameter updateKb is required and cannot be null.");
        }
        Validator.validate(updateKb);
        String parameterizedHost = Joiner.on(", ").join("{Endpoint}", this.client.endpoint());
        return service.update(kbId, updateKb, this.client.acceptLanguage(), parameterizedHost, this.client.userAgent())
            .flatMap(new Func1<Response<ResponseBody>, Observable<ServiceResponseWithHeaders<Operation, KnowledgebaseUpdateHeaders>>>() {
                @Override
                public Observable<ServiceResponseWithHeaders<Operation, KnowledgebaseUpdateHeaders>> call(Response<ResponseBody> response) {
                    try {
                        ServiceResponseWithHeaders<Operation, KnowledgebaseUpdateHeaders> clientResponse = updateDelegate(response);
                        return Observable.just(clientResponse);
                    } catch (Throwable t) {
                        return Observable.error(t);
                    }
                }
            });
    }

    private ServiceResponseWithHeaders<Operation, KnowledgebaseUpdateHeaders> updateDelegate(Response<ResponseBody> response) throws ErrorResponseException, IOException, IllegalArgumentException {
        return this.client.restClient().responseBuilderFactory().<Operation, ErrorResponseException>newInstance(this.client.serializerAdapter())
                .register(202, new TypeToken<Operation>() { }.getType())
                .registerError(ErrorResponseException.class)
                .buildWithHeaders(response, KnowledgebaseUpdateHeaders.class);
    }

    /**
     * Asynchronous operation to create a new knowledgebase.
     *
     * @param createKbPayload Post body of the request.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @throws ErrorResponseException thrown if the request is rejected by server
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent
     * @return the Operation object if successful.
     */
    public Operation create(CreateKbDTO createKbPayload) {
        return createWithServiceResponseAsync(createKbPayload).toBlocking().single().body();
    }

    /**
     * Asynchronous operation to create a new knowledgebase.
     *
     * @param createKbPayload Post body of the request.
     * @param serviceCallback the async ServiceCallback to handle successful and failed responses.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the {@link ServiceFuture} object
     */
    public ServiceFuture<Operation> createAsync(CreateKbDTO createKbPayload, final ServiceCallback<Operation> serviceCallback) {
        return ServiceFuture.fromResponse(createWithServiceResponseAsync(createKbPayload), serviceCallback);
    }

    /**
     * Asynchronous operation to create a new knowledgebase.
     *
     * @param createKbPayload Post body of the request.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the observable to the Operation object
     */
    public Observable<Operation> createAsync(CreateKbDTO createKbPayload) {
        return createWithServiceResponseAsync(createKbPayload).map(new Func1<ServiceResponse<Operation>, Operation>() {
            @Override
            public Operation call(ServiceResponse<Operation> response) {
                return response.body();
            }
        });
    }

    /**
     * Asynchronous operation to create a new knowledgebase.
     *
     * @param createKbPayload Post body of the request.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the observable to the Operation object
     */
    public Observable<ServiceResponse<Operation>> createWithServiceResponseAsync(CreateKbDTO createKbPayload) {
        if (this.client.endpoint() == null) {
            throw new IllegalArgumentException("Parameter this.client.endpoint() is required and cannot be null.");
        }
        if (createKbPayload == null) {
            throw new IllegalArgumentException("Parameter createKbPayload is required and cannot be null.");
        }
        Validator.validate(createKbPayload);
        String parameterizedHost = Joiner.on(", ").join("{Endpoint}", this.client.endpoint());
        return service.create(createKbPayload, this.client.acceptLanguage(), parameterizedHost, this.client.userAgent())
            .flatMap(new Func1<Response<ResponseBody>, Observable<ServiceResponse<Operation>>>() {
                @Override
                public Observable<ServiceResponse<Operation>> call(Response<ResponseBody> response) {
                    try {
                        ServiceResponse<Operation> clientResponse = createDelegate(response);
                        return Observable.just(clientResponse);
                    } catch (Throwable t) {
                        return Observable.error(t);
                    }
                }
            });
    }

    private ServiceResponse<Operation> createDelegate(Response<ResponseBody> response) throws ErrorResponseException, IOException, IllegalArgumentException {
        return this.client.restClient().responseBuilderFactory().<Operation, ErrorResponseException>newInstance(this.client.serializerAdapter())
                .register(202, new TypeToken<Operation>() { }.getType())
                .registerError(ErrorResponseException.class)
                .build(response);
    }


    /**
     * Download the knowledgebase.
     *
     * @param kbId Knowledgebase id.
     * @param environment Specifies whether environment is Test or Prod. Possible values include: 'Prod', 'Test'
     * @param downloadOptionalParameter the object representing the optional parameters to be set before calling this API
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @throws ErrorResponseException thrown if the request is rejected by server
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent
     * @return the QnADocumentsDTO object if successful.
     */
    public QnADocumentsDTO download(String kbId, EnvironmentType environment, DownloadOptionalParameter downloadOptionalParameter) {
        return downloadWithServiceResponseAsync(kbId, environment, downloadOptionalParameter).toBlocking().single().body();
    }

    /**
     * Download the knowledgebase.
     *
     * @param kbId Knowledgebase id.
     * @param environment Specifies whether environment is Test or Prod. Possible values include: 'Prod', 'Test'
     * @param downloadOptionalParameter the object representing the optional parameters to be set before calling this API
     * @param serviceCallback the async ServiceCallback to handle successful and failed responses.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the {@link ServiceFuture} object
     */
    public ServiceFuture<QnADocumentsDTO> downloadAsync(String kbId, EnvironmentType environment, DownloadOptionalParameter downloadOptionalParameter, final ServiceCallback<QnADocumentsDTO> serviceCallback) {
        return ServiceFuture.fromResponse(downloadWithServiceResponseAsync(kbId, environment, downloadOptionalParameter), serviceCallback);
    }

    /**
     * Download the knowledgebase.
     *
     * @param kbId Knowledgebase id.
     * @param environment Specifies whether environment is Test or Prod. Possible values include: 'Prod', 'Test'
     * @param downloadOptionalParameter the object representing the optional parameters to be set before calling this API
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the observable to the QnADocumentsDTO object
     */
    public Observable<QnADocumentsDTO> downloadAsync(String kbId, EnvironmentType environment, DownloadOptionalParameter downloadOptionalParameter) {
        return downloadWithServiceResponseAsync(kbId, environment, downloadOptionalParameter).map(new Func1<ServiceResponse<QnADocumentsDTO>, QnADocumentsDTO>() {
            @Override
            public QnADocumentsDTO call(ServiceResponse<QnADocumentsDTO> response) {
                return response.body();
            }
        });
    }

    /**
     * Download the knowledgebase.
     *
     * @param kbId Knowledgebase id.
     * @param environment Specifies whether environment is Test or Prod. Possible values include: 'Prod', 'Test'
     * @param downloadOptionalParameter the object representing the optional parameters to be set before calling this API
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the observable to the QnADocumentsDTO object
     */
    public Observable<ServiceResponse<QnADocumentsDTO>> downloadWithServiceResponseAsync(String kbId, EnvironmentType environment, DownloadOptionalParameter downloadOptionalParameter) {
        if (this.client.endpoint() == null) {
            throw new IllegalArgumentException("Parameter this.client.endpoint() is required and cannot be null.");
        }
        if (kbId == null) {
            throw new IllegalArgumentException("Parameter kbId is required and cannot be null.");
        }
        if (environment == null) {
            throw new IllegalArgumentException("Parameter environment is required and cannot be null.");
        }
        final String source = downloadOptionalParameter != null ? downloadOptionalParameter.source() : null;
        final String changedSince = downloadOptionalParameter != null ? downloadOptionalParameter.changedSince() : null;

        return downloadWithServiceResponseAsync(kbId, environment, source, changedSince);
    }

    /**
     * Download the knowledgebase.
     *
     * @param kbId Knowledgebase id.
     * @param environment Specifies whether environment is Test or Prod. Possible values include: 'Prod', 'Test'
     * @param source The source property filter to apply.
     * @param changedSince The last changed status property filter to apply.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the observable to the QnADocumentsDTO object
     */
    public Observable<ServiceResponse<QnADocumentsDTO>> downloadWithServiceResponseAsync(String kbId, EnvironmentType environment, String source, String changedSince) {
        if (this.client.endpoint() == null) {
            throw new IllegalArgumentException("Parameter this.client.endpoint() is required and cannot be null.");
        }
        if (kbId == null) {
            throw new IllegalArgumentException("Parameter kbId is required and cannot be null.");
        }
        if (environment == null) {
            throw new IllegalArgumentException("Parameter environment is required and cannot be null.");
        }
        String parameterizedHost = Joiner.on(", ").join("{Endpoint}", this.client.endpoint());
        return service.download(kbId, environment, source, changedSince, this.client.acceptLanguage(), parameterizedHost, this.client.userAgent())
            .flatMap(new Func1<Response<ResponseBody>, Observable<ServiceResponse<QnADocumentsDTO>>>() {
                @Override
                public Observable<ServiceResponse<QnADocumentsDTO>> call(Response<ResponseBody> response) {
                    try {
                        ServiceResponse<QnADocumentsDTO> clientResponse = downloadDelegate(response);
                        return Observable.just(clientResponse);
                    } catch (Throwable t) {
                        return Observable.error(t);
                    }
                }
            });
    }

    private ServiceResponse<QnADocumentsDTO> downloadDelegate(Response<ResponseBody> response) throws ErrorResponseException, IOException, IllegalArgumentException {
        return this.client.restClient().responseBuilderFactory().<QnADocumentsDTO, ErrorResponseException>newInstance(this.client.serializerAdapter())
                .register(200, new TypeToken<QnADocumentsDTO>() { }.getType())
                .registerError(ErrorResponseException.class)
                .build(response);
    }

    @Override
    public KnowledgebasesDownloadParameters download() {
        return new KnowledgebasesDownloadParameters(this);
    }

    /**
     * Internal class implementing KnowledgebasesDownloadDefinition.
     */
    class KnowledgebasesDownloadParameters implements KnowledgebasesDownloadDefinition {
        private KnowledgebasesImpl parent;
        private String kbId;
        private EnvironmentType environment;
        private String source;
        private String changedSince;

        /**
         * Constructor.
         * @param parent the parent object.
         */
        KnowledgebasesDownloadParameters(KnowledgebasesImpl parent) {
            this.parent = parent;
        }

        @Override
        public KnowledgebasesDownloadParameters withKbId(String kbId) {
            this.kbId = kbId;
            return this;
        }

        @Override
        public KnowledgebasesDownloadParameters withEnvironment(EnvironmentType environment) {
            this.environment = environment;
            return this;
        }

        @Override
        public KnowledgebasesDownloadParameters withSource(String source) {
            this.source = source;
            return this;
        }

        @Override
        public KnowledgebasesDownloadParameters withChangedSince(String changedSince) {
            this.changedSince = changedSince;
            return this;
        }

        @Override
        public QnADocumentsDTO execute() {
        return downloadWithServiceResponseAsync(kbId, environment, source, changedSince).toBlocking().single().body();
    }

        @Override
        public Observable<QnADocumentsDTO> executeAsync() {
            return downloadWithServiceResponseAsync(kbId, environment, source, changedSince).map(new Func1<ServiceResponse<QnADocumentsDTO>, QnADocumentsDTO>() {
                @Override
                public QnADocumentsDTO call(ServiceResponse<QnADocumentsDTO> response) {
                    return response.body();
                }
            });
        }
    }

    /**
     * GenerateAnswer call to query knowledgebase (QnA Maker Managed).
     *
     * @param kbId Knowledgebase id.
     * @param generateAnswerPayload Post body of the request.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @throws ErrorResponseException thrown if the request is rejected by server
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent
     * @return the QnASearchResultList object if successful.
     */
    public QnASearchResultList generateAnswer(String kbId, QueryDTO generateAnswerPayload) {
        return generateAnswerWithServiceResponseAsync(kbId, generateAnswerPayload).toBlocking().single().body();
    }

    /**
     * GenerateAnswer call to query knowledgebase (QnA Maker Managed).
     *
     * @param kbId Knowledgebase id.
     * @param generateAnswerPayload Post body of the request.
     * @param serviceCallback the async ServiceCallback to handle successful and failed responses.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the {@link ServiceFuture} object
     */
    public ServiceFuture<QnASearchResultList> generateAnswerAsync(String kbId, QueryDTO generateAnswerPayload, final ServiceCallback<QnASearchResultList> serviceCallback) {
        return ServiceFuture.fromResponse(generateAnswerWithServiceResponseAsync(kbId, generateAnswerPayload), serviceCallback);
    }

    /**
     * GenerateAnswer call to query knowledgebase (QnA Maker Managed).
     *
     * @param kbId Knowledgebase id.
     * @param generateAnswerPayload Post body of the request.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the observable to the QnASearchResultList object
     */
    public Observable<QnASearchResultList> generateAnswerAsync(String kbId, QueryDTO generateAnswerPayload) {
        return generateAnswerWithServiceResponseAsync(kbId, generateAnswerPayload).map(new Func1<ServiceResponse<QnASearchResultList>, QnASearchResultList>() {
            @Override
            public QnASearchResultList call(ServiceResponse<QnASearchResultList> response) {
                return response.body();
            }
        });
    }

    /**
     * GenerateAnswer call to query knowledgebase (QnA Maker Managed).
     *
     * @param kbId Knowledgebase id.
     * @param generateAnswerPayload Post body of the request.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the observable to the QnASearchResultList object
     */
    public Observable<ServiceResponse<QnASearchResultList>> generateAnswerWithServiceResponseAsync(String kbId, QueryDTO generateAnswerPayload) {
        if (this.client.endpoint() == null) {
            throw new IllegalArgumentException("Parameter this.client.endpoint() is required and cannot be null.");
        }
        if (kbId == null) {
            throw new IllegalArgumentException("Parameter kbId is required and cannot be null.");
        }
        if (generateAnswerPayload == null) {
            throw new IllegalArgumentException("Parameter generateAnswerPayload is required and cannot be null.");
        }
        Validator.validate(generateAnswerPayload);
        String parameterizedHost = Joiner.on(", ").join("{Endpoint}", this.client.endpoint());
        return service.generateAnswer(kbId, generateAnswerPayload, this.client.acceptLanguage(), parameterizedHost, this.client.userAgent())
            .flatMap(new Func1<Response<ResponseBody>, Observable<ServiceResponse<QnASearchResultList>>>() {
                @Override
                public Observable<ServiceResponse<QnASearchResultList>> call(Response<ResponseBody> response) {
                    try {
                        ServiceResponse<QnASearchResultList> clientResponse = generateAnswerDelegate(response);
                        return Observable.just(clientResponse);
                    } catch (Throwable t) {
                        return Observable.error(t);
                    }
                }
            });
    }

    private ServiceResponse<QnASearchResultList> generateAnswerDelegate(Response<ResponseBody> response) throws ErrorResponseException, IOException, IllegalArgumentException {
        return this.client.restClient().responseBuilderFactory().<QnASearchResultList, ErrorResponseException>newInstance(this.client.serializerAdapter())
                .register(200, new TypeToken<QnASearchResultList>() { }.getType())
                .registerError(ErrorResponseException.class)
                .build(response);
    }


    /**
     * Train call to add suggestions to knowledgebase (QnAMaker Managed).
     *
     * @param kbId Knowledgebase id.
     * @param trainOptionalParameter the object representing the optional parameters to be set before calling this API
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @throws ErrorResponseException thrown if the request is rejected by server
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent
     */
    public void train(String kbId, TrainOptionalParameter trainOptionalParameter) {
        trainWithServiceResponseAsync(kbId, trainOptionalParameter).toBlocking().single().body();
    }

    /**
     * Train call to add suggestions to knowledgebase (QnAMaker Managed).
     *
     * @param kbId Knowledgebase id.
     * @param trainOptionalParameter the object representing the optional parameters to be set before calling this API
     * @param serviceCallback the async ServiceCallback to handle successful and failed responses.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the {@link ServiceFuture} object
     */
    public ServiceFuture<Void> trainAsync(String kbId, TrainOptionalParameter trainOptionalParameter, final ServiceCallback<Void> serviceCallback) {
        return ServiceFuture.fromResponse(trainWithServiceResponseAsync(kbId, trainOptionalParameter), serviceCallback);
    }

    /**
     * Train call to add suggestions to knowledgebase (QnAMaker Managed).
     *
     * @param kbId Knowledgebase id.
     * @param trainOptionalParameter the object representing the optional parameters to be set before calling this API
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the {@link ServiceResponse} object if successful.
     */
    public Observable<Void> trainAsync(String kbId, TrainOptionalParameter trainOptionalParameter) {
        return trainWithServiceResponseAsync(kbId, trainOptionalParameter).map(new Func1<ServiceResponse<Void>, Void>() {
            @Override
            public Void call(ServiceResponse<Void> response) {
                return response.body();
            }
        });
    }

    /**
     * Train call to add suggestions to knowledgebase (QnAMaker Managed).
     *
     * @param kbId Knowledgebase id.
     * @param trainOptionalParameter the object representing the optional parameters to be set before calling this API
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the {@link ServiceResponse} object if successful.
     */
    public Observable<ServiceResponse<Void>> trainWithServiceResponseAsync(String kbId, TrainOptionalParameter trainOptionalParameter) {
        if (this.client.endpoint() == null) {
            throw new IllegalArgumentException("Parameter this.client.endpoint() is required and cannot be null.");
        }
        if (kbId == null) {
            throw new IllegalArgumentException("Parameter kbId is required and cannot be null.");
        }
        final List<FeedbackRecordDTO> feedbackRecords = trainOptionalParameter != null ? trainOptionalParameter.feedbackRecords() : null;

        return trainWithServiceResponseAsync(kbId, feedbackRecords);
    }

    /**
     * Train call to add suggestions to knowledgebase (QnAMaker Managed).
     *
     * @param kbId Knowledgebase id.
     * @param feedbackRecords List of feedback records.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the {@link ServiceResponse} object if successful.
     */
    public Observable<ServiceResponse<Void>> trainWithServiceResponseAsync(String kbId, List<FeedbackRecordDTO> feedbackRecords) {
        if (this.client.endpoint() == null) {
            throw new IllegalArgumentException("Parameter this.client.endpoint() is required and cannot be null.");
        }
        if (kbId == null) {
            throw new IllegalArgumentException("Parameter kbId is required and cannot be null.");
        }
        Validator.validate(feedbackRecords);
        FeedbackRecordsDTO trainPayload = new FeedbackRecordsDTO();
        trainPayload.withFeedbackRecords(feedbackRecords);
        String parameterizedHost = Joiner.on(", ").join("{Endpoint}", this.client.endpoint());
        return service.train(kbId, this.client.acceptLanguage(), trainPayload, parameterizedHost, this.client.userAgent())
            .flatMap(new Func1<Response<ResponseBody>, Observable<ServiceResponse<Void>>>() {
                @Override
                public Observable<ServiceResponse<Void>> call(Response<ResponseBody> response) {
                    try {
                        ServiceResponse<Void> clientResponse = trainDelegate(response);
                        return Observable.just(clientResponse);
                    } catch (Throwable t) {
                        return Observable.error(t);
                    }
                }
            });
    }

    private ServiceResponse<Void> trainDelegate(Response<ResponseBody> response) throws ErrorResponseException, IOException, IllegalArgumentException {
        return this.client.restClient().responseBuilderFactory().<Void, ErrorResponseException>newInstance(this.client.serializerAdapter())
                .register(204, new TypeToken<Void>() { }.getType())
                .registerError(ErrorResponseException.class)
                .build(response);
    }

    @Override
    public KnowledgebasesTrainParameters train() {
        return new KnowledgebasesTrainParameters(this);
    }

    /**
     * Internal class implementing KnowledgebasesTrainDefinition.
     */
    class KnowledgebasesTrainParameters implements KnowledgebasesTrainDefinition {
        private KnowledgebasesImpl parent;
        private String kbId;
        private List<FeedbackRecordDTO> feedbackRecords;

        /**
         * Constructor.
         * @param parent the parent object.
         */
        KnowledgebasesTrainParameters(KnowledgebasesImpl parent) {
            this.parent = parent;
        }

        @Override
        public KnowledgebasesTrainParameters withKbId(String kbId) {
            this.kbId = kbId;
            return this;
        }

        @Override
        public KnowledgebasesTrainParameters withFeedbackRecords(List<FeedbackRecordDTO> feedbackRecords) {
            this.feedbackRecords = feedbackRecords;
            return this;
        }

        @Override
        public void execute() {
        trainWithServiceResponseAsync(kbId, feedbackRecords).toBlocking().single().body();
    }

        @Override
        public Observable<Void> executeAsync() {
            return trainWithServiceResponseAsync(kbId, feedbackRecords).map(new Func1<ServiceResponse<Void>, Void>() {
                @Override
                public Void call(ServiceResponse<Void> response) {
                    return response.body();
                }
            });
        }
    }

}
