/**
 * 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.management.automation.v2015_10_31.implementation;

import retrofit2.Retrofit;
import com.google.common.reflect.TypeToken;
import com.microsoft.azure.AzureServiceFuture;
import com.microsoft.azure.CloudException;
import com.microsoft.azure.ListOperationCallback;
import com.microsoft.azure.management.automation.v2015_10_31.ErrorResponseException;
import com.microsoft.azure.management.automation.v2015_10_31.RunbookCreateOrUpdateParameters;
import com.microsoft.azure.management.automation.v2015_10_31.RunbookUpdateParameters;
import com.microsoft.azure.Page;
import com.microsoft.azure.PagedList;
import com.microsoft.rest.ServiceCallback;
import com.microsoft.rest.ServiceFuture;
import com.microsoft.rest.ServiceResponse;
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.PUT;
import retrofit2.http.Query;
import retrofit2.http.Url;
import retrofit2.Response;
import rx.functions.Func1;
import rx.Observable;

/**
 * An instance of this class provides access to all the operations defined
 * in Runbooks.
 */
public class RunbooksInner {
    /** The Retrofit service to perform REST calls. */
    private RunbooksService service;
    /** The service client containing this operation class. */
    private AutomationClientImpl client;

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

    /**
     * The interface defining all the services for Runbooks to be
     * used by Retrofit to perform actually REST calls.
     */
    interface RunbooksService {
        @Headers({ "Content-Type: application/json; charset=utf-8", "x-ms-logging-context: com.microsoft.azure.management.automation.v2015_10_31.Runbooks getContent" })
        @GET("subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Automation/automationAccounts/{automationAccountName}/runbooks/{runbookName}/content")
        Observable<Response<ResponseBody>> getContent(@Path("subscriptionId") String subscriptionId, @Path("resourceGroupName") String resourceGroupName, @Path("automationAccountName") String automationAccountName, @Path("runbookName") String runbookName, @Query("api-version") String apiVersion, @Header("accept-language") String acceptLanguage, @Header("User-Agent") String userAgent);

        @Headers({ "Content-Type: application/json; charset=utf-8", "x-ms-logging-context: com.microsoft.azure.management.automation.v2015_10_31.Runbooks get" })
        @GET("subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Automation/automationAccounts/{automationAccountName}/runbooks/{runbookName}")
        Observable<Response<ResponseBody>> get(@Path("subscriptionId") String subscriptionId, @Path("resourceGroupName") String resourceGroupName, @Path("automationAccountName") String automationAccountName, @Path("runbookName") String runbookName, @Query("api-version") String apiVersion, @Header("accept-language") String acceptLanguage, @Header("User-Agent") String userAgent);

        @Headers({ "Content-Type: application/json; charset=utf-8", "x-ms-logging-context: com.microsoft.azure.management.automation.v2015_10_31.Runbooks createOrUpdate" })
        @PUT("subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Automation/automationAccounts/{automationAccountName}/runbooks/{runbookName}")
        Observable<Response<ResponseBody>> createOrUpdate(@Path("subscriptionId") String subscriptionId, @Path("resourceGroupName") String resourceGroupName, @Path("automationAccountName") String automationAccountName, @Path("runbookName") String runbookName, @Body RunbookCreateOrUpdateParameters parameters, @Query("api-version") String apiVersion, @Header("accept-language") String acceptLanguage, @Header("User-Agent") String userAgent);

        @Headers({ "Content-Type: application/json; charset=utf-8", "x-ms-logging-context: com.microsoft.azure.management.automation.v2015_10_31.Runbooks update" })
        @PATCH("subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Automation/automationAccounts/{automationAccountName}/runbooks/{runbookName}")
        Observable<Response<ResponseBody>> update(@Path("subscriptionId") String subscriptionId, @Path("resourceGroupName") String resourceGroupName, @Path("automationAccountName") String automationAccountName, @Path("runbookName") String runbookName, @Body RunbookUpdateParameters parameters, @Query("api-version") String apiVersion, @Header("accept-language") String acceptLanguage, @Header("User-Agent") String userAgent);

