public async Task <IActionResult> GetPublishedProviderInformation(string publishedProviderVersion) { Guard.ArgumentNotNull(publishedProviderVersion, nameof(publishedProviderVersion)); string blobName = $"{publishedProviderVersion}.json"; ProviderVersionSystemCacheKey providerVersionFileSystemCacheKey = new ProviderVersionSystemCacheKey(blobName); if (_cacheSettings.IsEnabled && _fileSystemCache.Exists(providerVersionFileSystemCacheKey)) { await using Stream providerVersionDocumentStream = _fileSystemCache.Get(providerVersionFileSystemCacheKey); ProviderVersionSearchResult cachedProviderVersionSearchResult = providerVersionDocumentStream.AsPoco <ProviderVersionSearchResult>(); return(new OkObjectResult(cachedProviderVersionSearchResult)); } (string providerVersionId, string providerId)results = await _publishedFundingRepositoryPolicy.ExecuteAsync(() => _publishedFundingRepository.GetPublishedProviderId(publishedProviderVersion)); if (string.IsNullOrEmpty(results.providerVersionId) || string.IsNullOrEmpty(results.providerId)) { _logger.Error($"Failed to retrieve published provider with publishedProviderVersion: {publishedProviderVersion}"); return(new NotFoundResult()); } ApiResponse <ProvidersApiClientModel.Search.ProviderVersionSearchResult> apiResponse = await _providersApiClientPolicy.ExecuteAsync(() => _providersApiClient.GetProviderByIdFromProviderVersion(results.providerVersionId, results.providerId)); if (apiResponse?.Content == null || !apiResponse.StatusCode.IsSuccess()) { string errorMessage = $"Failed to retrieve GetProviderByIdFromProviderVersion with " + $"providerVersionId: {results.providerVersionId} and providerId: {results.providerId}"; _logger.Error(errorMessage); return(new InternalServerErrorResult(errorMessage)); } ProviderVersionSearchResult providerVersionSearchResult = _mapper.Map <ProviderVersionSearchResult>(apiResponse.Content); if (_cacheSettings.IsEnabled) { if (!_fileSystemCache.Exists(providerVersionFileSystemCacheKey)) { await using MemoryStream stream = new MemoryStream(providerVersionSearchResult.AsJsonBytes()); _fileSystemCache.Add(providerVersionFileSystemCacheKey, stream, ensureFolderExists: true); } } return(new OkObjectResult(providerVersionSearchResult)); }
public async Task <string> GetFundingFeedDocument(string absoluteDocumentPathUrl, bool isForPreLoad = false) { Guard.IsNullOrWhiteSpace(absoluteDocumentPathUrl, nameof(absoluteDocumentPathUrl)); string documentPath = ParseDocumentPathRelativeToBlobContainerFromFullUrl(absoluteDocumentPathUrl); string fundingFeedDocumentName = Path.GetFileNameWithoutExtension(documentPath); FundingFileSystemCacheKey fundingFileSystemCacheKey = new FundingFileSystemCacheKey(fundingFeedDocumentName); if (_cacheSettings.IsEnabled && _fileSystemCache.Exists(fundingFileSystemCacheKey)) { if (isForPreLoad) { return(null); } Stream fundingDocumentStream = _fileSystemCache.Get(fundingFileSystemCacheKey); using BinaryReader binaryReader = new BinaryReader(fundingDocumentStream); return(GetDocumentContentFromBytes(binaryReader.ReadBytes((int)fundingDocumentStream.Length))); } ICloudBlob blob = _blobClient.GetBlockBlobReference(documentPath); if (!blob.Exists()) { _logger.Error($"Failed to find blob with path: {documentPath}"); return(null); } using (MemoryStream fundingDocumentStream = (MemoryStream)await _publishedFundingRepositoryPolicy.ExecuteAsync(() => _blobClient.DownloadToStreamAsync(blob))) { if (fundingDocumentStream == null || fundingDocumentStream.Length == 0) { _logger.Error($"Invalid blob returned: {documentPath}"); return(null); } if (_cacheSettings.IsEnabled) { if (!_fileSystemCache.Exists(fundingFileSystemCacheKey)) { _fileSystemCache.Add(fundingFileSystemCacheKey, fundingDocumentStream); } } fundingDocumentStream.Position = 0; return(isForPreLoad ? null : GetDocumentContentFromBytes(fundingDocumentStream.ToArray())); } }
public async Task SetAssembly(string specificationId, Stream assembly) { Guard.IsNullOrWhiteSpace(specificationId, nameof(specificationId)); Guard.ArgumentNotNull(assembly, nameof(assembly)); ICloudBlob assemblyBlob = await GetAssemblyCloudBlob(specificationId); Guard.ArgumentNotNull(assemblyBlob, nameof(assemblyBlob)); await assemblyBlob.FetchAttributesAsync(); _fileSystemCache.Add(GetFileSystemCacheKey(specificationId, assemblyBlob.Properties.ETag), assembly, ensureFolderExists: true); }
public async Task <IActionResult> GetProviderFundingVersion(string providerFundingVersion) { if (string.IsNullOrWhiteSpace(providerFundingVersion)) { return(new BadRequestObjectResult("Null or empty id provided.")); } string blobName = $"{providerFundingVersion}.json"; try { ProviderFundingFileSystemCacheKey cacheKey = new ProviderFundingFileSystemCacheKey(providerFundingVersion); if (_cacheSettings.IsEnabled && _fileSystemCache.Exists(cacheKey)) { using Stream cachedStream = _fileSystemCache.Get(cacheKey); return(await GetContentResultForStream(cachedStream)); } bool exists = await _blobClientPolicy.ExecuteAsync(() => _blobClient.BlobExistsAsync(blobName)); if (!exists) { _logger.Error($"Blob '{blobName}' does not exist."); return(new NotFoundResult()); } ICloudBlob blob = await _blobClientPolicy.ExecuteAsync(() => _blobClient.GetBlobReferenceFromServerAsync(blobName)); using Stream blobStream = await _blobClientPolicy.ExecuteAsync(() => _blobClient.DownloadToStreamAsync(blob)); if (_cacheSettings.IsEnabled) { _fileSystemCache.Add(cacheKey, blobStream); } return(await GetContentResultForStream(blobStream)); } catch (Exception ex) { string errorMessage = $"Failed to fetch blob '{blobName}' from azure storage"; _logger.Error(ex, errorMessage); return(new InternalServerErrorResult(errorMessage)); } }
public async Task <IActionResult> FetchCoreProviderData(string specificationId) { Guard.IsNullOrWhiteSpace(specificationId, nameof(specificationId)); bool fileSystemCacheEnabled = _scopedProvidersServiceSettings.IsFileSystemCacheEnabled; if (fileSystemCacheEnabled) { _fileSystemCache.EnsureFoldersExist(ScopedProvidersFileSystemCacheKey.Folder); } string filesystemCacheKey = $"{CacheKeys.ScopedProviderSummariesFilesystemKeyPrefix}{specificationId}"; string cacheGuid = await _cachePolicy.ExecuteAsync(() => _cacheProvider.GetAsync <string>(filesystemCacheKey)); if (cacheGuid.IsNullOrEmpty()) { cacheGuid = Guid.NewGuid().ToString(); await _cacheProvider.SetAsync(filesystemCacheKey, cacheGuid, TimeSpan.FromDays(7), true); } ScopedProvidersFileSystemCacheKey cacheKey = new ScopedProvidersFileSystemCacheKey(specificationId, cacheGuid); if (fileSystemCacheEnabled && _fileSystemCache.Exists(cacheKey)) { await using Stream cachedStream = _fileSystemCache.Get(cacheKey); return(GetActionResultForStream(cachedStream, specificationId)); } IEnumerable <ProviderSummary> providerSummaries = await GetScopedProvidersForSpecification(specificationId); if (providerSummaries.IsNullOrEmpty()) { return(new NoContentResult()); } await using MemoryStream stream = new MemoryStream(providerSummaries.AsJsonBytes()) { Position = 0 }; if (fileSystemCacheEnabled) { _fileSystemCache.Add(cacheKey, stream); } return(GetActionResultForStream(stream, specificationId)); }
public async Task <IActionResult> GetAllProviders(string providerVersionId) { Guard.IsNullOrWhiteSpace(providerVersionId, nameof(providerVersionId)); string blobName = $"{providerVersionId.ToLowerInvariant()}.json"; bool fileSystemCacheEnabled = _providerVersionServiceSettings.IsFileSystemCacheEnabled; if (fileSystemCacheEnabled) { _fileSystemCache.EnsureFoldersExist(ProviderVersionFileSystemCacheKey.Folder); } ProviderVersionFileSystemCacheKey cacheKey = new ProviderVersionFileSystemCacheKey(providerVersionId); if (fileSystemCacheEnabled && _fileSystemCache.Exists(cacheKey)) { await using Stream cachedStream = _fileSystemCache.Get(cacheKey); return(GetActionResultForStream(cachedStream, providerVersionId)); } ICloudBlob blob = _blobClient.GetBlockBlobReference(blobName); if (!blob.Exists()) { _logger.Error($"Failed to find blob with path: {blobName}"); return(new NotFoundResult()); } await using Stream blobClientStream = await _blobRepositoryPolicy.ExecuteAsync(() => _blobClient.DownloadToStreamAsync(blob)); if (fileSystemCacheEnabled) { _fileSystemCache.Add(cacheKey, blobClientStream); } return(GetActionResultForStream(blobClientStream, providerVersionId)); }