Exemple #1
0
        private async Task <FhirResponseMessage> CreateOrUpdateAsync(FhirRequestMessage request)
        {
            var relativeUrl = request.Resource.ResourceType.GetLiteral();

            if (request.Method == HttpMethod.Put)
            {
                relativeUrl += $"/{request.Resource.Id}";
            }
            // Build REST url
            var endpoint = $"{Endpoint}{relativeUrl}";

            // Build Request Message
            var requestMessage = new HttpRequestMessage(request.Method, endpoint);

            requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _accessToken);
            requestMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue(request.MediaType));
            requestMessage.Content = new StringContent(request.GetContentAsString(), Encoding.UTF8, request.MediaType);

            // Initialzie HttpClient and Send the request
            var client = new HttpClient();

            using var response = await client.SendAsync(requestMessage);

            if (!response.IsSuccessStatusCode)
            {
                throw new HttpRequestException($"HTTP Request failed with status code: '{response.StatusCode}' and reason: '{response.ReasonPhrase}'");
            }

            return(new FhirResponseMessage((int)response.StatusCode, response.Content.Headers.ContentType.MediaType, await response.Content.ReadAsStringAsync()));
        }
Exemple #2
0
        private async Task <ResourceWrapper> UpdateUsingHttpClientAsync(ResourceWrapper resourceWrapper)
        {
            var serializer = new SerializationWrapper(FhirVersion);
            var request    = new FhirRequestMessage(HttpMethod.Put, resourceWrapper);
            var response   = await CreateOrUpdateAsync(request);

            return(serializer.Parse(response.Content, response.MimeType));
        }