        @Headers({ "Content-Type: application/json; charset=utf-8", "x-ms-logging-context: com.microsoft.azure.management.automation.v2015_10_31.Runbooks delete" })
        @HTTP(path = "subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Automation/automationAccounts/{automationAccountName}/runbooks/{runbookName}", method = "DELETE", hasBody = true)
        Observable<Response<ResponseBody>> delete(@Path("subscriptionId") String subscriptionId, @Path("resourceGroupName") String resourceGroupName, @Path("automationAccountName") String automationAccountName, @Path("runbookName") String runbookName, @Query("api-version") String apiVersion, @Header("accept-language") String acceptLanguage, @Header("User-Agent") String userAgent);

        @Headers({ "Content-Type: application/json; charset=utf-8", "x-ms-logging-context: com.microsoft.azure.management.automation.v2015_10_31.Runbooks listByAutomationAccount" })
        @GET("subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Automation/automationAccounts/{automationAccountName}/runbooks")
        Observable<Response<ResponseBody>> listByAutomationAccount(@Path("subscriptionId") String subscriptionId, @Path("resourceGroupName") String resourceGroupName, @Path("automationAccountName") String automationAccountName, @Query("api-version") String apiVersion, @Header("accept-language") String acceptLanguage, @Header("User-Agent") String userAgent);

        @Headers({ "Content-Type: application/json; charset=utf-8", "x-ms-logging-context: com.microsoft.azure.management.automation.v2015_10_31.Runbooks listByAutomationAccountNext" })
        @GET
        Observable<Response<ResponseBody>> listByAutomationAccountNext(@Url String nextUrl, @Header("accept-language") String acceptLanguage, @Header("User-Agent") String userAgent);

    }

    /**
     * Retrieve the content of runbook identified by runbook name.
     *
     * @param resourceGroupName Name of an Azure Resource group.
     * @param automationAccountName The name of the automation account.
     * @param runbookName The runbook name.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @throws CloudException thrown if the request is rejected by server
     * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent
     * @return the String object if successful.
     */
    public String getContent(String resourceGroupName, String automationAccountName, String runbookName) {
        return getContentWithServiceResponseAsync(resourceGroupName, automationAccountName, runbookName).toBlocking().single().body();
    }

    /**
     * Retrieve the content of runbook identified by runbook name.
     *
     * @param resourceGroupName Name of an Azure Resource group.
     * @param automationAccountName The name of the automation account.
     * @param runbookName The runbook name.
     * @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<String> getContentAsync(String resourceGroupName, String automationAccountName, String runbookName, final ServiceCallback<String> serviceCallback) {
        return ServiceFuture.fromResponse(getContentWithServiceResponseAsync(resourceGroupName, automationAccountName, runbookName), serviceCallback);
    }

    /**
     * Retrieve the content of runbook identified by runbook name.
     *
     * @param resourceGroupName Name of an Azure Resource group.
     * @param automationAccountName The name of the automation account.
     * @param runbookName The runbook name.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the observable to the String object
     */
    public Observable<String> getContentAsync(String resourceGroupName, String automationAccountName, String runbookName) {
        return getContentWithServiceResponseAsync(resourceGroupName, automationAccountName, runbookName).map(new Func1<ServiceResponse<String>, String>() {
            @Override
            public String call(ServiceResponse<String> response) {
                return response.body();
            }
        });
    }

