REST API’s are pretty cool, enabling you to do things like write your own custom reports, write your own interfaces and of course integrating load tests into your build.
Looking at the sample referenced with our REST API documentation; at 400 lines to demonstrate 4 API’s I must admit I found it pretty daunting so I thought I would see if I could simplify that somewhat.
Just like the documentation indicates: the first place you want to start is to enable alternate credentials. This is done by opening your user profile from Visual Studio Online.
And enabling alternative credentials.
At this point we are ready to start writing a sample!
Since the goal of this demo is to keep it simple I decided to use ASP.NET and let it handle all the Async goo.
To do this all you need to do is set the property Async=”True” on the page(see below).
While on the page I also tossed on a Textbox to display the result of the REST calls.
Here is the markup for that page:
<%@PageLanguage="C#"AutoEventWireup="true"async="true"CodeBehind="simple.aspx.cs"Inherits="ssimple.simple"%>
DOCTYPEhtml>
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title>title>
head>
<body>
<formid="form1"runat="server">
<div>
<asp:TextBoxID="TextBox1"runat="server"TextMode="MultiLine">asp:TextBox>
div>
form>
body>
html>
To make the code easier to read and access the functionality needed; bring in the following four namespaces and corresponding references:
using System.Globalization;
using System.Web.Configuration;
using System.Net.Http;
using System.Net.Http.Headers;
At this point I am ready to call my REST API: /TESTDROPS.
An important note: You need to use the VSCLT URI for Cloud-based load test REST API’s So the entire URL would look like:
https://
.vsclt.visualstudio.com/_apis/clt/testdrops"
While the REST API only takes one line of code (HttpClient.GetStringAsync…); a couple of lines of code are required to setup up the connection.
Since we are letting ASP.NET handle the Async handling you need to set this property in the event handler.
protectedasyncvoid Page_Load(object sender, EventArgs e)
{
HttpClient client = newHttpClient();
//setup the client connection
var vsoAccountUsername = WebConfigurationManager.AppSettings["VSOnlineAccountUsername"];
// var vsoAccountUsername = "Chass@outlook.com";
var vsoAccountPassword = WebConfigurationManager.AppSettings["VSOnlineAccountPassword"];
client.DefaultRequestHeaders.Authorization = newAuthenticationHeaderValue("Basic",
Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(string.Format(CultureInfo.InvariantCulture, "{0}:{1}", vsoAccountUsername, vsoAccountPassword))));
TextBox1.Text = await client.GetStringAsync("https://aidemo.vsclt.visualstudio.com/_apis/clt/testdrops");
}
If you are getting an HTTP 401 error when running this sample more than likely your account doesn’t have a Visual Studio Ultimate Subscription associated with it or you haven’t enabled alternate credentials.
For those of you a little more old school here is a console application version:
public static async void GetTestRuns()
{
try
{
var elsAccountUrl = new Uri(ConfigurationManager.AppSettings["ElsOnlineAccountUrl"]);
var vsoAccountUsername = ConfigurationManager.AppSettings["VSOnlineAccountUsername"];
var vsoAccountPassword = ConfigurationManager.AppSettings["VSOnlineAccountPassword"];using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
Convert.ToBase64String(
System.Text.Encoding.ASCII.GetBytes(
string.Format("{0}:{1}", vsoAccountUsername, vsoAccountPassword))));using (HttpResponseMessage response = client.GetAsync(elsAccountUrl+"/_apis/clt/testruns").Result)
{
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
Console.WriteLine(responseBody);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
If you haven’t played with App.Config files, here is an example:
https://{account}.vsclt.visualstudio.com" />
SEO Tagging: “worlds simplest REST API Sample”