예제 #1
0
        /// <summary>
        /// Request Google Directions
        /// <param name="request">Request object</param>
        /// <returns>The response model</returns>
        /// </summary>
        public async Task <OperationResult <DirectionsResponse> > RequestDirections(DirectionsRequest request)
        {
            using (var client = new HttpClient())
            {
                client.DefaultRequestHeaders.Add("Content-Type", "application/json;charset=utf-8");

                var result = DirectionsUrlFactory.BuildDirectionsUrl(request, _apiKey);
                if (result.IsSuccessful)
                {
                    var fullUrlString  = result.Result;
                    var responseString = await client.GetStringAsync(fullUrlString);

                    return(await ProcessResponse(responseString));
                }
                else
                {
                    return(OperationResult <DirectionsResponse> .AsFailure(result.Exception));
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Request Google Directions
        /// <param name="request">Request object</param>
        /// <returns>The response model</returns>
        /// </summary>
        public async Task <OperationResult <DirectionsResponse> > RequestDirections(DirectionsRequest request)
        {
            using (var client = new HttpClient())
            {
                AcceptJsonResponse(client);
                var result = DirectionsUrlFactory.BuildDirectionsUrl(request, _apiKey);
                if (result.IsSuccessful)
                {
                    var fullUrlString = result.Result;
                    var response      = await client.GetAsync(fullUrlString).ConfigureAwait(false);

                    if (!response.IsSuccessStatusCode)
                    {
                        return(OperationResult <DirectionsResponse> .AsFailure(response.ReasonPhrase));
                    }

                    var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                    return(await ProcessResponse(content));
                }

                return(OperationResult <DirectionsResponse> .AsFailure(result.Exception));
            }
        }
예제 #3
0
        /// <summary>
        /// Constructs a Google Directions URL string based on the <paramref name="request"/> and <paramref name="apiKey"/> parameters.
        /// Note that the <paramref name="request"/> parameter must contain a value for OriginCoordinates
        /// </summary>
        /// <param name="request"></param>
        /// <param name="apiKey"></param>
        /// <returns></returns>
        public static OperationResult <string> BuildDirectionsUrl(DirectionsRequest request, string apiKey)
        {
            if (request == null || request.OriginCoordinates == null)
            {
                return(OperationResult <string> .AsFailure("Invalid request parameter specified. Please make sure to specify the OriginCoordinates"));
            }

            if (string.IsNullOrEmpty(apiKey))
            {
                return(OperationResult <string> .AsFailure("Invalid Api key specified"));
            }

            var result = new StringBuilder();

            //URL
            result.Append(_googleDirectionsApiBaseUrl);

            //Origin

            var originString = request.OriginAddress;

            if (request.OriginPlaceId != null)
            {
                originString = $"place_id:{request.OriginPlaceId}";
            }
            else if (request.OriginCoordinates != null)
            {
                originString = $"{request.OriginCoordinates.Item1},{request.OriginCoordinates.Item2}";
            }

            result.Append($"&origin={originString}");

            //Destination
            var destinationString = request.DestinationAddress;

            if (request.DestinationPlaceId != null)
            {
                destinationString = $"place_id:{request.DestinationPlaceId}";
            }
            else if (request.DestinationCoordinates != null)
            {
                destinationString = $"{request.DestinationCoordinates.Item1},{request.DestinationCoordinates.Item2}";
            }

            result.Append($"&destination={destinationString}");

            //Departure Time
            result.Append("&departure_time=now");

            //Travel Mode
            result.Append($"&mode={request.Mode.ToString().ToLower()}");

            //Tolls
            if (request.AvoidTolls)
            {
                result.Append("&avoid=tolls");
            }

            //Unit Type
            result.Append($"&units={request.UnitType.ToString().ToLower()}");

            //Region
            result.Append($"&region={request.Region.ToLower()}");

            //Traffic Model
            result.Append($"&traffic_model={request.TrafficModel.Humanize().ToLower()}");

            //API Key
            result.Append($"&key={apiKey}");

            //Sensor
            result.Append("&sensor=false");

            //Add Waypoints
            if (request.Waypoints?.Count > 0)
            {
                result.Append("&waypoints=");

                request.Waypoints.ForEach((waypoint) =>
                {
                    if (!waypoint.IsStopover)
                    {
                        result.Append("via:");
                    }

                    result.Append($"{waypoint.Latitude},{waypoint.Longitude}");
                });
            }

            return(OperationResult <string> .AsSuccess(result.ToString()));
        }