public async Task <IActionResult> GetDistanceData(string origin, string destination, string mode, string unit) { // To ensure clean-up using (var client = new HttpClient()) { try { // Shorthand string.format to place parameters in the url var suffix = $"/maps/api/distancematrix/json?units={unit}&origins={origin}&destinations={destination}&mode={mode}&key={ApiKey}"; // sets the base address of the client, in this instance, Google's Distance Matrix client.BaseAddress = new Uri(BaseUrl); // Waits for a response from the suffixed address var res = await client.GetAsync(suffix); // Ensures a response is yielded res.EnsureSuccessStatusCode(); // Waits for the content to be parsed as a string. var stringRes = await res.Content.ReadAsStringAsync(); // Deserializes the string response to be a model that maps the response 1:1. var rawDistData = JsonConvert.DeserializeObject <DistanceResponseData>(stringRes); // Calls a service to further format the deserialized data. var dvm = _distance.GetDistanceViewModel(rawDistData); // Returns a status code with the parsed info return(Ok(dvm)); } catch (HttpRequestException ex) { return(BadRequest($"Getting the following error: {ex}")); } } }