internal static void SetHeaders(this Bundle.BundleEntryTransactionResponseComponent interaction, WebHeaderCollection headers)
 {
     foreach (var key in headers.AllKeys)
     {
         interaction.AddExtension(EXTENSION_RESPONSE_HEADER, new FhirString(key + ":" + headers[key]));
     }
 }
Example #2
0
        public void CallsCallbacks()
        {
            FhirClient client = new FhirClient(testEndpoint);

            bool           calledBefore = false;
            HttpStatusCode?status       = null;
            Resource       res          = null;

            Bundle.BundleEntryTransactionResponseComponent interaction = null;

            client.OnBeforeRequest += (sender, e) => calledBefore = true;
            client.OnAfterResponse += (sender, e) =>
            {
                res         = e.Resource;
                status      = e.RawResponse.StatusCode;
                interaction = e.Interaction;
            };

            client.Read <Patient>("Patient/1");
            Assert.IsTrue(calledBefore);
            Assert.IsNotNull(status);
            Assert.IsNotNull(res);
            Assert.IsTrue(res is Patient);
            Assert.IsTrue(interaction.GetBodyAsText().Contains("<Patient"));
            Assert.AreEqual("application/xml+fhir; charset=UTF-8", interaction.GetHeaders().Single(t => t.Item1 == "Content-Type").Item2);
        }
 public static byte[] GetBody(this Bundle.BundleEntryTransactionResponseComponent interaction)
 {
     if (interaction.UserData.ContainsKey(USERDATA_BODY))
     {
         return((byte[])interaction.UserData[USERDATA_BODY]);
     }
     else
     {
         return(null);
     }
 }
        public static string GetBodyAsText(this Bundle.BundleEntryTransactionResponseComponent interaction)
        {
            var body = interaction.GetBody();

            if (body != null)
            {
                return(decodeBody(body, Encoding.UTF8));
            }
            else
            {
                return(null);
            }
        }
Example #5
0
        public TResource Execute <TResource>(Bundle transaction, IEnumerable <HttpStatusCode> expect) where TResource : Resource
        {
            //TODO: Handle 304 Not Modified
            var interaction = transaction.Entry.First();

            var response = doRequest(interaction);

            LastResult = response.TransactionResponse;

            if (expect.Select(sc => sc.ToString()).Contains(response.TransactionResponse.Status))
            {
                bool noBody = response.TransactionResponse.Status == HttpStatusCode.NoContent.ToString();
                if (!noBody && Prefer == Rest.Prefer.ReturnRepresentation && response.Resource == null)
                {
                    var message = String.Format("Operation {0} on {1} expected a body but none was returned", interaction.Transaction.Method,
                                                interaction.Transaction.Url);
                    throw new FhirOperationException(message);
                }

                if (response.Resource != null && !response.Resource.GetType().CanBeTreatedAsType(typeof(TResource)))
                {
                    if (response.Resource is OperationOutcome)
                    {
                        var outcome = response.Resource as OperationOutcome;
                        reportOutcome(outcome);
                        throw new FhirOperationException("Operation succeeded, but returned an OperationOutcome", outcome);
                    }
                    else
                    {
                        var message = String.Format("Operation {0} on {1} expected a body of type {2} but a {3} was returned", interaction.Transaction.Method,
                                                    interaction.Transaction.Url, typeof(TResource).Name, response.Resource.GetType().Name);
                        throw new FhirOperationException(message);
                    }
                }

                return((TResource)response.Resource);
            }
            else
            {
                var message = String.Format("Operation returned unexpected status {0}", response.TransactionResponse.Status);

                if (response.Resource is OperationOutcome)
                {
                    var outcome = response.Resource as OperationOutcome;
                    reportOutcome(outcome);
                    throw new FhirOperationException(message + ", an OperationOutcome was included in the body", outcome);
                }

                throw new FhirOperationException(message);
            }
        }
        public static IEnumerable <Tuple <string, string> > GetHeaders(this Bundle.BundleEntryTransactionResponseComponent 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 static IEnumerable <string> GetHeader(this Bundle.BundleEntryTransactionResponseComponent interaction, string header)
 {
     return(interaction.GetHeaders().Where(h => h.Item1 == header).Select(h => h.Item2));
 }
 internal static void SetBody(this Bundle.BundleEntryTransactionResponseComponent interaction, byte[] data)
 {
     interaction.UserData[USERDATA_BODY] = data;
 }