Prototyping with scriptcs and building in Nancy

I had a project brief to work with an API the other day at work and it wasn’t one I was familiar with. Looking at the documentation for it I knew I wanted to hit some of the endpoints, get the responses and analyse the data before I dived in and started writing anything. I am a NancyFx convert, I like how easy it is to build any kind of web/HTTP based application. The speed and lightweight feel of it just sits well with me. However in this instance I want to dive into making API requests even faster than opening Visual Studio, picking a NancyFX template and creating some routes to return data from the API via my browser.

So how to do it?

I happened to go into the scriptcs session at the NDC London conference done by one of the founding fathers Glenn Block. Could I use this?

Well the answer is yes I could and it was great fun into the bargain! I won’t go through how to install it, how cool Chocolatey is (it really is) as the guide on the site will do a better job. http://scriptcs.net

Yes, you have to know your way round what is in what namespace and get the right using statements (I have Notepad++ as my text editor, there is a plugin for scriptcs, which helps but doesn’t seem to bring in usings, I know there is a plugin for SublimeText maybe it is better? If so I would love to hear from you!). Personally I see this as a challenge to my own knowledge of the framework and part of my continued learning. Anyway it was nice to be able to jump into prototyping the API calls and seeing results quickly in the command line.

I put the following code into my text editor (removing the api key and location of the saved file on my machine) and saved it as ProjectsBoards.csx:


#r "Newtonsoft.Json.dll"

using System.Net;
using System.Collections.Specialized;
using System.IO;
using Newtonsoft.Json;

using (var client = new WebClient())
{
var data = new NameValueCollection();
client.Headers.Add("apikey", "<APIKeyValue>");

var response = client.UploadValues("http://kanbanize.com/index.php/api/kanbanize/get_projects_and_boards/format/json", "POST", data);
var responseString = Encoding.Default.GetString(response);
File.WriteAllText(@"<filelocation>\projects-and-boards.json", responseString);
}

To run this script (after installing scriptcs obviously), you open a command prompt navigate to the directory you save your file and type:

scriptcs ProjectsBoards.csx

Alternatively you could give the scriptcs command the full path to your script file.

Now I have a JSON file I can look at, analyse and determine how I can use it in my application. I can parametrise the script and pass in the API endpoint and file name of where I want the data to be saved.


#r "Newtonsoft.Json.dll"

using System.Net;
using System.Collections.Specialized;
using System.IO;
using Newtonsoft.Json;

using (var client = new WebClient())
{
var data = new NameValueCollection();
client.Headers.Add("apikey", "enter api key");

var response = client.UploadValues(Env.ScriptArgs[0], "POST", data);
var responseString = Encoding.Default.GetString(response);
File.WriteAllText(@Env.ScriptArgs[1], responseString);
}

I can call this script as follows:

scriptcs Parameterised.csx -- http://kanbanize.com/index.php/api/kanbanize/get_projects_and_boards/format/json path\to\file

Now I can hit any endpoint and write a file out with the results (reusable!). I passed each result into http://json2csharp.com/ to generate POCOs for use in the Nancy application.

I then switched into Visual Studio and created my project using the Nancy application template with ASP.NET hosting and Razor with the knowledge of what data I was working with and some POCOs to start me off. Also performing this rapid prototyping with scriptcs had got me to consider that the API call needs parametrising in my application and encapsulating correctly. I can also quickly try out optional parameters on the API calls without having to change my application and roll it back if I am not happy with the results.

By using this approach I haven’t created an application I then scrapped or refactored once I knew more about the 3rd party API I was using. Plus it was good fun to use something new and see if it improved the overall development experience.

Leave a Reply

Your email address will not be published. Required fields are marked *