internal static void SetHeaders(this Bundle.ResponseComponent interaction, WebHeaderCollection headers)
 {
     foreach (var key in headers.AllKeys)
     {
         interaction.AddExtension(EXTENSION_RESPONSE_HEADER, new FhirString(key + ":" + headers[key]));
     }
 }
 internal static void SetBody(this Bundle.ResponseComponent interaction, byte[] data)
 {
     interaction.RemoveAnnotations <Body>();
     interaction.AddAnnotation(new Body {
         Data = data
     });
 }
Beispiel #3
0
        /// <summary>
        /// Deserialize JSON into a FHIR Bundle#Response
        /// </summary>
        public static void DeserializeJson(this Bundle.ResponseComponent current, ref Utf8JsonReader reader, JsonSerializerOptions options)
        {
            string propertyName;

            while (reader.Read())
            {
                if (reader.TokenType == JsonTokenType.EndObject)
                {
                    return;
                }

                if (reader.TokenType == JsonTokenType.PropertyName)
                {
                    propertyName = reader.GetString();
                    if (Hl7.Fhir.Serialization.FhirSerializerOptions.Debug)
                    {
                        Console.WriteLine($"Bundle.ResponseComponent >>> Bundle#Response.{propertyName}, depth: {reader.CurrentDepth}, pos: {reader.BytesConsumed}");
                    }
                    reader.Read();
                    current.DeserializeJsonProperty(ref reader, options, propertyName);
                }
            }

            throw new JsonException($"Bundle.ResponseComponent: invalid state! depth: {reader.CurrentDepth}, pos: {reader.BytesConsumed}");
        }
Beispiel #4
0
        /// <summary>
        /// Serialize a FHIR Bundle#Response into JSON
        /// </summary>
        public static void SerializeJson(this Bundle.ResponseComponent current, Utf8JsonWriter writer, JsonSerializerOptions options, bool includeStartObject = true)
        {
            if (includeStartObject)
            {
                writer.WriteStartObject();
            }
            // Component: Bundle#Response, Export: ResponseComponent, Base: BackboneElement (BackboneElement)
            ((Hl7.Fhir.Model.BackboneElement)current).SerializeJson(writer, options, false);

            writer.WriteString("status", current.StatusElement.Value);

            if (current.LocationElement != null)
            {
                if (!string.IsNullOrEmpty(current.LocationElement.Value))
                {
                    writer.WriteString("location", current.LocationElement.Value);
                }
                if (current.LocationElement.HasExtensions() || (!string.IsNullOrEmpty(current.LocationElement.ElementId)))
                {
                    JsonStreamUtilities.SerializeExtensionList(writer, options, "_location", false, current.LocationElement.Extension, current.LocationElement.ElementId);
                }
            }

            if (current.EtagElement != null)
            {
                if (!string.IsNullOrEmpty(current.EtagElement.Value))
                {
                    writer.WriteString("etag", current.EtagElement.Value);
                }
                if (current.EtagElement.HasExtensions() || (!string.IsNullOrEmpty(current.EtagElement.ElementId)))
                {
                    JsonStreamUtilities.SerializeExtensionList(writer, options, "_etag", false, current.EtagElement.Extension, current.EtagElement.ElementId);
                }
            }

            if (current.LastModifiedElement != null)
            {
                if (current.LastModifiedElement.Value != null)
                {
                    writer.WriteString("lastModified", ((DateTimeOffset)current.LastModifiedElement.Value).ToString("yyyy-MM-dd'T'HH:mm:ss.FFFFFFFK", System.Globalization.CultureInfo.InvariantCulture));
                }
                if (current.LastModifiedElement.HasExtensions() || (!string.IsNullOrEmpty(current.LastModifiedElement.ElementId)))
                {
                    JsonStreamUtilities.SerializeExtensionList(writer, options, "_lastModified", false, current.LastModifiedElement.Extension, current.LastModifiedElement.ElementId);
                }
            }

            if (current.Outcome != null)
            {
                writer.WritePropertyName("outcome");
                JsonSerializer.Serialize <object>(writer, (Hl7.Fhir.Model.Resource)current.Outcome, options);
            }

            if (includeStartObject)
            {
                writer.WriteEndObject();
            }
        }
        public FhirTransactionResponseEntry(
            Bundle.ResponseComponent response,
            Resource resource)
        {
            EnsureArg.IsNotNull(response, nameof(response));

            Response = response;
            Resource = resource;
        }
