public static FhirResult SetETagHeader(this FhirResult fhirResult, WeakETag weakETag) { if (weakETag != null) { fhirResult.Headers.Add(HeaderNames.ETag, weakETag.ToString()); } return(fhirResult); }
public async Task <ExportJobOutcome> ReplaceExportJobAsync(ExportJobRecord jobRecord, WeakETag eTag, CancellationToken cancellationToken) { EnsureArg.IsNotNull(jobRecord, nameof(jobRecord)); var cosmosExportJob = new CosmosExportJobRecordWrapper(jobRecord); var requestOptions = new RequestOptions() { PartitionKey = new PartitionKey(CosmosDbExportConstants.ExportJobPartitionKey), }; // Create access condition so that record is replaced only if eTag matches. if (eTag != null) { requestOptions.AccessCondition = new AccessCondition() { Type = AccessConditionType.IfMatch, Condition = eTag.ToString(), }; } try { ResourceResponse <Document> replaceResult = await _documentClient.ReplaceDocumentAsync( UriFactory.CreateDocumentUri(_cosmosDataStoreConfiguration.DatabaseId, _collectionConfiguration.CollectionId, jobRecord.Id), cosmosExportJob, requestOptions, cancellationToken : cancellationToken); var latestETag = replaceResult.Resource.ETag; return(new ExportJobOutcome(jobRecord, WeakETag.FromVersionId(latestETag))); } catch (DocumentClientException dce) { if (dce.StatusCode == HttpStatusCode.RequestEntityTooLarge) { throw new RequestRateExceededException(dce.RetryAfter); } if (dce.StatusCode == HttpStatusCode.PreconditionFailed) { throw new ResourceConflictException(eTag); } if (dce.StatusCode == HttpStatusCode.NotFound) { throw new JobNotFoundException(string.Format(Core.Resources.JobNotFound, jobRecord.Id)); } _logger.LogError(dce, "Unhandled Document Client Exception"); throw; } }