Tag Archives: api

NPM and the Node ecosystem

After one month of hacking around with Node.js, I created and distributed my first package. NPM is the repository of all kind packages that you can think of. At this moment, there are 28 698 packages in NPM which is a lot for a 4 years old project. The philosophy of Node and its community is to keep the core tiny and encourage composition with small modules that do one thing well.
In that sense, I wrote a small facebook API client that I called tiny-facebook-wrapper, as you can see in the figure. The package.json and .npmignore are two important files to have in any Node.js package. In a nutshell, the first one is the descriptor of your package with the author, version, dependencies, etc. The second one is used for keeping off things that you don’t want to share in your package, such as, docs, tests(?) or other superfluous files. Don’t forget to document your package in a README file with some hands-on examples of how developers can use your software, they will thank you from the bottom of their hearts.

Now that I have my module ready to share, I can create a dev account in the NPM registry directly from the command line:


$ npm adduser
$ Username: davidcunha
$ Password: ********
$ Email: davidgoncalvescunha@gmail.com

Publish the module:

$ npm publish

And that’s all folks, my small facebook API client is online for the community to use it:

Simple, right?

Advertisements
Tagged , , , ,

Faraday: your new best friend

I am a die-hard REST API fan, however the default Net::HTTP Ruby lib is very verbose and doesn’t give me all the features and flexibility I need for coding API clients fast and easy.
Faraday is a hell of a tool.. (disclaimer: the name is not related with any English scientist well known for contributions in the electromagnetism field). Faraday it’s a HTTP library, started by Rick Olson, which reuses Rack middleware concepts and provides a common interface for building API wrappers. Also it supports many out of the box advanced features, like OAuth, SSL, HTTP caching (Last-Modified and ETag headers), HTTP proxy, easy response data parsing, which could be cumbersome to code and configure with the popular Net::HTTP lib.

Here is a basic snippet:

require 'faraday'

Faraday.get(:url => 'http://faradayisfun.com')

But you can code more complex things and tweak configurations:

require 'faraday'

conn = Faraday.new(:url => 'http://faradayisfun.com') do |builder|
  builder.response :logger #logging stuff
  builder.use Faraday::Adapter::NetHttp #default adapter for Net::HTTP
  builder.use FaradayMiddleware::ParseJson #cool for parsing response bodies
end

res = conn.post do |request|
  request.url '/example'
  request.headers['Content-Type'] = 'application/json'
  request.body = "{send: body}"
end

res.body

Do you need HTTP basic auth? Faraday gives you some:

...
conn.basic_auth('user', 'pwd')
...

With Faraday you can consume APIs with some lines of code and customize its behavior like a pro. Checkout the github repo and take a look at all the cool extra options and supported features.

Tagged , , , , ,
Advertisements