public virtual async Task <string> SaveObservationAsync(ILookupTemplate <IFhirTemplate> config, IObservationGroup observationGroup, IDictionary <ResourceType, string> ids) { var identifier = GenerateObservationIdentifier(observationGroup, ids); var cacheKey = $"{identifier.System}|{identifier.Value}"; if (!_observationCache.TryGetValue(cacheKey, out Model.Observation existingObservation)) { existingObservation = await GetObservationFromServerAsync(identifier).ConfigureAwait(false); } Model.Observation result; if (existingObservation == null) { var newObservation = GenerateObservation(config, observationGroup, identifier, ids); result = await _client.CreateAsync <Model.Observation>(newObservation).ConfigureAwait(false); } else { var policyResult = await Policy <Model.Observation> .Handle <FhirOperationException>(ex => ex.Status == System.Net.HttpStatusCode.Conflict) .FallbackAsync(async ct => { var refreshObservation = await GetObservationFromServerAsync(identifier).ConfigureAwait(false); var mergedObservation = MergeObservation(config, refreshObservation, observationGroup); return(await _client.UpdateAsync(mergedObservation, versionAware: true).ConfigureAwait(false)); }) .ExecuteAndCaptureAsync(async() => { var mergedObservation = MergeObservation(config, existingObservation, observationGroup); return(await _client.UpdateAsync(mergedObservation, versionAware: true).ConfigureAwait(false)); }).ConfigureAwait(false); result = policyResult.Result; } _observationCache.CreateEntry(cacheKey) .SetAbsoluteExpiration(DateTimeOffset.UtcNow.AddHours(1)) .SetSize(1) .SetValue(result) .Dispose(); return(result.Id); }
public static async System.Threading.Tasks.Task <TResource> CreateResourceByIdentifierAsync <TResource>(this IFhirClient client, Identifier identifier, Action <TResource, Identifier> propertySetter = null) where TResource : Resource, new() { var resource = new TResource(); propertySetter?.Invoke(resource, identifier); // use Update to have control of the resource ID return(await client.UpdateAsync(resource)); }