Example #1
0
        /// <summary>
        /// Send a Bundle to a path on the server
        /// </summary>
        /// <param name="bundle">The contents of the Bundle to be sent</param>
        /// <param name="path">A path on the server to send the Bundle to</param>
        /// <returns>True if the bundle was successfully delivered, false otherwise</returns>
        /// <remarks>This method differs from Batch, in that it can be used to deliver a Bundle
        /// at the endpoint for messages, documents or binaries, instead of the batched update
        /// REST endpoint.</remarks>
        public bool DeliverBundle(Bundle bundle, string path)
        {
            if (Endpoint == null)
            {
                throw new InvalidOperationException("Endpoint must be provided using either the Endpoint property or the FhirClient constructor");
            }

            byte[] data;
            string contentType = ContentType.BuildContentType(PreferredFormat, false);

            if (PreferredFormat == ContentType.ResourceFormat.Json)
            {
                data = FhirSerializer.SerializeBundleToJsonBytes(bundle);
            }
            else if (PreferredFormat == ContentType.ResourceFormat.Xml)
            {
                data = FhirSerializer.SerializeBundleToXmlBytes(bundle);
            }
            else
            {
                throw new ArgumentException("Cannot encode a batch into format " + PreferredFormat.ToString());
            }

            var req = createRequest(Endpoint, true);

            req.Method      = "POST";
            req.ContentType = contentType;
            prepareRequest(req, data);

            return(doRequest(req, HttpStatusCode.OK, () => true));
        }
Example #2
0
        private HttpWebRequest prepareRequest(string method, Uri uri, object data, IEnumerable <Tag> tags, bool expectBundleResponse)
        {
            byte[] body = null;

            Uri api = uri;

            if (UseFormatParam)
            {
                api = api.AddParam(HttpUtil.RESTPARAM_FORMAT, ContentType.BuildFormatParam(PreferredFormat));
            }

            var req = initializeRequest(api, method);

            if (!UseFormatParam)
            {
                req.Accept = ContentType.BuildContentType(PreferredFormat, forBundle: expectBundleResponse);
            }

            if (data is Binary)
            {
                var bin = (Binary)data;
                body            = bin.Content;
                req.ContentType = bin.ContentType;
            }
            else if (data is Resource)
            {
                body = PreferredFormat == ResourceFormat.Xml ?
                       FhirSerializer.SerializeResourceToXmlBytes((Resource)data) :
                       FhirSerializer.SerializeResourceToJsonBytes((Resource)data);

                req.ContentType = ContentType.BuildContentType(PreferredFormat, false);
            }
            else if (data is Bundle)
            {
                body = PreferredFormat == ResourceFormat.Xml ?
                       FhirSerializer.SerializeBundleToXmlBytes((Bundle)data) :
                       FhirSerializer.SerializeBundleToJsonBytes((Bundle)data);

                req.ContentType = ContentType.BuildContentType(PreferredFormat, true);
            }
            else if (data is TagList)
            {
                body = PreferredFormat == ResourceFormat.Xml ?
                       FhirSerializer.SerializeTagListToXmlBytes((TagList)data) :
                       FhirSerializer.SerializeTagListToJsonBytes((TagList)data);

                req.ContentType = ContentType.BuildContentType(PreferredFormat, false);
            }

            if (tags != null)
            {
                req.Headers[HttpUtil.CATEGORY] = HttpUtil.BuildCategoryHeader(tags);
            }

            if (body != null)
            {
                writeBody(req, body);
            }
            return(req);
        }
Example #3
0
        public void SetBody(Bundle bundle, ResourceFormat format)
        {
            if (bundle == null)
            {
                throw Error.ArgumentNull("bundle");
            }

            _body = format == ResourceFormat.Xml ?
                    FhirSerializer.SerializeBundleToXmlBytes(bundle, summary: false) :
                    FhirSerializer.SerializeBundleToJsonBytes(bundle, summary: false);

            _contentType = ContentType.BuildContentType(format, forBundle: true);
        }
Example #4
0
        public void AvoidBOMUse()
        {
            Bundle b = new Bundle();

            var data = FhirSerializer.SerializeBundleToJsonBytes(b);

            Assert.IsFalse(data[0] == Encoding.UTF8.GetPreamble()[0]);

            data = FhirSerializer.SerializeBundleToXmlBytes(b);
            Assert.IsFalse(data[0] == Encoding.UTF8.GetPreamble()[0]);

            Patient p = new Patient();

            data = FhirSerializer.SerializeResourceToJsonBytes(p);
            Assert.IsFalse(data[0] == Encoding.UTF8.GetPreamble()[0]);

            data = FhirSerializer.SerializeResourceToXmlBytes(p);
            Assert.IsFalse(data[0] == Encoding.UTF8.GetPreamble()[0]);
        }