public Task<AzureMlBatchResponse> InvokeBatchAsync(AzureMlBatchRequest request)
 {
     return Task.FromResult(
         new AzureMlBatchResponse()
         {
             Lines = request.Trends
                 .Select(
                     x => new AzureMlBatchResponse.ResponseLine()
                     {
                         ProductId = x.ProductId,
                         Reorder = x.LastStockCount < 150,
                         Probability = (float) this.random.NextDouble()
                     })
                 .ToList()
         });
 }
 public Task <AzureMlBatchResponse> InvokeBatchAsync(AzureMlBatchRequest request)
 {
     return(Task.FromResult(
                new AzureMlBatchResponse()
     {
         Lines = request.Trends
                 .Select(
             x => new AzureMlBatchResponse.ResponseLine()
         {
             ProductId = x.ProductId,
             Reorder = x.LastStockCount < 150,
             Probability = (float)this.random.NextDouble()
         })
                 .ToList()
     }));
 }
        public async Task<AzureMlBatchResponse> InvokeBatchAsync(AzureMlBatchRequest request)
        {
            using (HttpClient client = new HttpClient())
            {
                var scoreRequest = new
                {
                    Inputs = new Dictionary<string, StringTable>
                    {
                        {
                            "input1",
                            new StringTable
                            {
                                ColumnNames =
                                    new[]
                                    {
                                        "ProductId",
                                        "ConsumptionRate",
                                        "AverageTimeBetweenPurchases",
                                        "AveragePurchaseSize",
                                        "Reorder",
                                        "StockLevel",
                                        "TotalOrders"
                                    },
                                Values = request.Trends.Select(
                                    trend => new[]
                                    {
                                        trend.ProductId.ToString(),
                                        trend.AvgPurchasesPerDay.ToString(),
                                        trend.AvgTimeBetweenPurchases.ToString(),
                                        trend.AvgQuantityPerOrder.ToString(),
                                        "True",
                                        trend.LastStockCount.ToString(),
                                        trend.TotalPurchases.ToString()
                                    })
                                    .ToArray()
                            }
                        },
                    },
                    GlobalParameters = new Dictionary<string, string>
                    {
                    }
                };

                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", this.apiKey);
                client.BaseAddress = this.serviceUri;

                HttpResponseMessage response = await client.PostAsJsonAsync("", scoreRequest);

                if (response.IsSuccessStatusCode)
                {
                    string resultString = await response.Content.ReadAsStringAsync();
                    JObject resultObject = JObject.Parse(resultString);

                    JToken arrValues = resultObject["Results"]["output1"]["value"]["Values"];
                    return new AzureMlBatchResponse()
                    {
                        Lines = arrValues.Select(
                            values => new AzureMlBatchResponse.ResponseLine()
                            {
                                ProductId = values[0].Value<int>(),
                                Reorder = values[7].Value<bool>(),
                                Probability = values[8].Value<float>()
                            }).ToList()
                    };
                }
                else
                {
                    Logger.Debug($"The request failed with status code: {response.StatusCode}");

                    // Print the headers - they include the requert ID and the timestamp, which are useful for debugging the failure
                    Logger.Debug(response.Headers.ToString());

                    string responseContent = await response.Content.ReadAsStringAsync();
                    Logger.Debug(responseContent);
                    throw new InvalidOperationException(responseContent);
                }
            }
        }
        public async Task <AzureMlBatchResponse> InvokeBatchAsync(AzureMlBatchRequest request)
        {
            using (HttpClient client = new HttpClient())
            {
                var scoreRequest = new
                {
                    Inputs = new Dictionary <string, StringTable>
                    {
                        {
                            "input1",
                            new StringTable
                            {
                                ColumnNames =
                                    new[]
                                {
                                    "ProductId",
                                    "ConsumptionRate",
                                    "AverageTimeBetweenPurchases",
                                    "AveragePurchaseSize",
                                    "Reorder",
                                    "StockLevel",
                                    "TotalOrders"
                                },
                                Values = request.Trends.Select(
                                    trend => new[]
                                {
                                    trend.ProductId.ToString(),
                                    trend.AvgPurchasesPerDay.ToString(),
                                    trend.AvgTimeBetweenPurchases.ToString(),
                                    trend.AvgQuantityPerOrder.ToString(),
                                    "True",
                                    trend.LastStockCount.ToString(),
                                    trend.TotalPurchases.ToString()
                                })
                                         .ToArray()
                            }
                        },
                    },
                    GlobalParameters = new Dictionary <string, string>
                    {
                    }
                };

                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", this.apiKey);
                client.BaseAddress = this.serviceUri;

                HttpResponseMessage response = await client.PostAsJsonAsync("", scoreRequest);

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

                    JObject resultObject = JObject.Parse(resultString);

                    JToken arrValues = resultObject["Results"]["output1"]["value"]["Values"];
                    return(new AzureMlBatchResponse()
                    {
                        Lines = arrValues.Select(
                            values => new AzureMlBatchResponse.ResponseLine()
                        {
                            ProductId = values[0].Value <int>(),
                            Reorder = values[7].Value <bool>(),
                            Probability = values[8].Value <float>()
                        }).ToList()
                    });
                }
                else
                {
                    Logger.Debug($"The request failed with status code: {response.StatusCode}");

                    // Print the headers - they include the requert ID and the timestamp, which are useful for debugging the failure
                    Logger.Debug(response.Headers.ToString());

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

                    Logger.Debug(responseContent);
                    throw new InvalidOperationException(responseContent);
                }
            }
        }