    /**
     * Retrieve the content of runbook identified by runbook name.
     *
     * @param resourceGroupName Name of an Azure Resource group.
     * @param automationAccountName The name of the automation account.
     * @param runbookName The runbook name.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the observable to the String object
     */
    public Observable<ServiceResponse<String>> getContentWithServiceResponseAsync(String resourceGroupName, String automationAccountName, String runbookName) {
        if (this.client.subscriptionId() == null) {
            throw new IllegalArgumentException("Parameter this.client.subscriptionId() is required and cannot be null.");
        }
        if (resourceGroupName == null) {
            throw new IllegalArgumentException("Parameter resourceGroupName is required and cannot be null.");
        }
        if (automationAccountName == null) {
            throw new IllegalArgumentException("Parameter automationAccountName is required and cannot be null.");
        }
        if (runbookName == null) {
            throw new IllegalArgumentException("Parameter runbookName is required and cannot be null.");
        }
        final String apiVersion = "2015-10-31";
        return service.getContent(this.client.subscriptionId(), resourceGroupName, automationAccountName, runbookName, apiVersion, this.client.acceptLanguage(), this.client.userAgent())
            .flatMap(new Func1<Response<ResponseBody>, Observable<ServiceResponse<String>>>() {
                @Override
                public Observable<ServiceResponse<String>> call(Response<ResponseBody> response) {
                    try {
                        ServiceResponse<String> clientResponse = getContentDelegate(response);
                        return Observable.just(clientResponse);
                    } catch (Throwable t) {
                        return Observable.error(t);
                    }
                }
            });
    }

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

    /**
     * Retrieve the runbook identified by runbook name.
     *
     * @param resourceGroupName Name of an Azure Resource group.
     * @param automationAccountName The name of the automation account.
     * @param runbookName The runbook name.
     * @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 RunbookInner object if successful.
     */
    public RunbookInner get(String resourceGroupName, String automationAccountName, String runbookName) {
        return getWithServiceResponseAsync(resourceGroupName, automationAccountName, runbookName).toBlocking().single().body();
    }

    /**
     * Retrieve the runbook identified by runbook name.
     *
     * @param resourceGroupName Name of an Azure Resource group.
     * @param automationAccountName The name of the automation account.
     * @param runbookName The runbook name.
     * @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<RunbookInner> getAsync(String resourceGroupName, String automationAccountName, String runbookName, final ServiceCallback<RunbookInner> serviceCallback) {
        return ServiceFuture.fromResponse(getWithServiceResponseAsync(resourceGroupName, automationAccountName, runbookName), serviceCallback);
    }

    /**
     * Retrieve the runbook identified by runbook name.
     *
     * @param resourceGroupName Name of an Azure Resource group.
     * @param automationAccountName The name of the automation account.
     * @param runbookName The runbook name.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the observable to the RunbookInner object
     */
    public Observable<RunbookInner> getAsync(String resourceGroupName, String automationAccountName, String runbookName) {
        return getWithServiceResponseAsync(resourceGroupName, automationAccountName, runbookName).map(new Func1<ServiceResponse<RunbookInner>, RunbookInner>() {
            @Override
            public RunbookInner call(ServiceResponse<RunbookInner> response) {
                return response.body();
            }
        });
    }

    /**
     * Retrieve the runbook identified by runbook name.
     *
     * @param resourceGroupName Name of an Azure Resource group.
     * @param automationAccountName The name of the automation account.
     * @param runbookName The runbook name.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the observable to the RunbookInner object
     */
    public Observable<ServiceResponse<RunbookInner>> getWithServiceResponseAsync(String resourceGroupName, String automationAccountName, String runbookName) {
        if (this.client.subscriptionId() == null) {
            throw new IllegalArgumentException("Parameter this.client.subscriptionId() is required and cannot be null.");
        }
        if (resourceGroupName == null) {
            throw new IllegalArgumentException("Parameter resourceGroupName is required and cannot be null.");
        }
        if (automationAccountName == null) {
            throw new IllegalArgumentException("Parameter automationAccountName is required and cannot be null.");
        }
        if (runbookName == null) {
            throw new IllegalArgumentException("Parameter runbookName is required and cannot be null.");
        }
        final String apiVersion = "2015-10-31";
        return service.get(this.client.subscriptionId(), resourceGroupName, automationAccountName, runbookName, apiVersion, this.client.acceptLanguage(), this.client.userAgent())
            .flatMap(new Func1<Response<ResponseBody>, Observable<ServiceResponse<RunbookInner>>>() {
                @Override
                public Observable<ServiceResponse<RunbookInner>> call(Response<ResponseBody> response) {
                    try {
                        ServiceResponse<RunbookInner> clientResponse = getDelegate(response);
                        return Observable.just(clientResponse);
                    } catch (Throwable t) {
                        return Observable.error(t);
                    }
                }
            });
    }

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

