Using oData with the NuGet API | John V. Petersen

:

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.