AzureMLDemo.MainPage.CallAzureML C# (CSharp) Method

CallAzureML() private method

Utility method calling Azure ML Service with specified parameters
private CallAzureML ( string make, string bodyStyle, double wheelBase, string numberOfCylinders, int engineSize, int horsepowers, int peakRPM, int highwayMPG ) : Task
make string
bodyStyle string
wheelBase double
numberOfCylinders string
engineSize int
horsepowers int
peakRPM int
highwayMPG int
return Task
        private async Task<float> CallAzureML(string make, string bodyStyle, double wheelBase, string numberOfCylinders, int engineSize, int horsepowers, int peakRPM, int highwayMPG)
        {
            // builds request body anonymous class
            var requestBody = new
            {
                Inputs = new Dictionary<string, object>()
                {
                    {
                        "input",
                        new
                        {
                            ColumnNames = new string[] {"make", "body-style", "wheel-base", "num-of-cylinders", "engine-size", "horsepower", "peak-rpm", "highway-mpg", "price"},
                            Values = new string[,] {  { make, bodyStyle, wheelBase.ToString(), numberOfCylinders, engineSize.ToString(), horsepowers.ToString(), peakRPM.ToString(), highwayMPG.ToString(), "0" } }
                        }
                    }
                },
                GlobalParameters = new Dictionary<string, int>()
            };

            try
            {
                // opens http client and sets some connection parameters
                using (var client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", _apiKey);
                    client.BaseAddress = new Uri(_requestUri);

                    // makes request
                    var response = await client.PostAsJsonAsync("", requestBody);

                    // checks response status
                    if (response.IsSuccessStatusCode)
                    {
                        // if status code is ok, gets response as a string and parses it to dynamic class using Newtonsoft.Json 
                        var responseContent = await response.Content.ReadAsStringAsync();
                        var val = JsonConvert.DeserializeObject<dynamic>(responseContent);

                        // attempts to get a price value and returns it if it is not null
                        float? price = (float)val?.Results?.prediction?.value?.Values[0][0];
                        if (price == null)
                            throw new InvalidDataException("Response message has unknown format or is empty.");

                        return (float)price;
                    }
                    else
                    {
                        // if status code is bad, gets error response as string and formats it to good-looking json by twofold (de)serialization 
                        var responseContent = await response.Content.ReadAsStringAsync();
                        var responseObject = JsonConvert.DeserializeObject(responseContent);
                        var formattedResponseContent = JsonConvert.SerializeObject(responseObject, Formatting.Indented);

                        var message = String.Format("Server returned error status code {0} with message {1}", response.StatusCode, formattedResponseContent);
                        throw new InvalidDataException(message);
                    }
                }
            }
            catch (Exception e)
            {
                throw;
            }
        }
    }