Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1340 Improve support for Binary endpoint #2474

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/Hl7.Fhir.Base/Rest/BaseFhirClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1079,7 +1079,7 @@ private static ResourceIdentity verifyResourceIdentity(Uri location, bool needId
// of the server, add a suggestion about this in the (legacy) parsing exception.
var suggestedVersionOnParseError = !Settings.VerifyFhirVersion ? fhirVersion : null;
(LastResult, LastBody, LastBodyAsText, LastBodyAsResource, var issue) =
await ValidateResponse(responseMessage, expect, _serializationEngine, suggestedVersionOnParseError)
await ValidateResponse(responseMessage, expect, _serializationEngine, suggestedVersionOnParseError, useBinaryProtocol: false)
.ConfigureAwait(false);

// If an error occurred while trying to interpret and validate the response, we will bail out now.
Expand Down Expand Up @@ -1135,10 +1135,15 @@ static string unexpectedBodyType(Bundle.RequestComponent rc) => $"Operation {rc.
/// <exception cref="FhirOperationException">The body content type could not be handled or the response status indicated failure, or we received an unexpected success status.</exception>
/// <exception cref="FormatException">Thrown when the original ITypedElement-based parsers are used and a parse exception occurred.</exception>
/// <exception cref="DeserializationFailedException">Thrown when a newer parsers is used and a parse exception occurred.</exception>
/// <seealso cref="HttpContentParsers.ExtractResponseData(HttpResponseMessage, IFhirSerializationEngine)"/>
internal static async Task<ResponseData> ValidateResponse(HttpResponseMessage responseMessage, IEnumerable<HttpStatusCode> expect, IFhirSerializationEngine engine, string? suggestedVersionOnParseError)
{
var responseData = (await responseMessage.ExtractResponseData(engine).ConfigureAwait(false))
/// <seealso cref="HttpContentParsers.ExtractResponseData(HttpResponseMessage, IFhirSerializationEngine, bool)"/>
internal static async Task<ResponseData> ValidateResponse(
HttpResponseMessage responseMessage,
IEnumerable<HttpStatusCode> expect,
IFhirSerializationEngine engine,
string? suggestedVersionOnParseError,
bool useBinaryProtocol)
{
var responseData = (await responseMessage.ExtractResponseData(engine, useBinaryProtocol).ConfigureAwait(false))
.TranslateUnsupportedBodyTypeException(responseMessage.StatusCode)
.TranslateLegacyParserException(suggestedVersionOnParseError);

Expand Down
4 changes: 2 additions & 2 deletions src/Hl7.Fhir.Base/Rest/EntryToHttpExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace Hl7.Fhir.Rest


internal static class EntryToHttpExtensions
{
{
public static HttpRequestMessage ToHttpRequestMessage(
this Bundle.EntryComponent entry,
Uri baseUrl,
Expand Down Expand Up @@ -63,7 +63,7 @@ HttpRequestMessage setBody(HttpRequestMessage message)

message = entry.Resource switch
{
Binary bin => message.WithBinaryContent(bin),
Binary binaryData when settings.BinaryTransfer is BinaryTransferBehaviour.UseData => message.WithBinaryContent(binaryData),
Parameters pars when isSearchUsingPost => message.WithFormUrlEncodedParameters(pars),
Resource resource => message.WithResourceContent(resource, serialization, ser, fhirVersion),
null => message.WithNoBody()
Expand Down
30 changes: 30 additions & 0 deletions src/Hl7.Fhir.Base/Rest/FhirClientSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#nullable enable

using Hl7.Fhir.Model;
using Hl7.Fhir.Serialization;
using Hl7.Fhir.Utility;
using System;
Expand Down Expand Up @@ -128,6 +129,17 @@ public bool CompressRequestBody
/// </summary>
public ParserSettings? ParserSettings = ParserSettings.CreateDefault();

/// <summary>
/// How to transfer binary data when sending data to a Binary endpoint.
/// </summary>
public BinaryTransferBehaviour BinarySendBehaviour = BinaryTransferBehaviour.UseData;

/// <summary>
/// How to transfer binary data when receiving data from a Binary endpoint.
/// </summary>
public BinaryTransferBehaviour BinaryReceiveBehaviour = BinaryTransferBehaviour.UseData;


public FhirClientSettings() { }

/// <summary>Clone constructor. Generates a new <see cref="FhirClientSettings"/> instance initialized from the state of the specified instance.</summary>
Expand Down Expand Up @@ -158,6 +170,8 @@ public void CopyTo(FhirClientSettings other)
other.PreferredParameterHandling = PreferredParameterHandling;
other.SerializationEngine = SerializationEngine;
other.RequestBodyCompressionMethod = RequestBodyCompressionMethod;
other.BinaryReceiveBehaviour = BinaryReceiveBehaviour;
other.BinarySendBehaviour = BinarySendBehaviour;
}

/// <summary>Creates a new <see cref="FhirClientSettings"/> object that is a copy of the current instance.</summary>
Expand All @@ -166,6 +180,22 @@ public void CopyTo(FhirClientSettings other)
/// <summary>Creates a new <see cref="FhirClientSettings"/> instance with default property values.</summary>
public static FhirClientSettings CreateDefault() => new();
}

/// <summary>
/// Describes how the client sends and receives data at the Binary endpoint.
/// </summary>
public enum BinaryTransferBehaviour
{
/// <summary>
/// Prefer to package binary data in a <see cref="Binary"/> resource.
/// </summary>
UseResource,

/// <summary>
/// Prefer to send and receive the binary data directly to and from the endpoint.
/// </summary>
UseData
}
}

#nullable restore
10 changes: 5 additions & 5 deletions src/Hl7.Fhir.Base/Rest/HttpContentBuilders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@
#nullable enable

using Hl7.Fhir.Model;
using Hl7.Fhir.Serialization;
using Hl7.Fhir.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Linq;
using Hl7.Fhir.Serialization;
using System;
using System.Text.Unicode;
using System.Text;
using System.Net;

namespace Hl7.Fhir.Rest
{
Expand All @@ -34,6 +33,7 @@ public static HttpContent CreateContentFromBinary(Binary b)
{
var content = new ByteArrayContent(b.Data ?? b.Content);
content.Headers.ContentType = MediaTypeHeaderValue.Parse(b.ContentType);
content.Headers.LastModified = b.Meta?.LastUpdated;

if (b.SecurityContext?.Reference is { } secRef)
content.Headers.Add(HttpUtil.SECURITYCONTEXT, secRef);
Expand Down
Loading
Loading