Beispiel #1
0
        /// <summary>
        /// Executes the request.
        /// </summary>
        /// <param name="remainingTimeCallback">A callback function in which the estimated remaining time in seconds is sent.</param>
        /// <returns>A response containing the requested data.</returns>
        public override async Task <Response> Execute(Action <int> remainingTimeCallback)
        {
            Stream responseStream = null;

            GetMetadata = true;

            if (Pushpins != null && Pushpins.Count > 18)
            {
                //Make a post request when there are more than 18 pushpins as there is a risk of URL becoming too large for a GET request.
                responseStream = await ServiceHelper.PostStringAsync(new Uri(GetPostRequestUrl()), GetPushpinsAsString(), null);
            }
            else
            {
                responseStream = await ServiceHelper.GetStreamAsync(new Uri(GetRequestUrl()));
            }

            if (responseStream != null)
            {
                var r = ServiceHelper.DeserializeStream <Response>(responseStream);
                responseStream.Dispose();

                return(r);
            }

            return(null);
        }
        /// <summary>
        /// Makes an Async request and monitors it till completion.
        /// </summary>
        /// <typeparam name="T">The type of resource to expect to be returned.</typeparam>
        /// <param name="requestUrl">REST URL request.</param>
        /// <param name="requestBody">The post request body.</param>
        /// <param name="remainingTimeCallback">A callback function in which the estimated remaining time in seconds is sent.</param>
        /// <returns>A completed response for a request.</returns>
        internal static async Task <Response> MakeAsyncPostRequest <T>(string requestUrl, string requestBody, Action <int> remainingTimeCallback) where T : Resource
        {
            Response response = null;

            using (var responseStream = await ServiceHelper.PostStringAsync(new Uri(requestUrl), requestBody, "application/json"))
            {
                response = ServiceHelper.DeserializeStream <Response>(responseStream);
                return(await ProcessAsyncResponse <T>(response, remainingTimeCallback));
            }
        }
Beispiel #3
0
        /// <summary>
        /// Processes a REST requests that returns data.
        /// </summary>
        /// <param name="request">The REST request to process.</param>
        /// <returns>The response from the REST service.</returns>
        public static async Task <Response> GetResponseAsync(BaseRestRequest request)
        {
            Stream responseStream = null;

            if (request is ElevationRequest)
            {
                var r = request as ElevationRequest;

                if (r.Points != null && r.Points.Count > 50)
                {
                    //Make a post request when there are more than 50 points as there is a risk of URL becoming too large for a GET request.
                    responseStream = await ServiceHelper.PostStringAsync(new Uri(r.GetPostRequestUrl()), r.GetPointsAsString(), null);
                }
                else
                {
                    responseStream = await ServiceHelper.GetStreamAsync(new Uri(r.GetRequestUrl()));
                }
            }
            else if (request is ImageryRequest)
            {
                var r = request as ImageryRequest;

                r.GetMetadata = true;

                if (r.Pushpins != null && r.Pushpins.Count > 18)
                {
                    //Make a post request when there are more than 18 pushpins as there is a risk of URL becoming too large for a GET request.
                    responseStream = await ServiceHelper.PostStringAsync(new Uri(r.GetPostRequestUrl()), r.GetPushpinsAsString(), null);
                }
                else
                {
                    responseStream = await ServiceHelper.GetStreamAsync(new Uri(r.GetRequestUrl()));
                }
            }
            else
            {
                responseStream = await ServiceHelper.GetStreamAsync(new Uri(request.GetRequestUrl()));
            }

            if (responseStream != null)
            {
                var ser = new DataContractJsonSerializer(typeof(Response));
                var r   = ser.ReadObject(responseStream) as Response;
                responseStream.Dispose();
                return(r);
            }

            return(null);
        }
        /// <summary>
        /// Processes a REST requests that returns an image stream.
        /// </summary>
        /// <param name="imageryRequest">The REST request to process.</param>
        /// <returns>A stream containing an image.</returns>
        public static async Task <Stream> GetImageAsync(BaseImageryRestRequest imageryRequest)
        {
            if (imageryRequest is ImageryRequest)
            {
                var r = imageryRequest as ImageryRequest;

                r.GetMetadata = false;

                if (r.Pushpins != null && (r.Pushpins.Count > 18 || r.Style != null))
                {
                    //Make a post request when there are more than 18 pushpins as there is a risk of URL becoming too large for a GET request.
                    return(await ServiceHelper.PostStringAsync(new Uri(r.GetPostRequestUrl()), r.GetPushpinsAsString(), null).ConfigureAwait(false));
                }
                else
                {
                    return(await ServiceHelper.GetStreamAsync(new Uri(r.GetRequestUrl())).ConfigureAwait(false));
                }
            }
            else
            {
                return(await ServiceHelper.GetStreamAsync(new Uri(imageryRequest.GetRequestUrl())).ConfigureAwait(false));
            }
        }
Beispiel #5
0
        /// <summary>
        /// Executes the request.
        /// </summary>
        /// <param name="remainingTimeCallback">A callback function in which the estimated remaining time in seconds is sent.</param>
        /// <returns>A response containing the requested data.</returns>
        public override async Task <Response> Execute(Action <int> remainingTimeCallback)
        {
            Stream responseStream = null;

            if (Points != null && Points.Count > 50)
            {
                //Make a post request when there are more than 50 points as there is a risk of URL becoming too large for a GET request.
                responseStream = await ServiceHelper.PostStringAsync(new Uri(GetPostRequestUrl()), GetPointsAsString(), null).ConfigureAwait(false);
            }
            else
            {
                responseStream = await ServiceHelper.GetStreamAsync(new Uri(GetRequestUrl())).ConfigureAwait(false);
            }

            if (responseStream != null)
            {
                var r = ServiceHelper.DeserializeStream <Response>(responseStream);
                responseStream.Dispose();
                return(r);
            }

            return(null);
        }
