Using oData with the NuGet API

Update…

As a commenter astutely pointed out, we can see the generated URL in LinqPad..

Still good to know and understand how to use Fiddler – especially if you want to flip between json and xml formats!!

————————–

Quick tip here on using oData with NuGet.

If you have visited the NuGet Gallery, you may have wondered were the RSS feed is. Try as you might, you won’t find it. There’s good reason for that given the fact that NuGet is backed by a data feed – a data feed with many data fields that you can query to properly scope your request. NuGet has a rich API -which you will see in a moment. Usually, when we work with NuGet, we do so via the NuGet Package Manager Dialog and/or the Package Manager Console. There may be times when you wish the raw feed. To do that, you need the API url. By default, NuGet gives you one package source, the official public NuGet Feed – which can be found at https://nuget.org/api/v2. The following image illustrates how the official feed is wired into your NuGet installation. You can get to this dialog by navigating to:

Tools\Library Package Manager\Package Manger Settings

One of the most useful features in .NET is Linq and the best tool available to learn Linq is LinqPad. In the following image, I set a connection to https://nuget.org/api/v2. As you can see, there are a number of fields in the package feed. With LinqPad, it’s easy to enter a Linq Query (whether it is the long form or as a lambda expression).



Packages.OrderByDescending (x => x.LastUpdated)
   .Select (
	  x =>
		 new
		 {
			Id = x.Id,
			Tags = x.Tags,
			Title = x.Title,
			Description = x.Description,
			ProjectUrl = x.ProjectUrl,
			GalleryDetailsUrl = x.GalleryDetailsUrl,
			Version = x.Version,
			LastUpdated = x.LastUpdated
		 }
   )

In this query, we are taking certain data fields and are ordering by the LastUpdated DateTime field descending.

Another useful tool is fiddler. The combination of LinqPad and Fiddler makes it easy to see how Linq queries get translated to oData URL's:

Here's how the oData URL appears as rendered in a browser:

Here is the URL:

http://nuget.org/api/v2/Packages()?$orderby=LastUpdated%20desc&$select=Id,Tags,Title,Description,ProjectUrl,GalleryDetailsUrl,Version,LastUpdated

Going back to Fiddler, if you want JSON returned instead of XML, just tweak the Accept Header to:

Accept: application/atom+json,application/json

That's all there is to accessing NuGet data via the NuGet API with oData.

About johnvpetersen

I've been developing software for 20 years, starting with dBase, Clipper and FoxBase + thereafter, migrating to FoxPro and Visual FoxPro and Visual Basic. Other areas of concentration include Oracle and SQL Server - versions 6-2008. From 1995 to 2001, I was a Microsoft Visual FoxPro MVP. Today, my emphasis is on ASP MVC .NET applications. I am a current Microsoft ASP .NET MVP. Publishing In 1999, I wrote the definitive whitepaper on ADO for VFP Developers. In 2002, I wrote the Absolute Beginner’s Guide to Databases for Que Publishing. I was a co-author of Visual FoxPro Enterprise Development from Prima Publishing with Rod Paddock, Ron Talmadge and Eric Ranft. I was also a co-author of Visual Basic Web Development from Prima Publishing with Rod Paddock and Richard Campbell. Education - B.S Business Administration – Mansfield University - M.B.A. – Information Systems – Saint Joseph’s University - J.D. – Rutgers University School of Law (Camden) In 2004, I graduated from the Rutgers University School of Law with a Juris Doctor Degree. I passed the Pennsylvania and New Jersey Bar exams and was in private practice for several years – concentrating transactional and general business law (contracts, copyrights, trademarks, independent contractor agreements, NDA’s, intellectual property and mergers and acquisitions.).
This entry was posted in NuGet, oData and tagged , . Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • Pascal Laurin

    In LinqPad you can also click on the SQL tab in the Result panel to get the generated URL if you don’t want to go back and forth to Fiddler.

  • Pinnewip

    Just pay atttention when you order on x.Version, because version is handled as string (so alphanumeric) and for example version 11.0.0.0 is lower then 9.0.0.0 !!!