Exemplo n.º 1
0
        private async Task <TResource> SearchByQueryParameterAsync <TResource>(string queryParameter, CancellationToken cancellationToken)
            where TResource : Resource, new()
        {
            EnsureArg.IsNotNullOrEmpty(queryParameter, nameof(queryParameter));

            string fhirTypeName = ModelInfo.GetFhirTypeNameForType(typeof(TResource));

            if (!Enum.TryParse(fhirTypeName, out ResourceType resourceType))
            {
                Debug.Assert(false, "Resource type could not be parsed from TResource");
            }

            Bundle bundle = await _fhirClient.SearchAsync(
                resourceType,
                queryParameter,
                count : null,
                cancellationToken);

            int       matchCount = 0;
            TResource result     = null;

            while (bundle != null)
            {
                matchCount += bundle.Entry.Count;

                if (matchCount > 1)
                {
                    // Multiple matches.
                    throw new MultipleMatchingResourcesException(typeof(TResource).Name);
                }
                else if (bundle?.Entry.Count == 1)
                {
                    // There was only one match but because the server could return empty continuation token
                    // with more results, we need to follow the links to make sure there are no additional matching resources.
                    result = (TResource)bundle.Entry[0].Resource;
                }

                if (bundle.NextLink != null)
                {
                    bundle = await _fhirClient.SearchAsync(bundle.NextLink.ToString(), cancellationToken);
                }
                else
                {
                    break;
                }
            }

            // Validate to make sure the resource is valid.
            if (result != null)
            {
                _fhirResourceValidator.Validate(result);
            }

            return(result);
        }
Exemplo n.º 2
0
        private async Task <IEnumerable <TResource> > SearchByQueryParameterAsync <TResource>(string queryParameter, int?maxCount, CancellationToken cancellationToken)
            where TResource : Resource, new()
        {
            EnsureArg.IsNotNullOrEmpty(queryParameter, nameof(queryParameter));

            string fhirTypeName = ModelInfo.GetFhirTypeNameForType(typeof(TResource));

            if (!Enum.TryParse(fhirTypeName, out ResourceType resourceType))
            {
                Debug.Assert(false, "Resource type could not be parsed from TResource");
            }

            Bundle bundle = await _fhirClient.SearchAsync(
                resourceType,
                queryParameter,
                count : null,
                cancellationToken);

            int matchCount = 0;
            var results    = new List <TResource>();

            while (bundle != null)
            {
                matchCount += bundle.Entry.Count;

                if (matchCount > maxCount)
                {
                    // Multiple matches.
                    throw new MultipleMatchingResourcesException(typeof(TResource).Name);
                }

                results.AddRange(bundle.Entry.Select(x => (TResource)x.Resource));

                if (bundle.NextLink != null)
                {
                    bundle = await _fhirClient.SearchAsync(bundle.NextLink.ToString(), cancellationToken);
                }
                else
                {
                    break;
                }
            }

            // Validate to make sure the resource is valid.
            foreach (TResource result in results)
            {
                _fhirResourceValidator.Validate(result);
            }

            return(results);
        }