Exemplo n.º 1
0
 /// <summary>
 /// Replaces a <see cref="CosmosTriggerSettings"/> in the Azure Cosmos service as an asynchronous operation.
 /// </summary>
 /// <param name="triggerSettings">The <see cref="CosmosTriggerSettings"/> object.</param>
 /// <param name="requestOptions">(Optional) The options for the trigger request <see cref="CosmosRequestOptions"/></param>
 /// <param name="cancellationToken">(Optional) <see cref="CancellationToken"/> representing request cancellation.</param>
 /// <returns>
 /// A <see cref="Task"/> containing a <see cref="CosmosTriggerResponse"/> which wraps a <see cref="CosmosTriggerSettings"/> containing the updated resource record.
 /// </returns>
 /// <exception cref="ArgumentNullException">If <paramref name="triggerSettings"/> is not set.</exception>
 /// <exception cref="CosmosException">This exception can encapsulate many different types of errors. To determine the specific error always look at the StatusCode property. Some common codes you may get when creating a Document are:
 /// <list type="table">
 ///     <listheader>
 ///         <term>StatusCode</term><description>Reason for exception</description>
 ///     </listheader>
 ///     <item>
 ///         <term>404</term><description>NotFound - This means the resource you tried to delete did not exist.</description>
 ///     </item>
 /// </list>
 /// </exception>
 /// <example>
 /// This examples replaces an existing trigger.
 /// <code language="c#">
 /// <![CDATA[
 /// //Updated settings
 /// CosmosTriggerSettings settings = new CosmosTriggerSettings
 /// {
 ///     Id = "testTriggerId",
 ///     Body = @"function AddTax() {
 ///         var item = getContext().getRequest().getBody();
 ///
 ///         // Validate/calculate the tax.
 ///         item.tax = item.cost* .15;
 ///
 ///         // Update the request -- this is what is going to be inserted.
 ///         getContext().getRequest().setBody(item);
 ///     }",
 ///     TriggerOperation = TriggerOperation.All,
 ///     TriggerType = TriggerType.Post
 /// };
 ///
 /// CosmosTriggerResponse response = await this.cosmosTrigger.ReplaceAsync(settings);
 /// ]]>
 /// </code>
 /// </example>
 public virtual Task <CosmosTriggerResponse> ReplaceAsync(
     CosmosTriggerSettings triggerSettings,
     CosmosRequestOptions requestOptions = null,
     CancellationToken cancellationToken = default(CancellationToken))
 {
     return(this.ProcessAsync(
                partitionKey: null,
                streamPayload: triggerSettings.GetResourceStream(),
                operationType: OperationType.Replace,
                requestOptions: requestOptions,
                cancellationToken: cancellationToken));
 }
 /// <summary>
 /// Creates a trigger as an asynchronous operation in the Azure Cosmos DB service.
 /// </summary>
 /// <param name="triggerSettings">The <see cref="CosmosTriggerSettings"/> object.</param>
 /// <param name="requestOptions">(Optional) The options for the stored procedure request <see cref="CosmosRequestOptions"/></param>
 /// <param name="cancellationToken">(Optional) <see cref="CancellationToken"/> representing request cancellation.</param>
 /// <returns>A task object representing the service response for the asynchronous operation.</returns>
 /// <exception cref="ArgumentNullException">If <paramref name="triggerSettings"/> is not set.</exception>
 /// <exception cref="System.AggregateException">Represents a consolidation of failures that occurred during async processing. Look within InnerExceptions to find the actual exception(s)</exception>
 /// <exception cref="CosmosException">This exception can encapsulate many different types of errors. To determine the specific error always look at the StatusCode property. Some common codes you may get when creating a Document are:
 /// <list type="table">
 ///     <listheader>
 ///         <term>StatusCode</term><description>Reason for exception</description>
 ///     </listheader>
 ///     <item>
 ///         <term>400</term><description>BadRequest - This means something was wrong with the request supplied. It is likely that an Id was not supplied for the new trigger or that the Body was malformed.</description>
 ///     </item>
 ///     <item>
 ///         <term>403</term><description>Forbidden - You have reached your quota of triggers for the collection supplied. Contact support to have this quota increased.</description>
 ///     </item>
 ///     <item>
 ///         <term>409</term><description>Conflict - This means a <see cref="CosmosTriggerSettings"/> with an id matching the id you supplied already existed.</description>
 ///     </item>
 ///     <item>
 ///         <term>413</term><description>RequestEntityTooLarge - This means the body of the <see cref="CosmosTriggerSettings"/> you tried to create was too large.</description>
 ///     </item>
 /// </list>
 /// </exception>
 /// <example>
 ///  This creates a trigger then uses the trigger in a create item.
 /// <code language="c#">
 /// <![CDATA[
 /// CosmosTrigger cosmosTrigger = await this.container.Triggers.CreateTriggerAsync(
 ///     new CosmosTriggerSettings
 ///     {
 ///         Id = "addTax",
 ///         Body = @"function AddTax() {
 ///             var item = getContext().getRequest().getBody();
 ///
 ///             // calculate the tax.
 ///             item.tax = item.cost * .15;
 ///
 ///             // Update the request -- this is what is going to be inserted.
 ///             getContext().getRequest().setBody(item);
 ///         }",
 ///         TriggerOperation = TriggerOperation.All,
 ///         TriggerType = TriggerType.Pre
 ///     });
 ///
 /// CosmosItemRequestOptions options = new CosmosItemRequestOptions()
 /// {
 ///     PreTriggers = new List<string>() { cosmosTrigger.Id },
 /// };
 ///
 /// // Create a new item with trigger set in the request options
 /// CosmosItemResponse<dynamic> createdItem = await this.container.Items.CreateItemAsync<dynamic>(item.status, item, options);
 /// double itemTax = createdItem.Resource.tax;
 /// ]]>
 /// </code>
 /// </example>
 public virtual Task <CosmosTriggerResponse> CreateTriggerAsync(
     CosmosTriggerSettings triggerSettings,
     CosmosRequestOptions requestOptions = null,
     CancellationToken cancellationToken = default(CancellationToken))
 {
     return(ExecUtils.ProcessResourceOperationAsync <CosmosTriggerResponse>(
                this.container.Database.Client,
                this.container.LinkUri,
                ResourceType.Trigger,
                OperationType.Create,
                requestOptions,
                partitionKey: null,
                streamPayload: triggerSettings.GetResourceStream(),
                requestEnricher: null,
                responseCreator: response => this.client.ResponseFactory.CreateTriggerResponse(response, new CosmosTrigger(this.container, triggerSettings.Id)),
                cancellationToken: cancellationToken));
 }