Exemplo n.º 1
0
        /// <summary>
        /// When a ResourceReference is relative, use the parent resource's fullUrl (e.g. from a Bundle's entry)
        /// to make it absolute.
        /// </summary>
        /// <param name="reference">The ResourceReference to get the (possibily relative) url from</param>
        /// <param name="parentResourceUri">Absolute uri representing the location of the resource this reference is in.</param>
        /// <remarks>Implements (part of the logic) as described in bundle.html#6.7.4.1</remarks>
        /// <returns></returns>
        public static Uri GetAbsoluteUriForReference(this ResourceReference reference, Uri parentResourceUri)
        {
            if (parentResourceUri == null)
            {
                throw Error.ArgumentNull(nameof(parentResourceUri));
            }
            if (reference == null)
            {
                throw Error.ArgumentNull(nameof(reference));
            }
            if (reference.Reference == null)
            {
                return(null);
            }

            // Don't need to do anything when Uri is absolute
            var referenceUri = new Uri(reference.Reference, UriKind.RelativeOrAbsolute);

            if (referenceUri.IsAbsoluteUri)
            {
                return(referenceUri);
            }

            if (!ResourceIdentity.IsRestResourceIdentity(parentResourceUri))
            {
                throw Error.Argument(nameof(parentResourceUri), "Must be an absolute FHIR REST identity when reference is relative");
            }
            var parent = new ResourceIdentity(parentResourceUri);

            return(HttpUtil.MakeAbsoluteToBase(referenceUri, parent.BaseUri));
        }
