/// <summary> /// Executes the scenario. /// </summary> protected override void RunScenario() { var partnerOperations = this.Context.UserPartnerOperations; var productId = this.ObtainProductId("Enter the ID of the product to check inventory for"); var customerId = this.ObtainCustomerId("Enter a customer ID"); var subscriptionId = this.ObtainSubscriptionId(customerId, "Enter a subscription ID"); string countryCode = this.Context.ConsoleHelper.ReadNonEmptyString("Enter the 2 digit country code", "The country code can't be empty"); var inventoryCheckRequest = new InventoryCheckRequest() { TargetItems = new InventoryItem[] { new InventoryItem { ProductId = productId } }, InventoryContext = new Dictionary <string, string>() { { "customerId", customerId }, { "azureSubscriptionId", subscriptionId } } }; this.Context.ConsoleHelper.StartProgress(string.Format(CultureInfo.InvariantCulture, "Checking inventory for product {0} in country {1}", productId, countryCode)); var inventoryResults = partnerOperations.Extensions.Product.ByCountry(countryCode).CheckInventory(inventoryCheckRequest); this.Context.ConsoleHelper.StopProgress(); this.Context.ConsoleHelper.WriteObject(inventoryResults, string.Format(CultureInfo.InvariantCulture, "Inventory check for product {0}", productId)); }
/// <summary> /// Executes the operations associated with the cmdlet. /// </summary> public override void ExecuteCmdlet() { Scheduler.RunTask(async() => { IPartner partner = await PartnerSession.Instance.ClientFactory.CreatePartnerOperationsAsync(CorrelationId, CancellationToken).ConfigureAwait(false); string countryCode = (string.IsNullOrEmpty(CountryCode)) ? PartnerSession.Instance.Context.CountryCode : CountryCode; if (Variables == null) { Variables = new Hashtable(); } IEnumerable <InventoryItem> item; InventoryCheckRequest request; request = new InventoryCheckRequest() { TargetItems = string.IsNullOrEmpty(SkuId) ? new InventoryItem[] { new InventoryItem { ProductId = ProductId } } : new InventoryItem[] { new InventoryItem { ProductId = ProductId, SkuId = SkuId } }, }; foreach (KeyValuePair <string, string> kvp in Variables.Cast <DictionaryEntry>().ToDictionary(kvp => (string)kvp.Key, kvp => (string)kvp.Value)) { request.InventoryContext.Add(kvp.Key, kvp.Value); } item = await partner.Extensions.Product.ByCountry(countryCode).CheckInventoryAsync(request, CancellationToken).ConfigureAwait(false); WriteObject(item.Select(i => new PSInventoryItem(i)), true); }, true); }
/// <summary> /// Gets the specified product SKU. /// </summary> /// <param name="countryCode">The country used to obtain the offer.</param> /// <param name="productId">Identifier for the product.</param> /// <param name="context">The list of variables needed to execute an inventory check on this item.</param> /// <exception cref="System.ArgumentException"> /// <paramref name="countryCode"/> is empty or null. /// or /// <paramref name="productId"/> is empty or null. /// or /// <paramref name="context"/> is empty or null. /// </exception> private void GetProductInventory(string countryCode, string productId, string skuId, Hashtable context) { IEnumerable <InventoryItem> item; InventoryCheckRequest request; countryCode.AssertNotEmpty(nameof(countryCode)); productId.AssertNotEmpty(nameof(productId)); request = new InventoryCheckRequest() { TargetItems = string.IsNullOrEmpty(skuId) ? new InventoryItem[] { new InventoryItem { ProductId = productId } } : new InventoryItem[] { new InventoryItem { ProductId = productId, SkuId = skuId } }, }; foreach (KeyValuePair <string, string> kvp in context.Cast <DictionaryEntry>().ToDictionary(kvp => (string)kvp.Key, kvp => (string)kvp.Value)) { request.InventoryContext.Add(kvp.Key, kvp.Value); } item = Partner.Extensions.Product.ByCountry(countryCode).CheckInventoryAsync(request).GetAwaiter().GetResult(); WriteObject(item.Select(i => new PSInventoryItem(i)), true); }
/// <summary> /// Gets the specified product sku. /// </summary> /// <param name="countryCode">The country used to obtain the offer.</param> /// <param name="productId">Identifier for the product.</param> /// <param name="context">The list of variables needed to execute an inventory check on this item.</param> /// <exception cref="System.ArgumentException"> /// <paramref name="countryCode"/> is empty or null. /// or /// <paramref name="productId"/> is empty or null. /// or /// <paramref name="context"/> is empty or null. /// </exception> private void GetProductInventory(string countryCode, string productId, string skuId, Hashtable context) { IEnumerable <InventoryItem> item; InventoryCheckRequest request; countryCode.AssertNotEmpty(nameof(countryCode)); productId.AssertNotEmpty(nameof(productId)); try { request = new InventoryCheckRequest() { TargetItems = string.IsNullOrEmpty(skuId) ? new InventoryItem[] { new InventoryItem { ProductId = productId } } : new InventoryItem[] { new InventoryItem { ProductId = productId, SkuId = skuId } }, InventoryContext = context.Cast <DictionaryEntry>().ToDictionary(kvp => (string)kvp.Key, kvp => (string)kvp.Value) }; item = Partner.Extensions.Product.ByCountry(countryCode).CheckInventory(request); WriteObject(item.Select(i => new PSInventoryItem(i)), true); } catch (PartnerException ex) { throw new PSPartnerException(null, ex); } finally { item = null; request = null; } }
/// <summary> /// Gets inventory validation results for the provided country. /// </summary> /// <param name="checkRequest">The request for the inventory check.</param> /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param> /// <returns>The inventory check results.</returns> public async Task <IEnumerable <InventoryItem> > CheckInventoryAsync(InventoryCheckRequest checkRequest, CancellationToken cancellationToken = default) { checkRequest.AssertNotNull(nameof(checkRequest)); IDictionary <string, string> parameters = new Dictionary <string, string> { { PartnerService.Instance.Configuration.Apis.CheckInventory.Parameters.Country, Context }, }; return(await Partner.ServiceClient.PostAsync <InventoryCheckRequest, IEnumerable <InventoryItem> >( new Uri( $"/{PartnerService.Instance.ApiVersion}/{PartnerService.Instance.Configuration.Apis.CheckInventory.Path}", UriKind.Relative), checkRequest, parameters, cancellationToken).ConfigureAwait(false)); }