RESTful Web Services
Hot off the presses RESTful Web Services by
Leonard Richardson and Sam Ruby has just been delivered to me, I can’t wait to get stuck in.
I’ll post a review as soon as I can.
Hot off the presses RESTful Web Services by
Leonard Richardson and Sam Ruby has just been delivered to me, I can’t wait to get stuck in.
I’ll post a review as soon as I can.
Yes! Regular Expressions are simple once you learn the grammar, and thats also the biggest problem with them, unless you learn the grammar, Regular Expressions look like the gibberish of some dark art, And unless you actually sit down and study you’ll not make much progress with them.
There are some good books on Regular Expressions.
Regular Expression Pocket Reference
Mastering Regular Expressions
Once you have Regular Expressions in your tool box you’ll quickly see many uses they can be put to such as page scraping or data validation, I’ve even seen them used them for updating Delphi code bases to the latest version of Delphi.
In the train timetable service I used 3 Regular expressions to extract the information need to output optimized version of the time table.
The first two
/<input type=”hidden” name=”DepTime” value=”[0-9][0-9]:[0-9][0-9]
/<nput type=”hidden” name=”ArrTime” value=”[0-9][0-9]:[0-9][0-9]
Are used to strip out the table elements which contain the departure and arrival times,
The strings which match the patterns are stored in two arrays, one for arrival and one of departures.
Then iterating through the two arrays a third regular expression is used
[0-9][0-9]:[0-9][0-9]
This Regular Expression returns the times from the strings contained in the two arrays and it is this information which is used to produce the timetables you see when using the service.
I’d be interested to hear an if there is an even easier way to do this.
There is an excellent tool available for working with Regular Expression, Regex Buddy It is a fantastic piece of software.
Most languages and platforms support Regular Expressions, For Delphi you can use the TRegex component which is free, for Delphi .NET it’s not needed as .NET supports Regular Expressions.
One of the tools I used to construct the train time table service was mod_rewrite.
mod_rewrite can change URLs on the fly based on any rewriting rules you want to apply.
so instead of
../trains/Trains.php?from=Cork&to=Cobh
we now have
../trains/Cork/Cobh
Much nicer isn’t it.
Rewriting allowed me to create a URL structure which hides the implementation details, is easy to remember and can be constructed easily.
The URL is also more RESTful as a particular URL refers to a particular resource, todays time table.
An added bonus of hiding the implementation details makes it more secure.
Using mod_rewrite is straight forward.
First of all, the mod_rewrite is usually installed by default with Apache so you shouldn’t to worry about that.
Next, if like me you don’t have control over the server then you’ll have to use a .htaccess file which can be placed in the folder you do have access to. In this case I placed the .htacces file in the ../trains folder.
An advantage of placing the .htaccess file in a sub-folder is that it won’t have an effect on your root folder or any other of its subfolders, The .htaccess file will only effect the folder it is placed in.
Next we need to add some stuff to .htaccess file
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/?$ /trains/trains.php?from=$1&to=$2 [L,NC]
RewriteRule ^/?$ /trains/trains.php [L,NC]
The first line activates the rewrite engine.
The next lines are where the magic happens,
it is in the format Command Pattern Substitution Flags
Command : Simply “RewriteRule”
Pattern : This is the Regular Expression to matched in the URL, If a match is found then the rule is applied.
Substitution : This is the replacement string that the will replace the part of the URL matching the pattern if any.
Flags : You can set flags affect the behavior of mod_rewrite, in this case L and NC. L means last rule, if a match is found do not apply any further URL rewrites. NC means Not Case Sensitive.
Now just save the changes and we’re done.
The rewrite rule RewriteRule ^([^/]*)/([^/]*)/?$ /trains/trains.php?from=$1&to=$2 [L,NC] will rewrite URLs of the format ../trains/Depart/Destination/ to ../trains/Trains.php?from=Depart&to=Destination
The second rewrite rule RewriteRule ^/?$ /trains/trains.php [L,NC] will rewrite URLs of the format ../trains/ to ../trains/Trains.php as long as the first rule wasn’t triggered by a matching pattern being found.
The rewriting of URLs is completely invisible to the user and they will only the simplified URL.
To get the must out of mod_rewrite you’ll need to understand regular expressions, just one more reason why developers should add regular expressions to their toolbox.
There are a number of mod_rewrite clones available for IIS so URL rewriting is not just for Apache, It’s for life.
When creating the train time table service I decided to use a RESTful approach for the API, This meant the URL should confirm to a number of RESTful Principles.
1) The URL represents a resource and consist of NOUNs, not VERBs.
If you using Verbs then you are not using REST. you’re using RPC.
One of the defining principles of REST is the use of a limited set of Verbs POST,GET,PUT and DELETE which correspond to Create, Read, Update and Delete (CRUD).
This allows the easy contruction of URLS so that a consumer and go directly to the resource required.
for example
http://www.eoinprout.com/trains/Cork/Cobh/
will return the Cork Cobh time table resource.
2) The URL hides implementation details.
There are many advantages to hiding the implementation details not the least of which is that it allows the background implementation to be changed without affecting the API.
In my case the service is implemented in PHP but by using mod_rewrite it was possible to hide the implementation details of the service, One more advantage of this is that many search engines do not properly index dynamic pages which require the correct parameters to be set. This aids the discovery of the service.
3) The service should expose other parts of the service via links allowing discovery and traversal.
The trick here is not expose everything in one URL. In the case of my service the user starts at ../trains/ which presents a list of departure stations and links to a list of possible destinations, from there another link will return the URL for the particular time table required. so all the users needs to remeber is the ../trains/ URL.
4) GETs are used for obtaining a copy of the resource.
GET is the standard HTTP method getting content when you type a URL into the address bar of the browser it is a GET which is executed , On IrishRail.Com POSTs are used to retrieve a copy of the time table a user may be looking for.
There are serious disadvantages when using POSTs to retrieve a resource, The resource cannot be bookmarked, The URL for the resource cannot be sent to someone else, URL cannot be constructed and search engines cannot index the resource.
REST has been gaining more and more traction lately but there are still a lot misconceptions about it, So that we’re all on the same page I’ve included a list of the articles which I think explain REST.
“How I explained REST to my wife” : An excellent discussion on what makes REST REST.
Common REST mistakes : Sometimes it helps to understand something by understanding what is not.
The RESTful Web at XML.COM : Good articles about REST on O’Reilly.
The Beauty of REST : A nice article on an actual REST implementation by Jon Udell, who is now working for Microsoft as an evangalist. Jon has written a number of articles on the benefits of REST
In one of my previous posts I moaned about the high cost of the mobile internet in Ireland, The site I used as an example was Irishrail.com, I use this site a lot, checking the Cork/Cobh timetables. I wanted to be able to check the train times on my mobile but I wouldn’t use the Irish Rail website when it costs so much to do so. I decided to create a REST style web service optimized for the mobile internet, i.e. use as little bandwidth as possible.
It’s also be a chance to try out some ideas related to REST.
IMHO REST is the way to construct web services.
The Service is available here and you get your timetable by traversing the links.
OR
You can go directly to the timetable you want by constructing the URL based in the Format
http://www.eoinprout.com/trains/DeparturePoint/Destination
For Example
http://www.eoinprout.com/Cobh/Cork/
http://www.eoinprout.com/Cork/Cobh/
The service will show the timetable for trains between your destination and point of departure for the day, bookmarking the timetable will allow you to quickly check “Todays” timetable.
The service returns “Todays” timetables, If I’m planning journeys far in advance I’ll use my PC, The phone is for when I need to check when the next train home is :). If anyone really wants it changed to support dates in the future just ask.
Of course the first question you ask is, “How much bandwidth does it save ?”
It saves a lot.
| Irishrail.com | eoinprout.com/trains/ | |
| Traversing Links | 167kb | 9kb |
| Constructing Link | Not possible | 1kb |
It’s not possible to construct the URL for a particular timetable on Irishrail.com because it’s using a POST to send parameters rather then a GET, Interestingly this means that the timetable cannot be bookmarked to allow quick access.
The service was created using PHP and mod_rewrite, which I’ll talk more about in the future as I’m going to be using this service to illustrate REST.