    /**
     * Create the runbook identified by runbook name.
     *
     * @param resourceGroupName Name of an Azure Resource group.
     * @param automationAccountName The name of the automation account.
     * @param runbookName The runbook name.
     * @param parameters The create or update parameters for runbook. Provide either content link for a published runbook or draft, not both.
     * @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 RunbookInner object if successful.
     */
    public RunbookInner createOrUpdate(String resourceGroupName, String automationAccountName, String runbookName, RunbookCreateOrUpdateParameters parameters) {
        return createOrUpdateWithServiceResponseAsync(resourceGroupName, automationAccountName, runbookName, parameters).toBlocking().single().body();
    }

    /**
     * Create the runbook identified by runbook name.
     *
     * @param resourceGroupName Name of an Azure Resource group.
     * @param automationAccountName The name of the automation account.
     * @param runbookName The runbook name.
     * @param parameters The create or update parameters for runbook. Provide either content link for a published runbook or draft, not both.
     * @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<RunbookInner> createOrUpdateAsync(String resourceGroupName, String automationAccountName, String runbookName, RunbookCreateOrUpdateParameters parameters, final ServiceCallback<RunbookInner> serviceCallback) {
        return ServiceFuture.fromResponse(createOrUpdateWithServiceResponseAsync(resourceGroupName, automationAccountName, runbookName, parameters), serviceCallback);
    }

    /**
     * Create the runbook identified by runbook name.
     *
     * @param resourceGroupName Name of an Azure Resource group.
     * @param automationAccountName The name of the automation account.
     * @param runbookName The runbook name.
     * @param parameters The create or update parameters for runbook. Provide either content link for a published runbook or draft, not both.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the observable to the RunbookInner object
     */
    public Observable<RunbookInner> createOrUpdateAsync(String resourceGroupName, String automationAccountName, String runbookName, RunbookCreateOrUpdateParameters parameters) {
        return createOrUpdateWithServiceResponseAsync(resourceGroupName, automationAccountName, runbookName, parameters).map(new Func1<ServiceResponse<RunbookInner>, RunbookInner>() {
            @Override
            public RunbookInner call(ServiceResponse<RunbookInner> response) {
                return response.body();
            }
        });
    }

    /**
     * Create the runbook identified by runbook name.
     *
     * @param resourceGroupName Name of an Azure Resource group.
     * @param automationAccountName The name of the automation account.
     * @param runbookName The runbook name.
     * @param parameters The create or update parameters for runbook. Provide either content link for a published runbook or draft, not both.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the observable to the RunbookInner object
     */
    public Observable<ServiceResponse<RunbookInner>> createOrUpdateWithServiceResponseAsync(String resourceGroupName, String automationAccountName, String runbookName, RunbookCreateOrUpdateParameters parameters) {
        if (this.client.subscriptionId() == null) {
            throw new IllegalArgumentException("Parameter this.client.subscriptionId() is required and cannot be null.");
        }
        if (resourceGroupName == null) {
            throw new IllegalArgumentException("Parameter resourceGroupName is required and cannot be null.");
        }
        if (automationAccountName == null) {
            throw new IllegalArgumentException("Parameter automationAccountName is required and cannot be null.");
        }
        if (runbookName == null) {
            throw new IllegalArgumentException("Parameter runbookName is required and cannot be null.");
        }
        if (parameters == null) {
            throw new IllegalArgumentException("Parameter parameters is required and cannot be null.");
        }
        Validator.validate(parameters);
        final String apiVersion = "2015-10-31";
        return service.createOrUpdate(this.client.subscriptionId(), resourceGroupName, automationAccountName, runbookName, parameters, apiVersion, this.client.acceptLanguage(), this.client.userAgent())
            .flatMap(new Func1<Response<ResponseBody>, Observable<ServiceResponse<RunbookInner>>>() {
                @Override
                public Observable<ServiceResponse<RunbookInner>> call(Response<ResponseBody> response) {
                    try {
                        ServiceResponse<RunbookInner> clientResponse = createOrUpdateDelegate(response);
                        return Observable.just(clientResponse);
                    } catch (Throwable t) {
                        return Observable.error(t);
                    }
                }
            });
    }

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

