Beispiel #1
0
        public async Task <Bundle> SearchForResourceAsync(
            ResourceType resourceType,
            string query = null,
            int?count    = null,
            CancellationToken cancellationToken = default)
        {
            EnsureArg.IsNotNull <ResourceType>(resourceType, nameof(resourceType));

            return(await _fhirClient.SearchAsync(resourceType, query, count, cancellationToken).ConfigureAwait(false));
        }
Beispiel #2
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);
        }