Teapot: Pharo web REST framework, it ain’t micro

August 28, 2014

Diving mostly into Java these days, it was just sheer bliss to attempt to cook up a sample app with a new framework in Pharo: Teapot. Extend it do a flexible simple app workout.

http://smalltalkhub.com/#!/~zeroflag/Teapot

It is really elegant, what REST does and what Pharo can do with REST, that Teapot amplifies. Really elegant in its code too, the framework brings forth all that is cool about dynamic programming in Smalltalk

The link above  leads one through a 5 min intro / dive into a working set of examples of the framework. I delved for a couple of hours to create a more dynamic example classes that leads me through:

The MCZ:  Teapot-Examples

* Just subclass TeapotRequestHandler and you have now a GET/ PUT/DELETE default for the new class with its appName as the URL path viz:

      http://localhost:8080/NewSubClassAppName   

     ( The ‘NewSubClassAppName’ is default if it is the sub class name given..else assign a name to #appName in the initialize method )

* Override the #forwardTokenA: request method and you are quickly off the block to custom respond to the request. There on, you can follow the example in TeapotLibrary to forward additional tokens of the url to make it return data as reqd.

* The beauty is that in Pharo, now you can change anything, the appName / the response data / any or all of  the URL paths et al in the methods of the new subclass.

* The tests from the browser or using ZnEasy ( as in #sampleGetRequest: ) is instantaneous. None of the edit, compile, restart webserver stuff here..

* This also provides flexibility in scaling dynamically to as many landing URL paths as needed with just a simple discovery of subclasses runtime

From the drudgery of eclipse and its stack to Pharo and its frameworks, Zn / Teapot et als, its sheer bliss.

To try out the examples:

* Load the mcz  and  in a workspace execute:

Teapot stopAll .
teapot := Teapot configure: {
                                     #defaultOutput -> #json. #port -> 8080. #debugMode -> true }.
teapotEx2 := TeapotRequestHandler new initialize.
teapotEx2 runApp: teapot.

*  For quick test run the samples in TeapotLibrary and TeapotSync >> #sampleGetRequest:   highlight and execute the comment lines

*  The inspector that pops up, shows the various test urls configured, and its 200 success code  return object. The urls can be tried out in the web browser  viz:

http://localhost:8080/Library/books/1  http://localhost:8080/Library/search/author/skr  ; http://localhost/SyndLoans/FAC/12HJ3456 etc..

 

The cool dark theme in Pharo is nice on the eye with its contrast too: bit of Indian Flag look with text in white, orange and green along with the soft grey-blue.. nice on the eye.. yellow highlight on the edge.. !

Teapot-Examples01

PS: The Debugger though is cause for concern, it does not align at all with the line highlighted, in Pharo3.0

Advertisements