    /**
     * Update the runbook identified by runbook name.
     *
     * @param resourceGroupName Name of an Azure Resource group.
     * @param automationAccountName The name of the automation account.
     * @param runbookName The runbook name.
     * @param parameters The update parameters for runbook.
     * @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 RunbookInner object if successful.
     */
    public RunbookInner update(String resourceGroupName, String automationAccountName, String runbookName, RunbookUpdateParameters parameters) {
        return updateWithServiceResponseAsync(resourceGroupName, automationAccountName, runbookName, parameters).toBlocking().single().body();
    }

    /**
     * Update the runbook identified by runbook name.
     *
     * @param resourceGroupName Name of an Azure Resource group.
     * @param automationAccountName The name of the automation account.
     * @param runbookName The runbook name.
     * @param parameters The update parameters for runbook.
     * @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<RunbookInner> updateAsync(String resourceGroupName, String automationAccountName, String runbookName, RunbookUpdateParameters parameters, final ServiceCallback<RunbookInner> serviceCallback) {
        return ServiceFuture.fromResponse(updateWithServiceResponseAsync(resourceGroupName, automationAccountName, runbookName, parameters), serviceCallback);
    }

    /**
     * Update the runbook identified by runbook name.
     *
     * @param resourceGroupName Name of an Azure Resource group.
     * @param automationAccountName The name of the automation account.
     * @param runbookName The runbook name.
     * @param parameters The update parameters for runbook.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the observable to the RunbookInner object
     */
    public Observable<RunbookInner> updateAsync(String resourceGroupName, String automationAccountName, String runbookName, RunbookUpdateParameters parameters) {
        return updateWithServiceResponseAsync(resourceGroupName, automationAccountName, runbookName, parameters).map(new Func1<ServiceResponse<RunbookInner>, RunbookInner>() {
            @Override
            public RunbookInner call(ServiceResponse<RunbookInner> response) {
                return response.body();
            }
        });
    }

    /**
     * Update the runbook identified by runbook name.
     *
     * @param resourceGroupName Name of an Azure Resource group.
     * @param automationAccountName The name of the automation account.
     * @param runbookName The runbook name.
     * @param parameters The update parameters for runbook.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the observable to the RunbookInner object
     */
    public Observable<ServiceResponse<RunbookInner>> updateWithServiceResponseAsync(String resourceGroupName, String automationAccountName, String runbookName, RunbookUpdateParameters parameters) {
        if (this.client.subscriptionId() == null) {
            throw new IllegalArgumentException("Parameter this.client.subscriptionId() is required and cannot be null.");
        }
        if (resourceGroupName == null) {
            throw new IllegalArgumentException("Parameter resourceGroupName is required and cannot be null.");
        }
        if (automationAccountName == null) {
            throw new IllegalArgumentException("Parameter automationAccountName is required and cannot be null.");
        }
        if (runbookName == null) {
            throw new IllegalArgumentException("Parameter runbookName is required and cannot be null.");
        }
        if (parameters == null) {
            throw new IllegalArgumentException("Parameter parameters is required and cannot be null.");
        }
        Validator.validate(parameters);
        final String apiVersion = "2015-10-31";
        return service.update(this.client.subscriptionId(), resourceGroupName, automationAccountName, runbookName, parameters, apiVersion, this.client.acceptLanguage(), this.client.userAgent())
            .flatMap(new Func1<Response<ResponseBody>, Observable<ServiceResponse<RunbookInner>>>() {
                @Override
                public Observable<ServiceResponse<RunbookInner>> call(Response<ResponseBody> response) {
                    try {
                        ServiceResponse<RunbookInner> clientResponse = updateDelegate(response);
                        return Observable.just(clientResponse);
                    } catch (Throwable t) {
                        return Observable.error(t);
                    }
                }
            });
    }

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

