Ejemplo n.º 1
0
        /// <summary>
        /// Execute a single endpoint.
        /// </summary>
        /// <typeparam name="T">Result type.</typeparam>
        /// <param name="endpointInfo">Endpoint to try</param>
        /// <param name="executeWithBaseUrl">Process the endpoint</param>
        /// <param name="retryCount">Retry count</param>
        /// <param name="backoffStrategy">>Given original wait time between retries, get a new wait time.</param>
        /// <returns>Result of executeWithBaseUrl</returns>
        internal T SingleExecuteWithRetries <T>(NuGetServiceInfo endpointInfo, Func <string, T> executeWithBaseUrl, int retryCount, Func <int, int> backoffStrategy)
        {
            int sleepInMs = 0;
            // I don't think there's a huge penalty to collecting an individual service's errors over the course of the retries, and it could be helpful for debugging
            IList <Exception> allErrors = new List <Exception>();

            while (retryCount-- > 0)
            {
                try
                {
                    return(executeWithBaseUrl(endpointInfo.Url));
                }
                catch (Exception ex)
                {
                    allErrors.Add(ex);
                }

                // If we get here, the previous attempt has failed. Recalculate the sleep time and wait.
                if (retryCount > 0)
                {
                    sleepInMs = backoffStrategy(sleepInMs);
                    Task.Delay(sleepInMs).Wait();
                }
            }

            // If we get here, we've tried {retryCount} times without success
            throw new AggregateException(allErrors);
        }
