Exemple #1
0
        public async Task <ConditionalPushResult> PushPackageIfNotExistsAsync(PackageSource source, Stream package)
        {
            var identity = _packageReader.GetPackageIdentity(package);

            package.Position = 0;

            var packageResultBeforePush = await GetPackageEntryAsync(source, identity);

            if (packageResultBeforePush.StatusCode == HttpStatusCode.OK)
            {
                return(new ConditionalPushResult
                {
                    PackageAlreadyExists = true,
                    PackageResult = packageResultBeforePush,
                });
            }

            var stopwatch      = Stopwatch.StartNew();
            var pushStatusCode = await PushPackageAsync(source, package);

            var timeToPush        = stopwatch.Elapsed;
            var packageStatusCode = HttpStatusCode.NotFound;
            HttpResult <PackageEntry> packageResultAfterPush = null;

            while (packageStatusCode == HttpStatusCode.NotFound &&
                   stopwatch.Elapsed < TimeSpan.FromMinutes(20))
            {
                packageResultAfterPush = await GetPackageEntryAsync(source, identity);

                packageStatusCode = packageResultAfterPush.StatusCode;
                if (packageStatusCode == HttpStatusCode.NotFound)
                {
                    await Task.Delay(TimeSpan.FromSeconds(1));
                }
            }

            return(new ConditionalPushResult
            {
                PackageAlreadyExists = false,
                PackageResult = packageResultAfterPush,
                PackagePushSuccessfully = packageStatusCode == HttpStatusCode.OK,
                PushStatusCode = pushStatusCode,
                TimeToPush = timeToPush,
                TimeToBeAvailable = packageStatusCode == HttpStatusCode.OK ? stopwatch.Elapsed : (TimeSpan?)null,
            });
        }
        private async Task <Dictionary <PackageEntrySource, PackageEntry> > GetPackageEntries(PackageSource source)
        {
            var sourceToPackageEntry = new Dictionary <PackageEntrySource, PackageEntry>();

            // Push then fetch.
            var pushResult = await _client.PushPackageIfNotExistsAsync(source, _testData.PackageFullMetadata);

            if (pushResult.PackageResult.Data == null)
            {
                throw new InvalidOperationException("The package was not pushed successfully.");
            }
            sourceToPackageEntry[PackageEntrySource.GetPackageFirst] = pushResult.PackageResult.Data;

            // Fetch again. This should return same result as the previous request since it's using the exact same API.
            var packageIdentity  = _packageReader.GetPackageIdentity(_testData.PackageFullMetadata);
            var getPackageResult = await _client.GetPackageEntryAsync(source, packageIdentity);

            if (getPackageResult.Data == null)
            {
                throw new InvalidOperationException("The package could not be fetched from using the ID and version keys.");
            }
            sourceToPackageEntry[PackageEntrySource.GetPackageSecond] = getPackageResult.Data;

            // Fetch from the packages collection using a simple query that could be easily send to another data store.
            var simpleFilterResult = await _client.GetPackageEntryFromCollectionWithSimpleFilterAsync(
                source,
                packageIdentity);

            if (simpleFilterResult.Data == null)
            {
                throw new InvalidOperationException("The package could not be fetched from the packages collection using a simple filter.");
            }
            sourceToPackageEntry[PackageEntrySource.PackageCollectionWithSimpleFilter] = simpleFilterResult.Data;

            // Fetch from the packages collection using a tricky query that should go to the database.
            var customFilterResult = await _client.GetPackageEntryFromCollectionWithCustomFilterAsync(
                source,
                packageIdentity);

            if (customFilterResult.Data == null)
            {
                throw new InvalidOperationException("The package could not be fetched from the packages collection using a custom filter.");
            }
            sourceToPackageEntry[PackageEntrySource.PackageCollectionWithCustomFilter] = customFilterResult.Data;

            return(sourceToPackageEntry);
        }