예제 #1
0
        private static T parseBody <T>(string body, string contentType,
                                       Func <string, ErrorList, T> xmlParser, Func <string, ErrorList, T> jsonParser) where T : class
        {
            ErrorList parseErrors = new ErrorList();
            T         result      = null;

            ContentType.ResourceFormat format = ContentType.GetResourceFormatFromContentType(contentType);

            switch (format)
            {
            case ContentType.ResourceFormat.Json:
                result = jsonParser(body, parseErrors);
                break;

            case ContentType.ResourceFormat.Xml:
                result = xmlParser(body, parseErrors);
                break;

            default:
                throw new FhirParseException("Cannot decode body: unrecognized content type " + contentType);
            }

            if (parseErrors.Count() > 0)
            {
                throw new FhirParseException("Failed to parse body: " + parseErrors.ToString(), parseErrors, body);
            }

            return(result);
        }
예제 #2
0
        public static void AssertResourceResponseConformsTo(FhirClient client, ContentType.ResourceFormat format)
        {
            AssertValidResourceContentTypePresent(client);

            var type = client.LastResponseDetails.ContentType;

            if (ContentType.GetResourceFormatFromContentType(type) != format)
            {
                TestResult.Fail(String.Format("{0} is not acceptable when expecting {1}", type, format.ToString()));
            }
        }
예제 #3
0
        private static byte[] serializeBody <T>(T data, ContentType.ResourceFormat format, Func <T, byte[]> xmlSerializer, Func <T, byte[]> jsonSerializer)
        {
            var isBundle = data is Bundle;

            if (format == ContentType.ResourceFormat.Json)
            {
                return(jsonSerializer(data)); // FhirSerializer.SerializeBundleToJsonBytes(bundle);
            }
            else if (format == ContentType.ResourceFormat.Xml)
            {
                return(xmlSerializer(data));   // FhirSerializer.SerializeBundleToXmlBytes(bundle);
            }
            else
            {
                throw new ArgumentException("Cannot encode a batch into format " + format.ToString());
            }
        }
예제 #4
0
 public static byte[] TagListBody(IEnumerable <Tag> tags, ContentType.ResourceFormat format)
 {
     return(serializeBody <IEnumerable <Tag> >(tags, format,
                                               t => FhirSerializer.SerializeTagListToXmlBytes(tags),
                                               t => FhirSerializer.SerializeTagListToJsonBytes(tags)));
 }