예제 #1
0
        /// <summary>
        /// Updates the specified subscription.
        /// </summary>
        /// <param name="customerId">Identifier for the customer.</param>
        /// <param name="subscription">An instance of <see cref="Subscription"/> that represents the modified subscription.</param>
        /// <returns>An instance of <see cref="Subscription"/> that represents the modified subscription.</returns>
        /// <exception cref="ArgumentException">
        /// <paramref name="customerId"/> is empty or null.
        /// </exception>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="subscription"/> is empty or null.
        /// </exception>
        public async Task <Subscription> UpdateSubscriptionAsync(string customerId, Subscription subscription)
        {
            CustomerPrincipal           principal;
            DateTime                    startTime;
            Dictionary <string, double> eventMetrics;
            Dictionary <string, string> eventProperties;
            Guid         correlationId;
            IPartner     operations;
            Subscription updatedSubscription;

            customerId.AssertNotEmpty(nameof(customerId));
            subscription.AssertNotNull(nameof(subscription));

            try
            {
                startTime     = DateTime.Now;
                correlationId = Guid.NewGuid();
                operations    = await this.GetAppOperationsAsync(correlationId);

                principal = (CustomerPrincipal)HttpContext.Current.User;

                if (principal.CustomerId.Equals(this.service.Configuration.PartnerCenterApplicationTenantId))
                {
                    updatedSubscription = await operations.Customers.ById(customerId).Subscriptions
                                          .ById(subscription.Id).PatchAsync(subscription);
                }
                else
                {
                    updatedSubscription = await operations.Customers.ById(principal.CustomerId).Subscriptions
                                          .ById(subscription.Id).PatchAsync(subscription);
                }

                // Track the event measurements for analysis.
                eventMetrics = new Dictionary <string, double>
                {
                    { "ElapsedMilliseconds", DateTime.Now.Subtract(startTime).TotalMilliseconds }
                };

                // Capture the request for the customer summary for analysis.
                eventProperties = new Dictionary <string, string>
                {
                    { "CustomerId", principal.CustomerId },
                    { "Name", principal.Name },
                    { "ParternCenterCorrelationId", correlationId.ToString() }
                };

                this.service.Telemetry.TrackEvent("GetSubscriptionAsync", eventProperties, eventMetrics);

                return(updatedSubscription);
            }
            finally
            {
                eventMetrics    = null;
                eventProperties = null;
                operations      = null;
                principal       = null;
            }
        }
        /// <summary>
        /// Patches a subscription.
        /// </summary>
        /// <param name="entity">The subscription information.</param>
        /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
        /// <returns>The updated subscription information.</returns>
        public async Task <Subscription> PatchAsync(Subscription entity, CancellationToken cancellationToken = default)
        {
            entity.AssertNotNull(nameof(entity));

            return(await Partner.ServiceClient.PatchAsync <Subscription, Subscription>(
                       new Uri(
                           string.Format(
                               CultureInfo.InvariantCulture,
                               $"/{PartnerService.Instance.ApiVersion}/{PartnerService.Instance.Configuration.Apis.UpdateSubscription.Path}",
                               Context.Item1,
                               Context.Item2),
                           UriKind.Relative),
                       entity,
                       cancellationToken).ConfigureAwait(false));
        }