Beispiel #6
0
        /// <summary>
        /// Executes the request.
        /// </summary>
        /// <param name="remainingTimeCallback">A callback function in which the estimated remaining time is sent.</param>
        /// <returns>A response containing the requested distance matrix.</returns>
        public override async Task <Response> Execute(Action <int> remainingTimeCallback)
        {
            //Make sure all origins and destinations are geocoded.
            await GeocodeWaypoints();

            var requestUrl  = GetRequestUrl();
            var requestBody = GetPostRequestBody();

            Response response = null;

            using (var responseStream = await ServiceHelper.PostStringAsync(new Uri(requestUrl), requestBody, "application/json"))
            {
                response = ServiceHelper.DeserializeStream <Response>(responseStream);
            }

            if (response != null && response.ErrorDetails != null && response.ErrorDetails.Length > 0)
            {
                throw new Exception("Error: " + response.ErrorDetails[0]);
            }

            if (response != null && response.ResourceSets != null && response.ResourceSets.Length > 0 && response.ResourceSets[0].Resources != null && response.ResourceSets[0].Resources.Length > 0)
            {
                if (response.ResourceSets[0].Resources[0] is DistanceMatrixAsyncStatus && !string.IsNullOrEmpty((response.ResourceSets[0].Resources[0] as DistanceMatrixAsyncStatus).RequestId))
                {
                    var status    = response.ResourceSets[0].Resources[0] as DistanceMatrixAsyncStatus;
                    var statusUrl = new Uri(status.CallbackUrl);
                    //var statusUrl = new Uri(this.Domain + "Routes/DistanceMatrixAsyncCallback?requestId=" + status.RequestId + "&key=" + this.BingMapsKey);

                    if (status.CallbackInSeconds > 0 || !status.IsCompleted || string.IsNullOrEmpty(status.ResultUrl))
                    {
                        remainingTimeCallback?.Invoke(status.CallbackInSeconds);

                        //Wait remaining seconds.
                        await Task.Delay(TimeSpan.FromSeconds(status.CallbackInSeconds));

                        status = await MonitorAsyncStatus(statusUrl, 0, remainingTimeCallback);
                    }

                    if (status != null)
                    {
                        if (status.IsCompleted && !string.IsNullOrEmpty(status.ResultUrl))
                        {
                            try
                            {
                                using (var resultStream = await ServiceHelper.GetStreamAsync(new Uri(status.ResultUrl)))
                                {
                                    DistanceMatrix dm = ServiceHelper.DeserializeStream <DistanceMatrix>(resultStream);

                                    response.ResourceSets[0].Resources[0] = dm;

                                    //TODO: Overwrite origins/destinations for now as we have added support for geocoding in this library, but this is not yet supported by the Distance Matirx API.
                                    dm.Origins      = this.Origins.ToArray();
                                    dm.Destinations = this.Destinations.ToArray();
                                }
                            }
                            catch (Exception ex)
                            {
                                response.ResourceSets[0].Resources[0] = new DistanceMatrix()
                                {
                                    ErrorMessage = "There was an issue downloading and serializing the results. Results Download URL: " + status.ResultUrl
                                };
                            }
                        }
                        else if (!status.IsAccepted)
                        {
                            response.ResourceSets[0].Resources[0] = new DistanceMatrix()
                            {
                                ErrorMessage = "The request was not accepted."
                            };
                        }
                        else if (!string.IsNullOrEmpty(status.ErrorMessage))
                        {
                            response.ResourceSets[0].Resources[0] = new DistanceMatrix()
                            {
                                ErrorMessage = status.ErrorMessage
                            };
                        }
                    }

                    return(response);
                }
                else if (response.ResourceSets[0].Resources[0] is DistanceMatrix && (response.ResourceSets[0].Resources[0] as DistanceMatrix).Results != null)
                {
                    DistanceMatrix dm = response.ResourceSets[0].Resources[0] as DistanceMatrix;

                    //TODO: Overwrite origins/destinations for now as we have added support for geocoding in this library, but this is not yet supported by the Distance Matirx API.
                    dm.Origins      = this.Origins.ToArray();
                    dm.Destinations = this.Destinations.ToArray();

                    if (dm.Results != null)
                    {
                        return(response);
                    }
                    else if (!string.IsNullOrEmpty(dm.ErrorMessage))
                    {
                        var msg = "Error: " + (response.ResourceSets[0].Resources[0] as DistanceMatrix).ErrorMessage;
                        throw new Exception(msg);
                    }
                }
                else if (response.ResourceSets[0].Resources[0] is DistanceMatrixAsyncStatus && !string.IsNullOrEmpty((response.ResourceSets[0].Resources[0] as DistanceMatrixAsyncStatus).ErrorMessage))
                {
                    var msg = "Error: " + (response.ResourceSets[0].Resources[0] as DistanceMatrixAsyncStatus).ErrorMessage;
                    throw new Exception(msg);
                }
                else if (response.ResourceSets[0].Resources[0] is DistanceMatrix && !string.IsNullOrEmpty((response.ResourceSets[0].Resources[0] as DistanceMatrix).ErrorMessage))
                {
                    var msg = "Error: " + (response.ResourceSets[0].Resources[0] as DistanceMatrix).ErrorMessage;
                    throw new Exception(msg);
                }
            }

            return(null);
        }