Using “MUrl” – A textual Oslo DSL for RESTful Clients

Cesar De la Torre

image “MUrl” is a textual DSL and runtime for interacting with REST/HTTP services.

It is actually just a great Oslo-DSL sample, as you can download it (all its source code) from MSDN.com:

http://download.microsoft.com/download/4/0/B/40B632EC-F201-43EE-8E29-C398A9DA8468/MUrlSample.msi

As long as I know, “MUrl” has been mainly developed by Douglas Purdy. I saw his session at MIX09, which was fabulous; so now, I want to explain what “MUrl” is and how to use it. This post is, in fact, explaining most of the content that Doug told in MIX09.

So, Just like we already have within Oslo and “M” Language, the following DSLs, at different levels:

MGraph

MSchema (for SQL)

MGrammar (to create custom DSLs)

We will have many more Oslo DSLs, (many of them, initially as examples), like:

MUrl (DSL for consuming Services, this is the one I’m going to write about)

MService (DSL for building Services, based on WCF-REST, not released, yet)

MWeb (DSL for building Webs, based on ASP.NET, not released, yet)

MEntity (DSL for building Entity Framework graphs, etc., not released, yet)

– Your great “MCustomSmartDSL”… 😉

Ok, so “MUrl” is an “M” based domain specific language (DSL) that provides a very compact and intuitive mechanism to communicate with RESTful services. You can use it from INTELLIPAD (for testing) or you can even use its run time from a .NET program (I’ll show both in this post).

Using “MUrl” from INTELLIPAD.

Steps:

1.- First of all, install download from the URL I wrote up above, and install it. Or course, you need to have Oslo SDK already installed, as pre-requisite.

2.- Start INTELLIPAD and select “MURL Mode” like I show below:

clip_image002

3.- Then we can just start writing and executing HTTP requests!. Type:

http://live.com

Select that text line, and press Ctrl+Enter to execute that request. You’ll see the results in a pane on the right:

clip_image004

We can check that the HTTP status is OK, so we get the info. All right, It works!.

4.- Even better, now we’re going to consume RESTFul Web Services, like the TWITTER service.

I login into TWITTER, just to show what messages I have:

clip_image006

Right, so last message is the one that says: “Yes! OSLO and MUrl rocks!!”.

We can query info from TWITTER, like executing the following GET request:

GET https://twitter.com/statuses/public_timeline.xml

We get the HTTP results. Nice, nothing new, similar than before, but now we’re quering a REST Web Service:

clip_image008

5.- In this step we’re going to submit a message to TWITTER service using MUrl. Now, type the following text into INTELLIPAD:

POST https://twitter.com/statuses/update.xml

with [<status>Este lo voy a borrar desde MUrl….</status>]

as text/xml

If you just execute that POST, you’ll get a 401 Http error, because in order to submit content to TWITTER, you must be authenticated, and we’re not, yet:

clip_image010

We’re going to use WINDOWS CREDENTIALS MANAGER to create custom credentials for TWITTER. We’ll use it later on from MUrl.

Enter into Control Panel\User Accounts and Family Safety\Credential Manager:

clip_image012

As you can see, I have created a ‘Generic Credential’ called “Twitter”. You can do the same clicking on the “Add a generic credential” link.

It is simple; it is composed by a Credential-Name, User Name and a Password:

clip_image014

Remember the credential’s name, as that name we’re going to use it from MUrl, ok?. (In my case, I just called it “Twitter”).

Then, add the following text to the original MUrl text request we wrote:

“authenticate using [Twitter]”

clip_image016

And now…, It works!! :-). You can check it down below at my TWITTER web page:

clip_image018

BTW, I had to change MUrl internal code in order to run it using Authentication.

If you get the “Expectation Failed” error message, you have to fix it, as well. Take a look to this post:

See: http://www.biztalkgurus.com/blogs/biztalksyn/archive/2009/04/05/issue-with-the-first-release-of-murl-sample-and-twitter.aspx

So, there you have it!, you can use MUrl directly from INTELLIPAD.

 

Using “MUrl” from a .NET app.

Steps:

1.- Now we’re going to develop a .NET program which consumes MUrl DSL RUNTIME. It is a nice way to shrink .NET code…

In this case, I have a WPF app which is kind of a TWITTER message editor/submitter:

clip_image020

Take into account that I added two references to:

– MUrlLib

– System.DataFlow

Then, I wrote the following .NET code to be executed when the button ‘Upload Message to Twitter’ is pressed:

.NET CODE:

var runtime = new Microsoft.Languages.MUrl.MUrlRuntime();

string twitterCommand = @”POST https://twitter.com/statuses/update.xml with [<status>”

+ txtTwitterMessage.Text + “</status>] “

+ “as text/xml “

+ “authenticate using [Twitter]”;

var statements = runtime.Parse(twitterCommand);

var result = runtime.Execute(statements.ElementAt(0));

MessageBox.Show(result.AsXml().ToString());

You can see that we’re constructing the same kind of simple MUrl text but within our .NET code… And, it just works in the same way!.

Looking into MUrl DSL M-Grammar program.

Cool!. So up to now, we’ve been just using this cool textual Oslo DSL. But because of it is just a sample, we can take a look at how it is implemented.

1.- Start INTELLIPAD and open the file “murl.mg” which you can find as part of the downloaded MUrl stuff. You’ll see something like this:

clip_image022

I want to highlight that now we’re running on MGrammar Mode, just like the custom DSL sample I wrote in this blog-post:

Creating a simple Textual DSL with Oslo, “M” Language, MGrammar and Intellipad

http://blogs.msdn.com/cesardelatorre/archive/2009/04/20/creating-a-simple-textual-dsl-with-oslo-m-language-mgrammar-and-intellipad.aspx

But in this case, it is a bit more complicated, of course.

2.- Change to MGrammar-Mode–> Tree Preview

clip_image024

It will ask you for an INPUT FILE, so…, just provide the MUrl Text we were writing!! (I called “MUrl Demo Sentences.murl”).

Then, you can see how MUrl M-Grammar program is translating MUrl text to HTTP requests data (which will be used be MUrl runtime):

clip_image026

We can even change the MUrl syntax from within the “murl.mg” file, so for instance, we can now say that in order to do a GET, you can type DAME (in Spanish) or OBTAIN, as well. And it will work just the same! 😉

clip_image028

Well, you can research on it (murl.mg) a bit more, It is quite interesting…

So, here we go!, “MUrl”!, a great Oslo DSL Sample!

Thanks Doug for this nice development!

0 comments

Discussion is closed.

Feedback usabilla icon