Calling VSTS APIs with C#

In the last API-related article, Personal Access Tokens and VSTS APIs, we looked at how we can use Fiddler, along with a Personal Access Token (PAT), to query the Visual Studio Team Services (VSTS) REST APIs. In this post, we’ll take a look at how we can use a 3rd party Fiddler extension, Request to Code, along with the “Paste JSON as Classes” Visual Studio feature to jump start the process of calling VSTS APIs with C#.

Pre-Requisites – Install the Extension

Before we get started we must make sure we’ve installed the Fiddler extension Request to Code. On the extension page, click on the download link to download the extension. It will come down as a ZIP file that contains a DLL along with a few other files. To install the extension, simply copy the DLL into your Fiddler2 Scripts folder. On my machine, the Scripts folder has the following path:

C:\Program Files (x86)\Fiddler2\Scripts

Once the file has been copied start Fiddler up and you will notice a new Code tab:

image

We’ll see here in a moment how to make use of this new tab.

Call an API in Fiddler

For this example, we’ll call the same API that we used in the previous post to get a list of projects for a given account. The HTTP call looks something like this:

GET https://moonspace.visualstudio.com/DefaultCollection/_apis/projects?api-version=2.0

As before, we must also provide the Authorization header which includes our PAT (refer to Personal Access Tokens and VSTS APIs for a quick refresher if needed).

Running the request in Fiddler will display results similar to the following (notice the JSON results in the Response section at the bottom):

image

 

While running API requests in Fiddler is a great way for testing out APIs and parameters it isn’t all that helpful for your C# program. You still need to write the C# code that will call the APIs and deserialize the results. Luckily, there are a couple of tools that can help us out (the extension and Visual Studio feature mentioned above).

Convert the API Request to C#

Remember the Code tab shown above? Now, click on your HTTP response (in the response history list) and drag it over and drop it within the Code pane. This will display some initial C# code that you can use.

image

Next, let’s open Visual Studio and create an empty Console app and remove the boilerplate code from the Program.cs file that’s initially displayed in the IDE. At this point, your IDE should look something like this:

image

 

Back in Fiddler, ensure Read Response is checked and then click on the arrow next to Copy Code and select Copy Runnable Code. This will copy the source code, along with the various using statements into the Clipboard.

image

 

Paste the code into Visual Studio:

image

 

At this point, your code should compile. If it does not, you might have an extraneous using statement (e.g. System.Windows.Forms). If so, simply delete it from your code and re-compile.

NOTE: You can modify the list of using statements to be included by clicking on the arrow next to Run Code and selecting References.

To test the code out, place a breakpoint on line 38 and run the app. At this point you can see the results of the call:

image

 

You can see in the above pinned expression that there were three items returned – all in JSON format.

While this is a good start, let’s see what we can do about deserializing the JSON text into a couple of POCO classes.

Create the POCOs

Let’s go back to our API response in Fiddler and click on the TextView tab. From here, copy the JSON results into the Clipboard.

image

Back in Visual Studio, place the cursor just after the opening bracket for the namespace declaration and click on Edit->Paste Special->Paste JSON as Classes. This will convert the JSON to C# classes and paste it directly into your code base.

image

Resolve any missing using statements you might have at this point (e.g. for System.Collection.Generic) and compile your code to make sure everything is OK to this point.

Let’s Deserialize!

In the Solution Explorer, right-click on References and select Add Reference. Type “json” into the search field and check the Json.NET assembly and click OK.

image

 

Add the following line of code after the ReadResponse(response) call in the MakeRequests() method:

var results = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(responseText);

Your breakpoint should still be active (if not, re-add it after the newly added line shown above).

Run your code.

image

 

Notice in the Locals section above that your JSON has been deserialized into actual C# classes. Nice!

Wrapping Up

While I would not call this “production” code it is a great start to getting something up and running quickly and (relatively) easily. From here, you can tweak the code however you see fit – e.g. proper exception handling, logging, properly parameterizing the HTTP requests, etc.

A big THANKS goes out to Chad Sowald for the excellent Fiddler extension!

Also, if you know of any other shortcuts, tips, etc. that make it quicker-n-simpler to implement RESTful API call in C#, please share them with us in the comments section below.

ContactUs.com
API

Calling VSTS APIs with C#

by Jeff Bramwell time to read: 4 min
2