Beispiel #6
0
 public static byte[] GetBody(this Bundle.ResponseComponent interaction)
 {
     if (interaction.UserData.ContainsKey(USERDATA_BODY))
     {
         return((byte[])interaction.UserData[USERDATA_BODY]);
     }
     else
     {
         return(null);
     }
 }
        public static byte[] GetBody(this Bundle.ResponseComponent interaction)
        {
            // [WMR 20160421] Optimization
            //if (interaction.UserData.ContainsKey(USERDATA_BODY))
            //    return (byte[])interaction.UserData[USERDATA_BODY];
            //else
            //    return null;
            object result;

            interaction.UserData.TryGetValue(USERDATA_BODY, out result);
            return((byte[])result);
        }
        public static string GetBodyAsText(this Bundle.ResponseComponent interaction)
        {
            var body = interaction.GetBody();

            if (body != null)
            {
                return(DecodeBody(body, Encoding.UTF8));
            }
            else
            {
                return(null);
            }
        }
        public void GivenARequestToCreateAPatient_WhenResponseIsOK_ThenResourceConflictExceptionShouldBeThrown()
        {
            var response = new Bundle.ResponseComponent();

            response.AddAnnotation(HttpStatusCode.OK);

            var context = new FhirTransactionContext(ChangeFeedGenerator.Generate());

            context.Request.Patient = FhirTransactionRequestEntryGenerator.GenerateDefaultCreateRequestEntry <Patient>();

            context.Response.Patient = new FhirTransactionResponseEntry(response, new Patient());

            Assert.Throws <ResourceConflictException>(() => _patientPipeline.ProcessResponse(context));
        }
        public static IEnumerable <Tuple <string, string> > GetHeaders(this Bundle.ResponseComponent interaction)
        {
            foreach (var headerExt in interaction.GetExtensions(EXTENSION_RESPONSE_HEADER))
            {
                if (headerExt.Value != null && headerExt.Value is FhirString)
                {
                    var header = ((FhirString)headerExt.Value).Value;

                    if (header != null)
                    {
                        yield return(header.SplitLeft(':'));
                    }
                }
            }
        }
        public void GivenARequestToUpdateAPatient_WhenResponseIsOK_ThenItShouldBeNoOp()
        {
            var response = new Bundle.ResponseComponent();

            response.AddAnnotation(HttpStatusCode.OK);

            var context = new FhirTransactionContext(ChangeFeedGenerator.Generate());

            context.Request.Patient = FhirTransactionRequestEntryGenerator.GenerateDefaultUpdateRequestEntry <Patient>(
                new ServerResourceId(ResourceType.Patient, "123"));

            context.Response.Patient = new FhirTransactionResponseEntry(response, new Patient());

            _patientPipeline.ProcessResponse(context);
        }
        public void TestReceiveErrorStatusWithOperationOutcomeIsHandled()
        {
            var client = new FhirClient("http://fhir2.healthintersections.com.au/open");  // an address that returns Status 404 with an OperationOutcome

            try
            {
                var pat = client.Read <Patient>("Patient/doesnotexist");
                Assert.Fail("Failed to throw an Exception on status 404");
            }
            catch (FhirOperationException fe)
            {
                // Expected exception happened
                if (fe.Status != HttpStatusCode.NotFound)
                {
                    Assert.Fail("Server response of 404 did not result in FhirOperationException with status 404.");
                }

                if (client.LastResult == null)
                {
                    Assert.Fail("LastResult not set in error case.");
                }

                Bundle.ResponseComponent entryComponent = client.LastResult;

                if (entryComponent.Status != "404")
                {
                    Assert.Fail("LastResult.Status is not 404.");
                }

                // Check that LastResult is of type OperationOutcome and properly filled.
                OperationOutcome operationOutcome = client.LastBodyAsResource as OperationOutcome;
                Assert.IsNotNull(operationOutcome, "Returned resource is not an OperationOutcome");

                Assert.IsTrue(operationOutcome.Issue.Count > 0, "OperationOutcome does not contain an issue");

                Assert.IsTrue(operationOutcome.Issue[0].Severity == OperationOutcome.IssueSeverity.Error, "OperationOutcome is not of severity 'error'");
            }
            catch (Exception e)
            {
                Assert.Fail("Failed to throw FhirOperationException on status 404: " + e.Message);
            }
        }
 public static IEnumerable <string> GetHeader(this Bundle.ResponseComponent interaction, string header)
 {
     return(interaction.GetHeaders().Where(h => h.Item1 == header).Select(h => h.Item2));
 }
 public static byte[] GetBody(this Bundle.ResponseComponent interaction) => interaction.Annotation <Body>()?.Data;
 internal static void SetBody(this Bundle.ResponseComponent interaction, byte[] data)
 {
     interaction.UserData[USERDATA_BODY] = data;
 }
        public static byte[] GetBody(this Bundle.ResponseComponent interaction)
        {
            var body = interaction.Annotation <Body>();

            return(body != null ? body.Data : null);
        }