Exemplo n.º 2
0
        public TransactionBuilder Get(string url)
        {
            var entry = newEntry(Bundle.HTTPVerb.GET, InteractionType.Unspecified);
            var uri   = new Uri(url, UriKind.RelativeOrAbsolute);

            if (uri.IsAbsoluteUri)
            {
                addEntry(entry, new RestUrl(url));
            }
            else
            {
                var absoluteUrl = HttpUtil.MakeAbsoluteToBase(uri, _baseUrl);
                addEntry(entry, new RestUrl(absoluteUrl));
            }
            return(this);
        }
        public static HttpRequestMessage ToHttpRequestMessage(this EntryRequest entry, Uri baseUrl, FhirClientSettings settings)
        {
            System.Diagnostics.Debug.WriteLine("{0}: {1}", entry.Method, entry.Url);

            if (entry.RequestBodyContent != null && !(entry.Method == HTTPVerb.POST || entry.Method == HTTPVerb.PUT))
            {
                throw Error.InvalidOperation("Cannot have a body on an Http " + entry.Method.ToString());
            }

            // Create an absolute uri when the interaction.Url is relative.
            var uri = new Uri(entry.Url, UriKind.RelativeOrAbsolute);

            if (!uri.IsAbsoluteUri)
            {
                uri = HttpUtil.MakeAbsoluteToBase(uri, baseUrl);
            }

            var location = new RestUrl(uri);

            if (settings.UseFormatParameter)
            {
                location.AddParam(HttpUtil.RESTPARAM_FORMAT, ContentType.BuildFormatParam(settings.PreferredFormat));
            }

            var request = new HttpRequestMessage(getMethod(entry.Method), location.Uri);

            request.Headers.Add("User-Agent", ".NET FhirClient for FHIR " + entry.Agent);

            if (!settings.UseFormatParameter)
            {
                request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse(ContentType.BuildContentType(settings.PreferredFormat, forBundle: false)));
            }

            if (entry.Headers.IfMatch != null)
            {
                request.Headers.Add("If-Match", entry.Headers.IfMatch);
            }
            if (entry.Headers.IfNoneMatch != null)
            {
                request.Headers.Add("If-None-Match", entry.Headers.IfNoneMatch);
            }
            if (entry.Headers.IfModifiedSince != null)
            {
                request.Headers.IfModifiedSince = entry.Headers.IfModifiedSince.Value.UtcDateTime;
            }
            if (entry.Headers.IfNoneExist != null)
            {
                request.Headers.Add("If-None-Exist", entry.Headers.IfNoneExist);
            }

            var interactionType = entry.Type;

            bool canHaveReturnPreference() => entry.Type == InteractionType.Create ||
            entry.Type == InteractionType.Update ||
            entry.Type == InteractionType.Patch;

            if (canHaveReturnPreference() && settings.PreferredReturn != null)
            {
                if (settings.PreferredReturn == Prefer.RespondAsync)
                {
                    request.Headers.Add("Prefer", PrimitiveTypeConverter.ConvertTo <string>(settings.PreferredReturn));
                }
                else
                {
                    request.Headers.Add("Prefer", "return=" + PrimitiveTypeConverter.ConvertTo <string>(settings.PreferredReturn));
                }
            }

            else if (interactionType == InteractionType.Search && settings.PreferredParameterHandling != null)
            {
                List <string> preferHeader = new List <string>();
                if (settings.PreferredParameterHandling.HasValue)
                {
                    preferHeader.Add("handling=" + settings.PreferredParameterHandling.GetLiteral());
                }
                if (settings.PreferredReturn.HasValue && settings.PreferredReturn == Prefer.RespondAsync)
                {
                    preferHeader.Add(settings.PreferredReturn.GetLiteral());
                }
                if (preferHeader.Count > 0)
                {
                    request.Headers.Add("Prefer", string.Join(", ", preferHeader));
                }
            }


            if (entry.RequestBodyContent != null)
            {
                setContentAndContentType(request, entry.RequestBodyContent, entry.ContentType, settings.PreferredFormat);
            }

            return(request);
        }
        public static HttpWebRequest ToHttpWebRequest(this EntryRequest entry, Uri baseUrl, FhirClientSettings settings)
        {
            System.Diagnostics.Debug.WriteLine("{0}: {1}", (object)entry.Method, (object)entry.Url);

            if (entry.RequestBodyContent != null && !(entry.Method == HTTPVerb.POST || entry.Method == HTTPVerb.PUT))
            {
                throw Error.InvalidOperation((string)("Cannot have a body on an Http " + entry.Method.ToString()));
            }

            // Create an absolute uri when the interaction.Url is relative.
            var uri = new Uri(entry.Url, UriKind.RelativeOrAbsolute);

            if (!uri.IsAbsoluteUri)
            {
                uri = HttpUtil.MakeAbsoluteToBase(uri, baseUrl);
            }

            var location = new RestUrl(uri);



            if (settings.UseFormatParameter)
            {
                location.AddParam(HttpUtil.RESTPARAM_FORMAT, ContentType.BuildFormatParam(settings.PreferredFormat));
            }

            var request = (HttpWebRequest)HttpWebRequest.Create(location.Uri);

            request.Method = entry.Method.ToString();
            setAgent(request, ".NET FhirClient for FHIR " + entry.Agent);

            if (!settings.UseFormatParameter)
            {
                request.Accept = ContentType.BuildContentType(settings.PreferredFormat, forBundle: false);
            }

            request.ContentType = entry.ContentType ?? ContentType.BuildContentType(settings.PreferredFormat, forBundle: false);

            if (entry.Headers.IfMatch != null)
            {
                request.Headers["If-Match"] = entry.Headers.IfMatch;
            }
            if (entry.Headers.IfNoneMatch != null)
            {
                request.Headers["If-None-Match"] = entry.Headers.IfNoneMatch;
            }
#if NETSTANDARD1_1
            if (entry.Headers.IfModifiedSince != null)
            {
                request.Headers["If-Modified-Since"] = entry.Headers.IfModifiedSince.Value.UtcDateTime.ToString();
            }
#else
            if (entry.Headers.IfModifiedSince != null)
            {
                request.IfModifiedSince = entry.Headers.IfModifiedSince.Value.UtcDateTime;
            }
#endif
            if (entry.Headers.IfNoneExist != null)
            {
                request.Headers["If-None-Exist"] = entry.Headers.IfNoneExist;
            }

            if (canHaveReturnPreference() && settings.PreferredReturn.HasValue)
            {
                if (settings.PreferredReturn == Prefer.RespondAsync)
                {
                    request.Headers["Prefer"] = PrimitiveTypeConverter.ConvertTo <string>(settings.PreferredReturn);
                }
                else
                {
                    request.Headers["Prefer"] = "return=" + PrimitiveTypeConverter.ConvertTo <string>(settings.PreferredReturn);
                }
            }
            else if (entry.Type == InteractionType.Search)
            {
                List <string> preferHeader = new List <string>();
                if (settings.PreferredParameterHandling.HasValue)
                {
                    preferHeader.Add("handling=" + settings.PreferredParameterHandling.GetLiteral());
                }
                if (settings.PreferredReturn.HasValue && settings.PreferredReturn == Prefer.RespondAsync)
                {
                    preferHeader.Add(settings.PreferredReturn.GetLiteral());
                }
                if (preferHeader.Count > 0)
                {
                    request.Headers["Prefer"] = string.Join(", ", preferHeader);
                }
            }


            bool canHaveReturnPreference() => entry.Type == InteractionType.Create ||
            entry.Type == InteractionType.Update ||
            entry.Type == InteractionType.Patch;

            // PCL doesn't support setting the length (and in this case will be empty anyway)
#if !NETSTANDARD1_1
            if (entry.RequestBodyContent == null)
            {
                request.ContentLength = 0;
            }
#endif
            return(request);
        }
        public static HttpWebRequest ToHttpRequest(this Bundle.EntryComponent entry, Uri baseUrl,
                                                   Prefer bodyPreference, ResourceFormat format, bool useFormatParameter, bool CompressRequestBody, out byte[] body)
        {
            System.Diagnostics.Debug.WriteLine("{0}: {1}", entry.Request.Method, entry.Request.Url);

            var interaction = entry.Request;

            body = null;

            if (entry.Resource != null && !(interaction.Method == Bundle.HTTPVerb.POST || interaction.Method == Bundle.HTTPVerb.PUT))
            {
                throw Error.InvalidOperation("Cannot have a body on an Http " + interaction.Method.ToString());
            }

            // Create an absolute uri when the interaction.Url is relative.
            var uri = new Uri(interaction.Url, UriKind.RelativeOrAbsolute);

            if (!uri.IsAbsoluteUri)
            {
                uri = HttpUtil.MakeAbsoluteToBase(uri, baseUrl);
            }
            var location = new RestUrl(uri);

            if (useFormatParameter)
            {
                location.AddParam(HttpUtil.RESTPARAM_FORMAT, Hl7.Fhir.Rest.ContentType.BuildFormatParam(format));
            }

            var request = (HttpWebRequest)HttpWebRequest.Create(location.Uri);

            request.Method = interaction.Method.ToString();
            setAgent(request, ".NET FhirClient for FHIR " + Model.ModelInfo.Version);

            if (!useFormatParameter)
            {
                request.Accept = Hl7.Fhir.Rest.ContentType.BuildContentType(format, forBundle: false);
            }

            if (interaction.IfMatch != null)
            {
                request.Headers["If-Match"] = interaction.IfMatch;
            }
            if (interaction.IfNoneMatch != null)
            {
                request.Headers["If-None-Match"] = interaction.IfNoneMatch;
            }
#if NETSTANDARD1_1
            if (interaction.IfModifiedSince != null)
            {
                request.Headers["If-Modified-Since"] = interaction.IfModifiedSince.Value.UtcDateTime.ToString();
            }
#else
            if (interaction.IfModifiedSince != null)
            {
                request.IfModifiedSince = interaction.IfModifiedSince.Value.UtcDateTime;
            }
#endif
            if (interaction.IfNoneExist != null)
            {
                request.Headers["If-None-Exist"] = interaction.IfNoneExist;
            }

            if (interaction.Method == Bundle.HTTPVerb.POST || interaction.Method == Bundle.HTTPVerb.PUT)
            {
                request.Headers["Prefer"] = bodyPreference == Prefer.ReturnMinimal ? "return=minimal" : "return=representation";
            }

            if (entry.Resource != null)
            {
                bool searchUsingPost =
                    interaction.Method == Bundle.HTTPVerb.POST &&
                    (entry.HasAnnotation <TransactionBuilder.InteractionType>() &&
                     entry.Annotation <TransactionBuilder.InteractionType>() == TransactionBuilder.InteractionType.Search) &&
                    entry.Resource is Parameters;

                setBodyAndContentType(request, entry.Resource, format, CompressRequestBody, searchUsingPost, out body);
            }
            // PCL doesn't support setting the length (and in this case will be empty anyway)
#if !NETSTANDARD1_1
            else
            {
                request.ContentLength = 0;
            }
#endif
            return(request);
        }