private static async Task ProcessAsyncResponse()

in Source/Internal/ServiceHelper.cs [411:457]


        private static async Task<Response> ProcessAsyncResponse(Response response, Action<int> remainingTimeCallback)
        {
            if (response != null)
            {
                if (response.ErrorDetails != null && response.ErrorDetails.Length > 0)
                {
                    throw new Exception(String.Join("", response.ErrorDetails));
                }

                if (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 AsyncStatus && !string.IsNullOrEmpty((response.ResourceSets[0].Resources[0] as AsyncStatus).RequestId))
                    {
                        var status = response.ResourceSets[0].Resources[0] as AsyncStatus;

                        status = await ServiceHelper.ProcessAsyncStatus(status, remainingTimeCallback).ConfigureAwait(false);

                        if (status != null && status.IsCompleted && !string.IsNullOrEmpty(status.ResultUrl))
                        {
                            try
                            {
                                using (var resultStream = await ServiceHelper.GetStreamAsync(new Uri(status.ResultUrl)).ConfigureAwait(false))
                                {
                                    return ServiceHelper.DeserializeStream<Response>(resultStream);
                                }
                            }
                            catch (Exception ex)
                            {
                                throw new Exception("There was an issue downloading and serializing the results. Results Download URL: " + status.ResultUrl + "\r\n" + ex.Message);
                            }
                        }

                        return response;
                    }
                    else if (response.ResourceSets[0].Resources[0] is AsyncStatus && !string.IsNullOrEmpty((response.ResourceSets[0].Resources[0] as AsyncStatus).ErrorMessage))
                    {
                        throw new Exception((response.ResourceSets[0].Resources[0] as AsyncStatus).ErrorMessage);
                    }
                    else if (response.ResourceSets[0].Resources[0] is Resource && !(response.ResourceSets[0].Resources[0] is AsyncStatus))
                    {
                        return response;
                    }
                }
            }

            throw new Exception("No response returned by service.");
        }