/// <inheritdoc />
        public async override Task <BigQueryRoutine> CreateRoutineAsync(RoutineReference routineReference, Routine resource, CreateRoutineOptions options = null, CancellationToken cancellationToken = default)
        {
            var request = CreateInsertRoutineRequest(routineReference, resource, options);
            var result  = await request.ExecuteAsync(cancellationToken).ConfigureAwait(false);

            return(new BigQueryRoutine(this, result));
        }
        private InsertRequest CreateInsertRoutineRequest(RoutineReference routineReference, Routine resource, CreateRoutineOptions options)
        {
            CheckResourceReference(routineReference, resource);

            resource.RoutineReference ??= routineReference;

            var request = Service.Routines.Insert(resource, routineReference.ProjectId, routineReference.DatasetId);

            options?.ModifyRequest(request);
            request.PrettyPrint = PrettyPrint;
            return(request);
        }
        /// <inheritdoc />
        public override BigQueryRoutine CreateRoutine(RoutineReference routineReference, Routine resource, CreateRoutineOptions options = null)
        {
            var request = CreateInsertRoutineRequest(routineReference, resource, options);
            var result  = request.Execute();

            return(new BigQueryRoutine(this, result));
        }
        /// <inheritdoc />
        public override BigQueryRoutine GetOrCreateRoutine(RoutineReference routineReference, Routine resource, GetRoutineOptions getOptions = null, CreateRoutineOptions createOptions = null)
        {
            CheckResourceReference(routineReference, resource);

            try
            {
                return(GetRoutine(routineReference, getOptions));
            }
            catch (GoogleApiException ex) when(ex.HttpStatusCode == HttpStatusCode.NotFound)
            {
                try
                {
                    return(CreateRoutine(routineReference, resource, createOptions));
                }
                catch (GoogleApiException ex2) when(ex2.HttpStatusCode == HttpStatusCode.Conflict)
                {
                    return(GetRoutine(routineReference, getOptions));
                }
            }
        }
        /// <inheritdoc />
        public async override Task <BigQueryRoutine> GetOrCreateRoutineAsync(RoutineReference routineReference, Routine resource, GetRoutineOptions getOptions = null, CreateRoutineOptions createOptions = null, CancellationToken cancellationToken = default)
        {
            CheckResourceReference(routineReference, resource);

            try
            {
                return(await GetRoutineAsync(routineReference, getOptions, cancellationToken).ConfigureAwait(false));
            }
            catch (GoogleApiException ex) when(ex.HttpStatusCode == HttpStatusCode.NotFound)
            {
                try
                {
                    return(await CreateRoutineAsync(routineReference, resource, createOptions, cancellationToken).ConfigureAwait(false));
                }
                catch (GoogleApiException ex2) when(ex2.HttpStatusCode == HttpStatusCode.Conflict)
                {
                    return(await GetRoutineAsync(routineReference, getOptions, cancellationToken).ConfigureAwait(false));
                }
            }
        }
 /// <summary>
 /// Asynchronously attempts to fetch the specified routine, creating it if it doesn't exist.
 /// This method just creates a <see cref="RoutineReference"/> and delegates to <see cref="GetOrCreateRoutineAsync(RoutineReference, Routine, GetRoutineOptions, CreateRoutineOptions, CancellationToken)"/>.
 /// </summary>
 /// <param name="projectId">The project ID. Must not be null.</param>
 /// <param name="datasetId">The dataset ID. Must not be null.</param>
 /// <param name="routineId">The routine ID. Must not be null.</param>
 /// <param name="resource">The routine resource representation to use for the creation. Must not be null. If this routine's <see cref="Routine.RoutineReference"/> is specified, then it must be the same as the one obtained from the other parameters, else it will be set to the one obtained from the other parameters.</param>
 /// <param name="getOptions">The options for the "get" operation. May be null, in which case defaults will be supplied.</param>
 /// <param name="createOptions">The options for the "create" operation. May be null, in which case defaults will be supplied.</param>
 /// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
 /// <returns>A task representing the asynchronous operation. When complete, the result is
 /// the existing or new routine.</returns>
 public virtual Task <BigQueryRoutine> GetOrCreateRoutineAsync(string projectId, string datasetId, string routineId, Routine resource, GetRoutineOptions getOptions = null, CreateRoutineOptions createOptions = null, CancellationToken cancellationToken = default) =>
 GetOrCreateRoutineAsync(GetRoutineReference(projectId, datasetId, routineId), resource, getOptions, createOptions, cancellationToken);
 /// <summary>
 /// Asynchronously attempts to fetch the specified routine, creating it if it doesn't exist.
 /// </summary>
 /// <param name="routineReference">A fully-qualified identifier for the routine. Must not be null.</param>
 /// <param name="resource">The routine resource representation to use for the creation. Must not be null. If this routine's <see cref="Routine.RoutineReference"/> is specified, then it must be the same as the one obtained from the other parameters, else it will be set to the one obtained from the other parameters.</param>
 /// <param name="getOptions">The options for the "get" operation. May be null, in which case defaults will be supplied.</param>
 /// <param name="createOptions">The options for the "create" operation. May be null, in which case defaults will be supplied.</param>
 /// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
 /// <returns>A task representing the asynchronous operation. When complete, the result is
 /// the existing or new routine.</returns>
 public virtual Task <BigQueryRoutine> GetOrCreateRoutineAsync(RoutineReference routineReference, Routine resource, GetRoutineOptions getOptions = null, CreateRoutineOptions createOptions = null, CancellationToken cancellationToken = default) =>
 throw new NotImplementedException();
 /// <summary>
 /// Attempts to fetch the specified routine, creating it if it doesn't exist.
 /// </summary>
 /// <param name="routineReference">A fully-qualified identifier for the routine. Must not be null.</param>
 /// <param name="resource">The routine resource representation to use for the creation. Must not be null. If this routine's <see cref="Routine.RoutineReference"/> is specified, then it must be the same as the one obtained from the other parameters, else it will be set to the one obtained from the other parameters.</param>
 /// <param name="getOptions">The options for the "get" operation. May be null, in which case defaults will be supplied.</param>
 /// <param name="createOptions">The options for the "create" operation. May be null, in which case defaults will be supplied.</param>
 /// <returns>The existing or new routine.</returns>
 public virtual BigQueryRoutine GetOrCreateRoutine(RoutineReference routineReference, Routine resource, GetRoutineOptions getOptions = null, CreateRoutineOptions createOptions = null) =>
 throw new NotImplementedException();
 /// <summary>
 /// Attempts to fetch the specified routine, creating it if it doesn't exist.
 /// This method just creates a <see cref="RoutineReference"/> and delegates to <see cref="GetOrCreateRoutine(RoutineReference, Routine, GetRoutineOptions, CreateRoutineOptions)"/>.
 /// </summary>
 /// <param name="projectId">The project ID. Must not be null.</param>
 /// <param name="datasetId">The dataset ID. Must not be null.</param>
 /// <param name="routineId">The routine ID. Must not be null.</param>
 /// <param name="resource">The routine resource representation to use for the creation. Must not be null. If this routine's <see cref="Routine.RoutineReference"/> is specified, then it must be the same as the one obtained from the other parameters, else it will be set to the one obtained from the other parameters.</param>
 /// <param name="getOptions">The options for the "get" operation. May be null, in which case defaults will be supplied.</param>
 /// <param name="createOptions">The options for the "create" operation. May be null, in which case defaults will be supplied.</param>
 /// <returns>The existing or new routine.</returns>
 public virtual BigQueryRoutine GetOrCreateRoutine(string projectId, string datasetId, string routineId, Routine resource, GetRoutineOptions getOptions = null, CreateRoutineOptions createOptions = null) =>
 GetOrCreateRoutine(GetRoutineReference(projectId, datasetId, routineId), resource, getOptions, createOptions);
 /// <summary>
 /// Asynchronously creates the specified routine within this client's project.
 /// This method just creates a <see cref="RoutineReference"/> and delegates to <see cref="CreateRoutineAsync(RoutineReference, Routine, CreateRoutineOptions, CancellationToken)"/>.
 /// </summary>
 /// <param name="datasetId">The dataset ID. Must not be null.</param>
 /// <param name="routineId">The routine ID. Must not be null.</param>
 /// <param name="resource">The routine resource representation to use for the creation. Must not be null. If this routine's <see cref="Routine.RoutineReference"/> is specified, then it must be the same as the one obtained from the other parameters, else it will be set to the one obtained from the other parameters.</param>
 /// <param name="options">The options for the operation. May be null, in which case defaults will be supplied.</param>
 /// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
 /// <returns>A task representing the asynchronous operation. When complete, the result is
 /// the newly created routine.</returns>
 public virtual Task <BigQueryRoutine> CreateRoutineAsync(string datasetId, string routineId, Routine resource, CreateRoutineOptions options = null, CancellationToken cancellationToken = default) =>
 CreateRoutineAsync(GetRoutineReference(datasetId, routineId), resource, options, cancellationToken);
 /// <summary>
 /// Creates the specified routine.
 /// </summary>
 /// <param name="routineReference">A fully-qualified identifier for the routine. Must not be null.</param>
 /// <param name="resource">The routine resource representation to use for the creation. Must not be null. If this routine's <see cref="Routine.RoutineReference"/> is specified, then it must be the same as the one obtained from the other parameters, else it will be set to the one obtained from the other parameters.</param>
 /// <param name="options">The options for the operation. May be null, in which case defaults will be supplied.</param>
 /// <returns>The newly created routine.</returns>
 public virtual BigQueryRoutine CreateRoutine(RoutineReference routineReference, Routine resource, CreateRoutineOptions options = null) =>
 throw new NotImplementedException();
 /// <summary>
 /// Creates the specified routine.
 /// This method just creates a <see cref="RoutineReference"/> and delegates to <see cref="CreateRoutine(RoutineReference, Routine, CreateRoutineOptions)"/>.
 /// </summary>
 /// <param name="projectId">The project ID. Must not be null.</param>
 /// <param name="datasetId">The dataset ID. Must not be null.</param>
 /// <param name="routineId">The routine ID. Must not be null.</param>
 /// <param name="resource">The routine resource representation to use for the creation. Must not be null. If this routine's <see cref="Routine.RoutineReference"/> is specified, then it must be the same as the one obtained from the other parameters, else it will be set to the one obtained from the other parameters.</param>
 /// <param name="options">The options for the operation. May be null, in which case defaults will be supplied.</param>
 /// <returns>The newly created routine.</returns>
 public virtual BigQueryRoutine CreateRoutine(string projectId, string datasetId, string routineId, Routine resource, CreateRoutineOptions options = null) =>
 CreateRoutine(GetRoutineReference(projectId, datasetId, routineId), resource, options);