Exemplo n.º 1
0
        public async Task <IHttpActionResult> GetCarData(string year, string make, string model)
        {
            HttpResponseMessage response;
            var car = new carViewModel();

            ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("https://one.nhtsa.gov/");
                try
                {
                    response = await client.GetAsync("webapi/api/Recalls/vehicle/modelyear/"
                                                     + year + "/make/" + make + "/model/" + model + "?format=json");

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

                    var result = JsonConvert.DeserializeObject <Recall>(json);
                    car.RecallResults = result.Results;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    await Task.FromResult(0);
                }
            }

            return(Ok(car));
        }
Exemplo n.º 2
0
        public async Task <IHttpActionResult> GetCarData(string model_year = "", string make = "", string model_name = "", string model_trim = "")
        {
            HttpResponseMessage response;
            var content   = "";
            var singleCar = await db.GetCar(model_year, make, model_name, model_trim);

            var car = new carViewModel
            {
                Car = singleCar,
                //Car = db.GetCars(year, make, model, trim),
                Recalls = content,
                Images  = ""
            };


            //Get recall Data

            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("http://www.nhtsa.gov/");
                try
                {
                    response = await client.GetAsync("webapi/api/Recalls/vehicle/modelyear/" + model_year + "/make/"
                                                     + make + "/model/" + model_name + "?format=json");

                    content = await response.Content.ReadAsStringAsync();
                }
                catch (Exception e)
                {
                    return(InternalServerError(e));
                }
            }

            car.Recalls = content;


            //////////////////////////////   My Bing Search   //////////////////////////////////////////////////////////

            string query = model_year + " " + make + " " + model_name + " " + model_trim;

            string rootUri = "https://api.datamarket.azure.com/Bing/Search";

            var bingContainer = new Bing.BingSearchContainer(new Uri(rootUri));

            var accountKey = ConfigurationManager.AppSettings["BingAPIKey"];;

            bingContainer.Credentials = new NetworkCredential(accountKey, accountKey);


            var imageQuery = bingContainer.Image(query, null, null, null, null, null, null);

            var imageResults = imageQuery.Execute().ToList();


            car.Images = imageResults.First().MediaUrl;

            ////////////////////////////////////////////////////////////////////////////////////////////////////////////

            return(Ok(car));
        }
Exemplo n.º 3
0
        public async Task <IHttpActionResult> getCarImage(string year, string make, string model, string trim)
        {
            var car = new carViewModel();

            //Bing Cognitive Search
            var query = year + " " + make + " " + model + " " + trim;
            var url   = $"https://api.cognitive.microsoft.com/bing/v7.0/images/" +
                        $"search?q={WebUtility.UrlEncode(query)}" +
                        $"&count=20&offset=0&mkt=en-us&safeSearch=Strict";
            var requestHeaderKey   = "Ocp-Apim-Subscription-Key";
            var requestHeaderValue = ConfigurationManager.AppSettings["bingSearchKey"];

            try
            {
                using (var client = new HttpClient())
                {
                    client.DefaultRequestHeaders.Add(requestHeaderKey, requestHeaderValue);
                    var json = await client.GetStringAsync(url);

                    var result = JsonConvert.DeserializeObject <SearchResults>(json);
                    car.ImageUrl = result.Images.Select(i => i.ContentUrl);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                await Task.FromResult(0);
            }

            return(Ok(car));
        }
Exemplo n.º 4
0
        public async Task <IHttpActionResult> GetCarData(string year, string make, string model, string trim)
        {
            HttpResponseMessage response;

            var car = new carViewModel();

            // Allow use of whichever protocol the receiving server is using.
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("https://one.nhtsa.gov/");
                try
                {
                    response = await client.GetAsync("webapi/api/Recalls/vehicle/modelyear/" + year + "/make/"
                                                     + make + "/model/" + model + "?format=json");

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

                    var result = JsonConvert.DeserializeObject <Recalls>(json);
                    car.RecallResults = result.Results;

                    //*Builds URL with data:
                    //https://one.nhtsa.gov/webapi/api/Recalls/vehicle/modelyear/2008/make/pontiac/model/g6?format=json
                    //which is sent, and await resulting data back in json format.
                    //*
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                    await Task.FromResult(0);
                }
            }
            return(Ok(car));
        }