Data driven HTTP interactionby revdiablo (Prior)
|on Jul 22, 2007 at 08:32 UTC||Need Help??|
Hello good monks,
I've recently come up with something I haven't seen before. Granted, this hardly means it hasn't been done before; I just haven't run across it previously. If there's prior art, I'd be interested by all means!
We all know about LWP::UserAgent and WWW::Mechanize -- and I've certainly gained much purchase from using only those modules for a long time -- but there's a certain class of problems that I end up solving repeatedly, in a very similar way. That is: making HTTP requests, reacting to the responses, possibly making more requests, responding to those responses, ad infinitum. I've never liked using imperative Perl code to write this kind of bread and butter request/response/request/response chain.
It struck me that this is a straightforward recursive problem. It can be solved easily with a data structure to describe the requests, their responses, further requests for those responses, and so on. This way the actual HTTP processing (i.e., the use of LWP::UserAgent) is isolated to a simple recursive routine (with good, consistent error handling!), but the definition of the expected behavior is somewhere else.
Now, in case anyone is curious, I'll include an incomplete, and probably broken example. This is what I've been using to develop the idea. This is supposed to be a last.fm track submission client. Right now it does authentication and submits a single bogus track.
Here's the definition:
And here's the handler routine
I wasn't entirely sure if I should post this code, because this Meditation is really intended to be about the idea. I can think of all kinds of ways to improve the code (in terms of readability, conciseness, completeness, correctness), but I posted it to help explain the idea, in case my textual description is lacking. I'd really like to know, (1) if there is already a well-known -- or even not-well-known -- implementation of this kind of thing out there somewhere, and (2) if this is interesting enough to anybody else that I should spend more time working on it.
Thanks for any response!