Exemple #1
0
        private static async Task MainAsync()
        {
            // Start

            Console.WriteLine("Jasmin rocks! Let's update the Price List Amount...");

            // Handle errors

            try
            {
                // Create the HTTP client to send the request

                using (HttpClient client = new HttpClient())
                {
                    // Set the authorization header

                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AccessToken);

                    // Get the Sales Item to update the Price List PVP2 price amount.
                    SalesItemResource salesItem = await GetSalesItemResourceAsync(client, ItemKey);

                    // Check if PVP2 price list exists
                    var priceListLine = salesItem.PriceListLines.FirstOrDefault(item => item.PriceList.Equals("PVP2", StringComparison.OrdinalIgnoreCase));
                    if (priceListLine == null)
                    {
                        throw new Exception("Price List PVP2 does not exsits.");
                    }

                    Console.WriteLine("Price List Line - PVP2: {0} {1} {2}", priceListLine.Id, priceListLine.PriceList, priceListLine.PriceAmount.Value);

                    // Get a random number and update the price list line amount value
                    decimal value = GetRandomNumber();
                    await UpdatePriceListLinePriceAmountAsync(client, salesItem.Id, priceListLine.Id, value);

                    // Let's validate if the value was updated...

                    // Get the Sales Item with the updated Price List PVP2 price amount.
                    salesItem = await GetSalesItemResourceAsync(client, ItemKey);

                    // Check if PVP2 price list has the updated value
                    priceListLine = salesItem.PriceListLines.FirstOrDefault(item => item.PriceList.Equals("PVP2", StringComparison.OrdinalIgnoreCase));
                    if (priceListLine != null)
                    {
                        Console.WriteLine("Price List Line - PVP2: {0} {1} {2}", priceListLine.Id, priceListLine.PriceList, priceListLine.PriceAmount.Value);
                    }
                }
            }
            catch (Exception exception)
            {
                ConsoleHelper.WriteErrorLine("Error found!");
                ConsoleHelper.WriteErrorLine(exception.Message);
            }

            // End

            Console.Write("Press any key to end... ");
            Console.ReadKey();
        }
Exemple #2
0
        private static async Task <SalesItemResource> GetSalesItemResourceAsync(HttpClient client, string itemKey)
        {
            // List Sales Items by Key to get the LineID attribute
            // URI of the endpoint

            string endpoint = string.Format(CultureInfo.CurrentCulture, "{0}/{1}/{2}/salesCore/salesItems/{3}/extension", ApiBaseAddress, AccountKey, SubscriptionKey, itemKey);

            // Send the request (GET)

            Console.WriteLine("List the sales item by key...");

            Uri endpointUri = new Uri(endpoint);

            using (HttpResponseMessage response = await client.GetAsync(endpointUri))
            {
                // Failed?

                if (!response.IsSuccessStatusCode)
                {
                    string errorContent = await response.Content.ReadAsStringAsync();

                    StringBuilder sb = new StringBuilder();
                    sb.AppendLine(string.Format(CultureInfo.CurrentCulture, "The requested failed with status code {0} ({1}).", (int)response.StatusCode, response.StatusCode));

                    if (!string.IsNullOrWhiteSpace(errorContent))
                    {
                        sb.Append(string.Format(CultureInfo.CurrentCulture, "Message: {0}.", errorContent));
                    }

                    throw new InvalidOperationException(sb.ToString());
                }

                // Succeeded

                string json = await response.Content.ReadAsStringAsync();

                JsonSerializerSettings settings = new JsonSerializerSettings()
                {
                    ContractResolver = new CamelCasePropertyNamesContractResolver(),
                    Formatting       = Formatting.Indented
                };

                SalesItemResource salesItem = JsonConvert.DeserializeObject <SalesItemResource>(json);

                Console.WriteLine("The Sales item attributes were obtained with success.");
                Console.WriteLine("Sales Item: {0}.{1}", salesItem.Id, salesItem.Itemkey);

                return(salesItem);
            }
        }