    /**
     * Delete the runbook by name.
     *
     * @param resourceGroupName Name of an Azure Resource group.
     * @param automationAccountName The name of the automation account.
     * @param runbookName The runbook name.
     * @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 resourceGroupName, String automationAccountName, String runbookName) {
        deleteWithServiceResponseAsync(resourceGroupName, automationAccountName, runbookName).toBlocking().single().body();
    }

    /**
     * Delete the runbook by name.
     *
     * @param resourceGroupName Name of an Azure Resource group.
     * @param automationAccountName The name of the automation account.
     * @param runbookName The runbook name.
     * @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 resourceGroupName, String automationAccountName, String runbookName, final ServiceCallback<Void> serviceCallback) {
        return ServiceFuture.fromResponse(deleteWithServiceResponseAsync(resourceGroupName, automationAccountName, runbookName), serviceCallback);
    }

    /**
     * Delete the runbook by name.
     *
     * @param resourceGroupName Name of an Azure Resource group.
     * @param automationAccountName The name of the automation account.
     * @param runbookName The runbook name.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the {@link ServiceResponse} object if successful.
     */
    public Observable<Void> deleteAsync(String resourceGroupName, String automationAccountName, String runbookName) {
        return deleteWithServiceResponseAsync(resourceGroupName, automationAccountName, runbookName).map(new Func1<ServiceResponse<Void>, Void>() {
            @Override
            public Void call(ServiceResponse<Void> response) {
                return response.body();
            }
        });
    }