Beispiel #17
0
        /// <summary>
        /// Deserialize JSON into a FHIR Bundle#Response
        /// </summary>
        public static void DeserializeJsonProperty(this Bundle.ResponseComponent current, ref Utf8JsonReader reader, JsonSerializerOptions options, string propertyName)
        {
            switch (propertyName)
            {
            case "status":
                if (reader.TokenType == JsonTokenType.Null)
                {
                    current.StatusElement = new FhirString();
                    reader.Skip();
                }
                else
                {
                    current.StatusElement = new FhirString(reader.GetString());
                }
                break;

            case "_status":
                if (current.StatusElement == null)
                {
                    current.StatusElement = new FhirString();
                }
                ((Hl7.Fhir.Model.Element)current.StatusElement).DeserializeJson(ref reader, options);
                break;

            case "location":
                if (reader.TokenType == JsonTokenType.Null)
                {
                    current.LocationElement = new FhirUri();
                    reader.Skip();
                }
                else
                {
                    current.LocationElement = new FhirUri(reader.GetString());
                }
                break;

            case "_location":
                if (current.LocationElement == null)
                {
                    current.LocationElement = new FhirUri();
                }
                ((Hl7.Fhir.Model.Element)current.LocationElement).DeserializeJson(ref reader, options);
                break;

            case "etag":
                if (reader.TokenType == JsonTokenType.Null)
                {
                    current.EtagElement = new FhirString();
                    reader.Skip();
                }
                else
                {
                    current.EtagElement = new FhirString(reader.GetString());
                }
                break;

            case "_etag":
                if (current.EtagElement == null)
                {
                    current.EtagElement = new FhirString();
                }
                ((Hl7.Fhir.Model.Element)current.EtagElement).DeserializeJson(ref reader, options);
                break;

            case "lastModified":
                if (reader.TokenType == JsonTokenType.Null)
                {
                    current.LastModifiedElement = new Instant();
                    reader.Skip();
                }
                else
                {
                    current.LastModifiedElement = new Instant(DateTimeOffset.Parse(reader.GetString()));
                }
                break;

            case "_lastModified":
                if (current.LastModifiedElement == null)
                {
                    current.LastModifiedElement = new Instant();
                }
                ((Hl7.Fhir.Model.Element)current.LastModifiedElement).DeserializeJson(ref reader, options);
                break;

            case "outcome":
                current.Outcome = JsonStreamResourceConverter.PolymorphicRead(ref reader, typeof(Resource), options);
                break;

            // Complex: response, Export: ResponseComponent, Base: BackboneElement
            default:
                ((Hl7.Fhir.Model.BackboneElement)current).DeserializeJsonProperty(ref reader, options, propertyName);
                break;
            }
        }