private async SystemTasks.Task <FhirResponse> Request(CommandRequest request)
        {
            var internalTraceId = Guid.NewGuid();
            var fhirResponse    = new FhirResponse();

            var handler = _httpRequestHelper.GetClientHandler(request);

            using (var client = new HttpClient(handler))
            {
                var httpRequest = _httpRequestHelper.GetRequestMessage(request);

                //LogRequest(httpRequest, request.Resource, internalTraceId);

                using (HttpResponseMessage res = await client.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead))
                    using (HttpContent content = res.Content)
                    {
                        //res.EnsureSuccessStatusCode(); //will throw a HttpRequestException to catch in future

                        var mediaType = content.Headers.ContentType?.MediaType?.ToLowerInvariant();

                        if (res.Headers?.Location != null)
                        {
                            fhirResponse.ResponseLocation = res.Headers.Location;
                        }

                        //TODO: upgrade to core 2.2 and use IHttpClientFactory
                        // for delegate handling and retries policy
                        if (!string.IsNullOrEmpty(mediaType) && mediaType.Contains("fhir"))
                        {
                            fhirResponse = await ParseResource(content, request, fhirResponse);
                        }
                        else
                        {
                            fhirResponse = await ParseBinary(content, request, fhirResponse);
                        }

                        //get content from fhirResponse
                        //string responseMessage = null;


                        //LogResponse(res.Headers, (int)res.StatusCode, responseMessage, internalTraceId);

                        if (!res.IsSuccessStatusCode)
                        {
                            var diagnostics = $"{res.StatusCode} encountered for the URL {httpRequest.RequestUri.AbsoluteUri}";

                            var errorResource =
                                (fhirResponse.Resource.ResourceType != ResourceType.OperationOutcome) ?
                                OperationOutcomeFactory.CreateGenericError(diagnostics) :
                                fhirResponse.GetResource <OperationOutcome>();

                            throw new HttpFhirException("Request resulted in an error.", errorResource, res.StatusCode);
                        }
                    }
            }


            return(await SystemTasks.Task.Run(() => fhirResponse));
        }
Example #2
0
        public async Task <CommandResponse> ForwardRequest(CommandRequest request)
        {
            //var internalTraceId = Guid.NewGuid();

            var handler = _httpRequestHelper.GetClientHandler(request);

            var response = new CommandResponse();

            using (var client = new HttpClient(handler))
            {
                var httpRequest = _httpRequestHelper.GetRequestMessage(request);
                //string responseMessage = null;

                //LogRequest(httpRequest, request.Resource, internalTraceId);

                using (HttpResponseMessage res = await client.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead))
                    using (HttpContent content = res.Content)
                    {
                        try
                        {
                            //TODO: upgrade to core 2.2 and use IHttpClientFactory for delegate handling
                            var data = content.ReadAsByteArrayAsync().Result;

                            //responseMessage = Encoding.UTF8.GetString(data, 0, data.Length);

                            if (res.Headers.TransferEncodingChunked == true &&
                                res.Headers.TransferEncoding.Count == 1)
                            {
                                res.Headers.TransferEncoding.Clear();
                            }

                            foreach (var resHeader in res.Headers)
                            {
                                response.Headers.Add(resHeader.Key, resHeader.Value);
                            }


                            var contentLength = content.Headers.ContentLength;

                            foreach (var resHeader in content.Headers)
                            {
                                response.Headers.Add(resHeader.Key, resHeader.Value);
                            }

                            response.Content    = data;
                            response.StatusCode = (int)res.StatusCode;
                        }
                        catch (Exception e)
                        {
                            throw new HttpRequestException($"Parsing response from {request.ForwardUrl} failed.");
                        }
                        finally
                        {
                            //LogResponse(res.Headers, (int)res.StatusCode, responseMessage, internalTraceId);
                        }
                    }
            }


            return(await Task.Run(() => response));
        }