internal CosmosClientStreamWrapper( IResourcePermissionResponse resourcePermissionResponse, PermissionModeKind permissionMode) { var currentPermission = resourcePermissionResponse?.ResourcePermissions? .FirstOrDefault(p => p?.PermissionMode == permissionMode); _partitionKey = new PartitionKey(currentPermission?.PartitionKey); _partitionKeyStr = currentPermission?.PartitionKey; _partitionKeyHeader = resourcePermissionResponse?.PartitionKeyHeader; try { _cosmosClient = new CosmosClient(resourcePermissionResponse?.EndpointUrl, currentPermission?.ResourceToken); _container = _cosmosClient .GetDatabase(resourcePermissionResponse?.DatabaseId) .GetContainer(resourcePermissionResponse?.CollectionId); } catch (Exception ex) { Debug.WriteLine($"Unable to instantiate {nameof(CosmosClientStreamWrapper)}. Unhandled exception {ex}"); throw new CosmosClientException($"Unable to instantiate {nameof(CosmosClientStreamWrapper)}. Unhandled exception {ex}", ex); } }
// local function evaluating the validity of the object stored in the cache. static Task <bool> IsCachedObjectValidFunc(IResourcePermissionResponse cachedPermissionObj) { if (cachedPermissionObj is null) { return(Task.FromResult(false)); } // Get the Permission that is closed to expire. var expires = cachedPermissionObj.ResourcePermissions?.OrderBy(resourcePermission => resourcePermission.ExpiresUtc) .FirstOrDefault() ?.ExpiresUtc ?? default; // Set expiration permission five minutes before actual expiration to be on safe side. return(Task.FromResult(DateTime.UtcNow <= expires - TimeSpan.FromMinutes(5))); }