public static DVLAData getDVLADetails(string vrm) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://moneysupermarket-prod.apigee.net/gb/reference-data/v0/vehicles?registrationNumber=" + vrm); request.Headers.Add("client_id", "EHtEf346HoQnp54Rj7RuGbg1eGAuAwIN"); request.Method = "GET"; HttpWebResponse response = request.GetResponse() as HttpWebResponse; StreamReader reader = new StreamReader(response.GetResponseStream()); string json = reader.ReadToEnd(); json.TrimStart(new char[] { '{' }).TrimEnd(new char[] { '}' }); JObject data = JObject.Parse(json); JObject cardata = JObject.Parse(data["vehicles"][0].ToString()); string fuel; switch (cardata["engineTypeId"].ToString()) { case "1": fuel = "Petrol"; break; case "2": fuel = "Diesel"; break; case "3": fuel = "Electric"; break; default: fuel = "Unknown"; break; } DVLAData car = new DVLAData { vrm = vrm, make = cardata["makeName"].ToString(), model = cardata["modelName"].ToString(), varient = cardata["variantName"].ToString(), year = Convert.ToInt32(cardata["manufacturedYear"].ToString()), fuel = fuel }; return(car); }
public IActionResult Car(string vrm) { vrm = vrm.Replace(" ", ""); ViewData["Car"] = VehicleProvider.getVehicleFromVRM(vrm); try { ViewData["DVLACar"] = VehicleProvider.getDVLADetails(vrm); } catch { ViewData["DVLACar"] = new DVLAData(); } ViewData["vrm"] = vrm; return(View()); }
public IActionResult AddVehicle(string vrm) { if (!isAuthenticated()) { return(RedirectToAction("Login", "Auth")); } vrm = vrm.Replace(" ", ""); try { ViewData["DVLADetails"] = VehicleProvider.getDVLADetails(vrm); } catch { ViewData["DVLADetails"] = new DVLAData(); } ViewData["vrm"] = vrm; return(View()); }