public EstimateViewModel Map(TaxiResponse response) { if (response == null) { return(null); } var vm = new EstimateViewModel { Origin = response.Origin, Destination = response.Destination, Trips = new List <TripViewModel>() }; if (response.Details == null) { return(vm); } foreach (var detail in response.Details) { var trip = new TripViewModel { Information = detail.Details, CarClass = detail.CarType.ToString(), TaxiService = detail.TaxiService.ToString() }; vm.Trips.Add(trip); } return(vm); }
public async Task SaveHistoricalDataAsync(TaxiResponse response) { foreach (var trip in response.Details) { var historicalData = new HistoricalData { OriginLatitude = response.Origin.Latitude, OriginLongitude = response.Origin.Longitude, DestinationLatitude = response.Destination.Latitude, DestinationLongitude = response.Destination.Longitude, TaxiService = trip.TaxiService.ToString(), CarType = trip.CarType.ToString(), Distance = trip.Details.Distance, Price = trip.Details.Price.Cost, MinPrice = trip.Details.Price.MinPrice, MaxPrice = trip.Details.Price.MaxPrice, SurgeMultiplier = trip.Details.Price.SurgeMultiplier, OrderDate = DateTime.Now }; await _historicalData.SaveHistoricalDataAsync(historicalData); } await _uow.CommitAsync(); }
public async Task <Response> GetTaxiAsync(string plaque, string urlBase, string servicePrefix, string controller) { //Todo dentro de un try-catch try { //1 Crear el HttpClient HttpClient client = new HttpClient {//La url viene como parámentro BaseAddress = new Uri(urlBase), }; //Definimos la url con el prefijo (api) //nombre del controlador y la placa string url = $"{servicePrefix}{controller}/{plaque}"; //Realizamos la peticion HttpResponseMessage response = await client.GetAsync(url); //la leemos en este caso com un string string result = await response.Content.ReadAsStringAsync(); //Si la comunicación falla if (!response.IsSuccessStatusCode) { return(new Response { IsSuccess = false, Message = result, }); } //Si la comunicación es exitosa //Deserealizamos el string a objeto TaxiResponse model = JsonConvert.DeserializeObject <TaxiResponse>(result); return(new Response { IsSuccess = true, Result = model }); } catch (Exception ex) { return(new Response { IsSuccess = false, Message = ex.Message }); } }
public async Task <Response> GetTaxiAsync(string plaque, string urlBase, string servicePrefix, string controller) { try { HttpClient client = new HttpClient { BaseAddress = new Uri(urlBase), }; string url = $"{servicePrefix}{controller}/{plaque}"; HttpResponseMessage response = await client.GetAsync(url); string result = await response.Content.ReadAsStringAsync(); result += "}"; if (!response.IsSuccessStatusCode) { return(new Response { IsSuccess = false, Message = result }); } TaxiResponse model = JsonConvert.DeserializeObject <TaxiResponse>(result); return(new Response { IsSuccess = true, Result = model }); } catch (Exception ex) { return(new Response { IsSuccess = false, Message = ex.Message }); } }
public async Task <TaxiResponse> EstimateOrderAsync(TaxiRequest order) { if (!_validator.ValidateOrder(order)) { throw new InvalidOperationException("Order is not valid!"); } var response = new TaxiResponse { Origin = order.Origin, Destination = order.Destination, Details = new List <TripDetail>(4) }; //// //// //// //// //// //// GOOGLE MAPS //// //// //// //// //// //// // var distanceRequest = _factory.CreateDistanceRequest(order); var distance = await _distance.GetDistanceAsync(distanceRequest); //// //// //// //// //// //// //// UBER //// //// //// //// //// //// //// var uberRequest = _factory.CreateUberRequest(order); var uberPriceResponse = await _uber.EstimatePriceAsync(uberRequest); var uberTimeResponse = await _uber.EstimateTimeAsync(uberRequest); var uberTrip = _mapper.FromUber(order, uberPriceResponse, uberTimeResponse); response.Details.Add(uberTrip); //// //// //// //// //// //// //// UKLON //// //// //// //// //// //// /// var uklonRequest = _factory.CreateUklonRequest(order); var originAddressRequest = _factory.CreateNearestAddressRequest(order); var destinationAddressRequest = _factory.CreateNearestAddressRequest(order, false); var origin = (await _uklon.SearchNearestAddressAsync(originAddressRequest)).Addresses.Single(); var destination = (await _uklon.SearchNearestAddressAsync(destinationAddressRequest)).Addresses.Single(); uklonRequest.Route.RoutePoints.Add(new Point(origin.Address, origin.HouseNumber)); uklonRequest.Route.RoutePoints.Add(new Point(destination.Address, destination.HouseNumber)); var uklonPriceResponse = await _uklon.EstimatePriceV2Async(uklonRequest); var uklonTrip = _mapper.FromUklon(order, uklonPriceResponse, distance); response.Details.Add(uklonTrip); //// //// //// //// //// //// //// BOLT //// //// //// //// //// //// //// var boltRequest = _factory.CreateBoltRequest(order); var boltPriceResponse = await _bolt.EstimatePriceAsync(boltRequest); var boltTrip = _mapper.FromBolt(order, boltPriceResponse, distance); response.Details.Add(boltTrip); //// //// //// //// //// //// //// 838 //// //// //// //// //// //// ///// var taxi838Request = _factory.CreateTaxi838Request(order); var taxi838PriceResponse = await _taxi838.EstimatePriceAsync(taxi838Request); var taxi838Trip = _mapper.FromTaxi838(order, taxi838PriceResponse, distance); response.Details.Add(taxi838Trip); //// //// //// //// //// //// //// //// //// //// //// //// //// //// //// await _historicalData.SaveHistoricalDataAsync(response); return(response); }