Using “MUrl” – A textual Oslo DSL for RESTful Clients
|“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:
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:
– 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.
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:
3.- Then we can just start writing and executing HTTP requests!. Type:
Select that text line, and press Ctrl+Enter to execute that request. You’ll see the results in a pane on the right:
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:
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:
We get the HTTP results. Nice, nothing new, similar than before, but now we’re quering a REST Web Service:
5.- In this step we’re going to submit a message to TWITTER service using MUrl. Now, type the following text into INTELLIPAD:
with [<status>Este lo voy a borrar desde MUrl….</status>]
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:
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:
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:
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]”
And now…, It works!! :-). You can check it down below at my TWITTER web page:
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:
So, there you have it!, you can use MUrl directly from INTELLIPAD.
Using “MUrl” from a .NET app.
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:
Take into account that I added two references to:
Then, I wrote the following .NET code to be executed when the button ‘Upload Message to Twitter’ is pressed:
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));
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:
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
But in this case, it is a bit more complicated, of course.
2.- Change to MGrammar-Mode–> Tree Preview
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):
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! 😉
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!