Join 3,440 readers in helping fund MetaFilter (Hide)


Help me understand interacting with a RESTful API, please
August 8, 2011 2:47 PM   Subscribe

I'm tired of using other people's modules for interacting with websites' APIs. But I can't seem to quite wrap my head around using JSON with a website/cgi script I haven't designed. Are there any good tutorials that talk over how to build a JSON file to send to a RESTful API and then do what you want, preferably generalized so I'm not searching for a new one for each website?

The API I'm trying to work with at the moment is Tumblr, and I primarily program in python. I've dabbled with Java, Javascript, Ruby and C, but I really "get" python the best. There is a tumblr module for python, but it's broken. It throws an exception even when it works, and it looks to be all but abandoned on Google Code. If it matters for this, I prefer python 2.7, but I could probably figure out the translation with a step by step guide. For what I specifically want to do, I'd like to be able to upload link posts to a tumblr. I'm happy taking baby steps along the way, too.

So, since Tumblr has a well documented and fairly accessible looking API, how do I go about writing a module for uploading it that I'd actually understand? I don't want someone to just write it for me. I'd like a tutorial that explains how to put together a json file, send it, and so on.

I've managed to sort of put together my own sorts of AJAX interactions on experiments I've made, but they're pretty simplistic, and it's easier because I can see what my request output will be in the shell after I dump it to the json module and then form the response code around that. And if the guide is written for somebody really inexperienced, I'd actually prefer that. I feel like I get my bearings better if a guide kind of takes things back to the beginning.

For now, I'm going to be working mostly on my home computer, but I'd like to at least have a basic understanding of what to do with mostly builtin and small external modules rather than full on frameworks so that if I want to set up something on a somewhat restricted hosted server it can still work.

Yes, I know 9/10 times you can find a good library, and I could probably scrape by with the one I found, but I really feel like I'd rather build this tool to enhance my skills. Besides, I just know someday I'm going to find another website I want to work with that doesn't have a good API wrapper.
posted by mccarty.tim to Computers & Internet (7 answers total) 7 users marked this as a favorite
 
It sounds like you could just write a Python class that encapsulates key-value pairs (dictionary) that correspond to Tumblr API keys (and corresponding values).

Once you have an instance of this class, populate its keys with values and then use Python json routines to convert the Python object instance into a JSON representation, submitting it via AJAX or whatever.
posted by Blazecock Pileon at 3:34 PM on August 8, 2011


There are four python json libraries linked at json.org. I'd start there.
posted by axiom at 3:54 PM on August 8, 2011


Why don't you delve into the existing library and find out why it's not working? Along the way you'll learn how to interact with the API so at the end of the day you should be able to write your own.
posted by Rhomboid at 4:48 PM on August 8, 2011


The basic thing to know about RESTful APIs is that they're just HTTP traffic. Instead of your browser communicating with the remote server, though, you're writing a program to do it, and the data is in a closely-specified format.

Given that, you probably want to start with the Python urllib2 tutorial; it'll show you how to interact programmatically with a webserver. Then you can look at the json module documentation Blazecock Pileon linked to, and the "posting" section of the docs you linked to to tie it all together.

Rhomboid's suggestion is also a good one.
posted by asterix at 5:33 PM on August 8, 2011


This Perl module seems to have been updated in the last six months or so, that might be a good place to start.
posted by AmbroseChapel at 5:59 PM on August 8, 2011


Thanks, guys! Wrapping my head around what JSON exactly was and how it compares to the types in Python was what confused me. I don't really have the full program written yet, but I'm happy with the exercises I've run so far.
posted by mccarty.tim at 6:52 PM on August 8, 2011


For the record, I couldn't seem to authenticate with tumblr over oauth, so I switched to V1, which worked like a charm with pyCurl.
posted by mccarty.tim at 5:19 PM on August 9, 2011


« Older Where can I get a professional...   |  I agreed to take a teaching jo... Newer »
This thread is closed to new comments.