    /**
     * Delete the runbook by name.
     *
     * @param resourceGroupName Name of an Azure Resource group.
     * @param automationAccountName The name of the automation account.
     * @param runbookName The runbook name.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the {@link ServiceResponse} object if successful.
     */
    public Observable<ServiceResponse<Void>> deleteWithServiceResponseAsync(String resourceGroupName, String automationAccountName, String runbookName) {
        if (this.client.subscriptionId() == null) {
            throw new IllegalArgumentException("Parameter this.client.subscriptionId() is required and cannot be null.");
        }
        if (resourceGroupName == null) {
            throw new IllegalArgumentException("Parameter resourceGroupName is required and cannot be null.");
        }
        if (automationAccountName == null) {
            throw new IllegalArgumentException("Parameter automationAccountName is required and cannot be null.");
        }
        if (runbookName == null) {
            throw new IllegalArgumentException("Parameter runbookName is required and cannot be null.");
        }
        final String apiVersion = "2015-10-31";
        return service.delete(this.client.subscriptionId(), resourceGroupName, automationAccountName, runbookName, apiVersion, this.client.acceptLanguage(), 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(200, new TypeToken<Void>() { }.getType())
                .register(204, new TypeToken<Void>() { }.getType())
                .registerError(ErrorResponseException.class)
                .build(response);
    }

    /**
     * Retrieve a list of runbooks.
     *
     * @param resourceGroupName Name of an Azure Resource group.
     * @param automationAccountName The name of the automation account.
     * @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 PagedList&lt;RunbookInner&gt; object if successful.
     */
    public PagedList<RunbookInner> listByAutomationAccount(final String resourceGroupName, final String automationAccountName) {
        ServiceResponse<Page<RunbookInner>> response = listByAutomationAccountSinglePageAsync(resourceGroupName, automationAccountName).toBlocking().single();
        return new PagedList<RunbookInner>(response.body()) {
            @Override
            public Page<RunbookInner> nextPage(String nextPageLink) {
                return listByAutomationAccountNextSinglePageAsync(nextPageLink).toBlocking().single().body();
            }
        };
    }

    /**
     * Retrieve a list of runbooks.
     *
     * @param resourceGroupName Name of an Azure Resource group.
     * @param automationAccountName The name of the automation account.
     * @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<List<RunbookInner>> listByAutomationAccountAsync(final String resourceGroupName, final String automationAccountName, final ListOperationCallback<RunbookInner> serviceCallback) {
        return AzureServiceFuture.fromPageResponse(
            listByAutomationAccountSinglePageAsync(resourceGroupName, automationAccountName),
            new Func1<String, Observable<ServiceResponse<Page<RunbookInner>>>>() {
                @Override
                public Observable<ServiceResponse<Page<RunbookInner>>> call(String nextPageLink) {
                    return listByAutomationAccountNextSinglePageAsync(nextPageLink);
                }
            },
            serviceCallback);
    }

    /**
     * Retrieve a list of runbooks.
     *
     * @param resourceGroupName Name of an Azure Resource group.
     * @param automationAccountName The name of the automation account.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the observable to the PagedList&lt;RunbookInner&gt; object
     */
    public Observable<Page<RunbookInner>> listByAutomationAccountAsync(final String resourceGroupName, final String automationAccountName) {
        return listByAutomationAccountWithServiceResponseAsync(resourceGroupName, automationAccountName)
            .map(new Func1<ServiceResponse<Page<RunbookInner>>, Page<RunbookInner>>() {
                @Override
                public Page<RunbookInner> call(ServiceResponse<Page<RunbookInner>> response) {
                    return response.body();
                }
            });
    }

    /**
     * Retrieve a list of runbooks.
     *
     * @param resourceGroupName Name of an Azure Resource group.
     * @param automationAccountName The name of the automation account.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the observable to the PagedList&lt;RunbookInner&gt; object
     */
    public Observable<ServiceResponse<Page<RunbookInner>>> listByAutomationAccountWithServiceResponseAsync(final String resourceGroupName, final String automationAccountName) {
        return listByAutomationAccountSinglePageAsync(resourceGroupName, automationAccountName)
            .concatMap(new Func1<ServiceResponse<Page<RunbookInner>>, Observable<ServiceResponse<Page<RunbookInner>>>>() {
                @Override
                public Observable<ServiceResponse<Page<RunbookInner>>> call(ServiceResponse<Page<RunbookInner>> page) {
                    String nextPageLink = page.body().nextPageLink();
                    if (nextPageLink == null) {
                        return Observable.just(page);
                    }
                    return Observable.just(page).concatWith(listByAutomationAccountNextWithServiceResponseAsync(nextPageLink));
                }
            });
    }

    /**
     * Retrieve a list of runbooks.
     *
    ServiceResponse<PageImpl<RunbookInner>> * @param resourceGroupName Name of an Azure Resource group.
    ServiceResponse<PageImpl<RunbookInner>> * @param automationAccountName The name of the automation account.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the PagedList&lt;RunbookInner&gt; object wrapped in {@link ServiceResponse} if successful.
     */
    public Observable<ServiceResponse<Page<RunbookInner>>> listByAutomationAccountSinglePageAsync(final String resourceGroupName, final String automationAccountName) {
        if (this.client.subscriptionId() == null) {
            throw new IllegalArgumentException("Parameter this.client.subscriptionId() is required and cannot be null.");
        }
        if (resourceGroupName == null) {
            throw new IllegalArgumentException("Parameter resourceGroupName is required and cannot be null.");
        }
        if (automationAccountName == null) {
            throw new IllegalArgumentException("Parameter automationAccountName is required and cannot be null.");
        }
        final String apiVersion = "2015-10-31";
        return service.listByAutomationAccount(this.client.subscriptionId(), resourceGroupName, automationAccountName, apiVersion, this.client.acceptLanguage(), this.client.userAgent())
            .flatMap(new Func1<Response<ResponseBody>, Observable<ServiceResponse<Page<RunbookInner>>>>() {
                @Override
                public Observable<ServiceResponse<Page<RunbookInner>>> call(Response<ResponseBody> response) {
                    try {
                        ServiceResponse<PageImpl<RunbookInner>> result = listByAutomationAccountDelegate(response);
                        return Observable.just(new ServiceResponse<Page<RunbookInner>>(result.body(), result.response()));
                    } catch (Throwable t) {
                        return Observable.error(t);
                    }
                }
            });
    }

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

    /**
     * Retrieve a list of runbooks.
     *
     * @param nextPageLink The NextLink from the previous successful call to List operation.
     * @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 PagedList&lt;RunbookInner&gt; object if successful.
     */
    public PagedList<RunbookInner> listByAutomationAccountNext(final String nextPageLink) {
        ServiceResponse<Page<RunbookInner>> response = listByAutomationAccountNextSinglePageAsync(nextPageLink).toBlocking().single();
        return new PagedList<RunbookInner>(response.body()) {
            @Override
            public Page<RunbookInner> nextPage(String nextPageLink) {
                return listByAutomationAccountNextSinglePageAsync(nextPageLink).toBlocking().single().body();
            }
        };
    }

    /**
     * Retrieve a list of runbooks.
     *
     * @param nextPageLink The NextLink from the previous successful call to List operation.
     * @param serviceFuture the ServiceFuture object tracking the Retrofit calls
     * @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<List<RunbookInner>> listByAutomationAccountNextAsync(final String nextPageLink, final ServiceFuture<List<RunbookInner>> serviceFuture, final ListOperationCallback<RunbookInner> serviceCallback) {
        return AzureServiceFuture.fromPageResponse(
            listByAutomationAccountNextSinglePageAsync(nextPageLink),
            new Func1<String, Observable<ServiceResponse<Page<RunbookInner>>>>() {
                @Override
                public Observable<ServiceResponse<Page<RunbookInner>>> call(String nextPageLink) {
                    return listByAutomationAccountNextSinglePageAsync(nextPageLink);
                }
            },
            serviceCallback);
    }

    /**
     * Retrieve a list of runbooks.
     *
     * @param nextPageLink The NextLink from the previous successful call to List operation.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the observable to the PagedList&lt;RunbookInner&gt; object
     */
    public Observable<Page<RunbookInner>> listByAutomationAccountNextAsync(final String nextPageLink) {
        return listByAutomationAccountNextWithServiceResponseAsync(nextPageLink)
            .map(new Func1<ServiceResponse<Page<RunbookInner>>, Page<RunbookInner>>() {
                @Override
                public Page<RunbookInner> call(ServiceResponse<Page<RunbookInner>> response) {
                    return response.body();
                }
            });
    }

    /**
     * Retrieve a list of runbooks.
     *
     * @param nextPageLink The NextLink from the previous successful call to List operation.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the observable to the PagedList&lt;RunbookInner&gt; object
     */
    public Observable<ServiceResponse<Page<RunbookInner>>> listByAutomationAccountNextWithServiceResponseAsync(final String nextPageLink) {
        return listByAutomationAccountNextSinglePageAsync(nextPageLink)
            .concatMap(new Func1<ServiceResponse<Page<RunbookInner>>, Observable<ServiceResponse<Page<RunbookInner>>>>() {
                @Override
                public Observable<ServiceResponse<Page<RunbookInner>>> call(ServiceResponse<Page<RunbookInner>> page) {
                    String nextPageLink = page.body().nextPageLink();
                    if (nextPageLink == null) {
                        return Observable.just(page);
                    }
                    return Observable.just(page).concatWith(listByAutomationAccountNextWithServiceResponseAsync(nextPageLink));
                }
            });
    }

    /**
     * Retrieve a list of runbooks.
     *
    ServiceResponse<PageImpl<RunbookInner>> * @param nextPageLink The NextLink from the previous successful call to List operation.
     * @throws IllegalArgumentException thrown if parameters fail the validation
     * @return the PagedList&lt;RunbookInner&gt; object wrapped in {@link ServiceResponse} if successful.
     */
    public Observable<ServiceResponse<Page<RunbookInner>>> listByAutomationAccountNextSinglePageAsync(final String nextPageLink) {
        if (nextPageLink == null) {
            throw new IllegalArgumentException("Parameter nextPageLink is required and cannot be null.");
        }
        String nextUrl = String.format("%s", nextPageLink);
        return service.listByAutomationAccountNext(nextUrl, this.client.acceptLanguage(), this.client.userAgent())
            .flatMap(new Func1<Response<ResponseBody>, Observable<ServiceResponse<Page<RunbookInner>>>>() {
                @Override
                public Observable<ServiceResponse<Page<RunbookInner>>> call(Response<ResponseBody> response) {
                    try {
                        ServiceResponse<PageImpl<RunbookInner>> result = listByAutomationAccountNextDelegate(response);
                        return Observable.just(new ServiceResponse<Page<RunbookInner>>(result.body(), result.response()));
                    } catch (Throwable t) {
                        return Observable.error(t);
                    }
                }
            });
    }

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

}