Ejemplo n.º 2
0
        private bool IsSingleEndpointAvailable(NuGetServiceInfo endpoint, RequestWrapper request)
        {
            HttpClient          client   = request.GetClient();
            HttpResponseMessage response = PathUtility.GetHttpResponse(client, endpoint.Url, (() => request.IsCanceled()),
                                                                       ((msg, num) => request.Verbose(Resources.Messages.RetryingDownload, msg, num)), (msg) => request.Verbose(msg), (msg) => request.Debug(msg));

            if (response != null)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
        /// <summary>
        /// Extract a NuGet v3 endpoint from the service's entry root in NuGet v3's service index
        /// </summary>
        public static NuGetServiceInfo GetV3Endpoint(dynamic serviceEntryRoot)
        {
            string typeName, serviceUrl;

            typeName = serviceUrl = null;
            if (serviceEntryRoot.Metadata.HasProperty("type"))
            {
                typeName = serviceEntryRoot.Metadata.type.ToLowerInvariant();
            }

            if (serviceEntryRoot.Metadata.HasProperty("id"))
            {
                serviceUrl = serviceEntryRoot.Metadata.id;
            }

            if (KnownServiceTypeStrings.ContainsKey(typeName))
            {
                NuGetServiceInfo templateServiceInfo = KnownServiceTypeStrings[typeName];
                return(new NuGetServiceInfo(serviceUrl, templateServiceInfo.Type, templateServiceInfo.Preference));
            }

            return(null);
        }
Ejemplo n.º 4
0
 public NuGetPackageFeed3(NuGetServiceInfo primaryServiceEndpoint)
 {
     this.Endpoints.Add(primaryServiceEndpoint);
 }
Ejemplo n.º 5
0
 public NuGetQueryFeed3(NuGetServiceInfo primaryServiceEndpoint)
 {
     this.Endpoints.Add(primaryServiceEndpoint);
 }
 public NuGetAutoCompleteFeed3(NuGetServiceInfo primaryServiceEndpoint)
 {
     this.Endpoints.Add(primaryServiceEndpoint);
 }
        public static NuGetResourceCollection3 Make(dynamic root, string baseUrl, RequestWrapper request)
        {
            request.Debug(Messages.DebugInfoCallMethod3, "NuGetResourceCollection3", "Make", baseUrl);
            NuGetResourceCollection3 collection = new NuGetResourceCollection3();
            Dictionary <NuGetServiceType, NuGetServiceInfo> currentServiceMap = new Dictionary <NuGetServiceType, NuGetServiceInfo>();

            foreach (dynamic resourceElement in root.resources)
            {
                NuGetServiceInfo serviceInfo = NuGetServiceInfo.GetV3Endpoint(resourceElement);
                if (serviceInfo == null)
                {
                    continue;
                }

                bool serviceUsed       = currentServiceMap.ContainsKey(serviceInfo.Type) && currentServiceMap[serviceInfo.Type].Preference <= serviceInfo.Preference;
                bool serviceSupplement = currentServiceMap.ContainsKey(serviceInfo.Type) && currentServiceMap[serviceInfo.Type].Preference == serviceInfo.Preference;
                switch (serviceInfo.Type)
                {
                case NuGetServiceType.AutoComplete:
                    // No feed yet OR no version (lowest possible stable version) OR greater version
                    if (serviceUsed || collection.AutoCompleteFeed == null)
                    {
                        serviceUsed = true;
                        if (serviceSupplement)
                        {
                            ((NuGetAutoCompleteFeed3)collection.AutoCompleteFeed).Endpoints.Add(serviceInfo);
                        }
                        else
                        {
                            collection.AutoCompleteFeed = new NuGetAutoCompleteFeed3(serviceInfo);
                        }
                    }

                    break;

                case NuGetServiceType.Registrations:
                    if (serviceUsed || collection.PackagesFeed == null)
                    {
                        serviceUsed = true;
                        if (serviceSupplement)
                        {
                            ((NuGetPackageFeed3)collection.PackagesFeed).Endpoints.Add(serviceInfo);
                        }
                        else
                        {
                            collection.PackagesFeed = new NuGetPackageFeed3(serviceInfo);
                        }
                    }

                    break;

                case NuGetServiceType.Query:
                    if (serviceUsed || collection.QueryFeed == null)
                    {
                        serviceUsed = true;
                        if (serviceSupplement)
                        {
                            ((NuGetQueryFeed3)collection.QueryFeed).Endpoints.Add(serviceInfo);
                        }
                        else
                        {
                            collection.QueryFeed = new NuGetQueryFeed3(serviceInfo);
                        }
                    }

                    break;

                case NuGetServiceType.Files:
                    if (serviceUsed || collection.FilesFeed == null)
                    {
                        serviceUsed          = true;
                        collection.FilesFeed = new NuGetFilesFeed3(serviceInfo.Url);
                    }

                    break;

                case NuGetServiceType.ReportAbuse:
                    if (serviceUsed || collection.AbuseFeed == null)
                    {
                        serviceUsed          = true;
                        collection.AbuseFeed = new NuGetAbuseFeed3(serviceInfo.Url);
                    }

                    break;
                }

                if (serviceUsed)
                {
                    request.Debug(Messages.NuGetEndpointDiscovered, serviceInfo.Type, serviceInfo.Url);
                    if (!serviceSupplement)
                    {
                        currentServiceMap[serviceInfo.Type] = serviceInfo;
                    }
                }
            }

            collection.GetSearchQueryDelegate = (searchTerms) =>
            {
                string searchString = String.Empty;
                // TODO: encode search terms?
                foreach (NuGetSearchTerm searchTerm in searchTerms)
                {
                    switch (searchTerm.Term)
                    {
                    case NuGetSearchTerm.NuGetSearchTermType.SearchTerm:
                        searchString += searchTerm.Text;
                        break;

                    case NuGetSearchTerm.NuGetSearchTermType.Tag:
                        HttpQueryBuilder tQb = new HttpQueryBuilder().Add(Constants.TagQueryParam, searchTerm.Text, separator: ":", encode: false);
                        searchString += " " + tQb.ToQueryString();
                        break;

                    case NuGetSearchTerm.NuGetSearchTermType.Contains:
                        HttpQueryBuilder cQb = new HttpQueryBuilder().Add(Constants.DescriptionQueryParam, searchTerm.Text, separator: ":", encode: false);
                        searchString += " " + cQb.ToQueryString();
                        break;

                    default:
                        break;
                    }
                }

                return(searchString.Trim());
            };

            collection.PackageConverter              = new PackageBaseConverter();
            collection.PackageDependencyConverter    = new PackageDependencyConverter();
            collection.PackageDependencySetConverter = new DependencyGroupConverter();
            collection.CatalogUrlConverter           = new CatalogUrlConverter();
            Uri uri = new Uri(baseUrl);

            if (uri.Host.Contains(Constants.NuGetOrgHost))
            {
                collection.GalleryFeed = new NuGetGalleryFeedOrg();
            }
            else if (uri.Host.Contains(Constants.MyGetOrgHost))
            {
                collection.GalleryFeed = new NuGetGalleryFeedMyGet(baseUrl);
            }

            request.Debug(Messages.DebugInfoReturnCall, "NuGetResourceCollection3", "Make");
            return(collection);
        }