Cookie Notice

As far as I know, and as far as I remember, nothing in this page does anything with Cookies.

2013/04/05

Module to automate API form

One thing I've noticed about the web APIs I'm seeing online is that you can often determine what format you want it in. For example, the API URL is something like example.com/api/v1/feed which gives you RSS, but if you want it in ATOM, you can get it at example.com/api/v1/feed/atom. Want JSON? example.com/api/v1/feed/json. This way, your Javascript can AJAX that mess up, while your users can pull it into EXCEL with example.com/api/v1/feed/csv and be happy.

This is cool. So I wrote some Perl to automate that stuff for me.


This does not do JSONP, because I'm not expecting, right now, to want to cross-site script the stuff I'm exporting right now. But maybe. And it shouldn't be too bad of a modification. And this is a naive and potentially stupid way of doing CSV. Maybe wrapping everything in quotes?
The cool step would be to make a dispatch table and function refs to the right thing, pulling the magic for each format out. This might be done later.

6 comments:

  1. This concept is called "content negotiation". Many modules exist for it, and most web frameworks can do it automagically. See Mojolicious for example. As usual its fun to roll your own to learn, but better not to reinvent the wheel in practice.

    ReplyDelete
  2. Fair enough. You'll understand that some would think about spending time looking through CPAN for modules covering a concept he doesn't have terminology for, then begging the admin to install the modules on the production server, and think "Frak it, I'll code that thing myself."

    I've started trying to move our way-old-school web setup to a framework. No, I tried to get to the point of understanding web frameworks to get to the point where I'd rather go to them than start with '#!/usr/bin/perl' and 'use CGI;'. I'll never try Catalyst again, but I got to the point where I can do something with Dancer, should we adjust our infrastructure to the point where MVCs make sense.

    (No bias against Mojolicious yet, but certainly bias against learning things over coding things.)

    ReplyDelete
  3. I hope you didn't take my note as aggressive, I can see how it might come off that way. I really was just trying to clue you in on the name of the concept.

    I have no experience with Dancer, so I won't comment, but I'm a big proponent of Mojolicious. Note that it can run under a CGI environment for a smooth transition for you. Also check out Mojolicious::Lite for a DSL that will seem similar to Dancer (or other small script webapps).

    As always, have fun!

    ReplyDelete
  4. I didn't. Not really. But the leap from "try this" to something usable is big enough that I'd rather not, most of the time.

    ReplyDelete
  5. For CSV, I'd suggest using Text::CSV_XS because it handles the edge cases and corner cases well (and it's a very battle tested piece of code.)

    https://metacpan.org/module/Text::CSV_XS

    ReplyDelete
  6. I had reason to do CSV recently, I forget the reason, and found getting it working painful. If you know your content has no commas to escape (and in this case, I did), that's simple enough, but yeah.

    ReplyDelete