Consuming HTTP APIs in Ruby
What is your favourite technique for consuming HTTP APIs in Ruby? I like using HTTParty!
- It offers a simple, intuitive API.
- It makes it easy to support a whole bunch of standard API authentication mechanisms.
- It de-serialises responses based on content type headers.
- It allows us to write simple wrappers that are very close in form to the API we want to communicate with.
- It has a nice name! And we know how hard it is to name things.
I have come to employ a few patterns when working with HTTParty. They are all centered around having a convenient internal API to work with, and the ease of testing.
Most APIs I’ve worked with have one of the following authentication mechanism:
- HTTP Basic Authentication
- HTTP Digest Authentication
- Auth token in header
- API key in query params / request body
I am really fond of examples, so let’s consider an example. A RESTful service, where we interact with the “Article” resource. We’re able to list articles, get details about an article, create, update & delete an article. The service demands HTTP Basic auth, and JSON encoding.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
|
Here’s why I like this code:
- Simple, easy to read code that mimics the API quite nicely.
- Intuitive. Sending a POST request, is as simple as calling
post
. No need to remember multiple things. Specifying headers, is literally passing an argument calledheaders
. - Interacting with the API is now simple:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
Where’s the fun without ever changing requirements?
API, now V2, demands that we use digest auth, instead of basic auth.
1 2 3 4 5 6 7 8 9 10 |
|
That was a simple change. Let’s try adding custom headers. The API now supports logging, tracking and tagging requests. All done through headers. Since this is context specific, we’ll pass in the context as an argument to the constructor:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
In addition to basic auth, we can also send in an oauth style “Bearer” token:
1 2 3 4 5 6 |
|
Okay, this last example was lame. But the point remains, HTTParty allows you to build your own service objects, and gets out of your way. Exactly what a library should do.
If you really take a close look at our examples, consuming an HTTP API is all about:
- Setting Headers
- Specifying Query parameters
- Request body encoding
- Parsing response based on content type
- Making the HTTP requests
Not surprising, right? HTTParty makes it simple, and intuitive to perform all these actions. And hence it remains my favourite.
P.S. Here’s the code used above, in a Gist.