protected static async Task <TResource> CreateResourceByIdentityAsync <TResource>(IFhirClient client, Model.Identifier identifier, Action <TResource, Model.Identifier> propertySetter) where TResource : Model.Resource, new() { EnsureArg.IsNotNull(client, nameof(client)); EnsureArg.IsNotNull(identifier, nameof(identifier)); var resource = new TResource(); propertySetter?.Invoke(resource, identifier); return(await client.CreateAsync <TResource>(resource).ConfigureAwait(false)); }
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 || ex.Status == System.Net.HttpStatusCode.PreconditionFailed) .RetryAsync(2, async(polyRes, attempt) => { existingObservation = await GetObservationFromServerAsync(identifier).ConfigureAwait(false); }) .ExecuteAndCaptureAsync(async() => { var mergedObservation = MergeObservation(config, existingObservation, observationGroup); return(await _client.UpdateAsync(mergedObservation, versionAware: true).ConfigureAwait(false)); }).ConfigureAwait(false); var exception = policyResult.FinalException; if (exception != null) { throw exception; } result = policyResult.Result; } _observationCache.CreateEntry(cacheKey) .SetAbsoluteExpiration(DateTimeOffset.UtcNow.AddHours(1)) .SetSize(1) .SetValue(result) .Dispose(); return(result.Id); }