Wednesday, January 14, 2015

Building the RESTful API

Now that the database and sample data in place, one needs to create a way (API) to get data in and out of the database.

A common mechanism is to create a RESTful API.
Representational State Transfer (REST) has gained widespread acceptance across the Web as a simpler alternative to SOAP and WSDL based Web services. In layman's terms, REST is an architecture style or design pattern used as a set of guidelines for creating web services which allow anything connected to a network (web servers, private intranets, smartphones, fitness bands, banking systems, traffic cameras, televisions etc.) to communicate with one another via a shared common communications protocol known as Hypertext Transfer Protocol (HTTP). 
Representational state transfer- Wikipedia, the free encyclopedia

One of the features of Firebase is that it provides the RESTful API automatically; i.e., no coding or configuring required.  While Firebase provides a complete guide on the API <https://www.firebase.com/docs/rest/guide/>, the examples below will provide the basic concepts.

note: Interestingly enough, in the responsive web design app that we will build we will not be using the RESTful API, but rather the more powerful Web - JavaScript API. The reason that we take a detour to learn using a RESTful API is that there are many other services that one might want to tap into that use a RESTful API, e.g., Google APIs <https://developers.google.com/apis-explorer/>.

Now that one has an idea of what a RESTful API is, one has to have tools to test them.  One particularly easy to use tool is a Google Chrome extension called "Postman - REST Client".  Install Google Chrome and get it.

To get one started here are five things to try in "Postman - REST Client", replacing "wineapp" with the name of your Firebase app. This will exercise the four main data operations in "CRUD", i.e, Create, Read, Update, and Delete.

  • List the (Collection) Wineries:
     
    note: One oddity of the Firebase RESTful API is that is presents lists as an object (key, value) instead of an array of objects as many other RESTful APIs provide.  Another oddity is that one must add the ".json" suffix to the URLs.

  • Retrieve a (Element) Wine:

  • Create a (Element) Winery:


    note: The response will contain the auto-generated key (they list it as the name) of the newly created object.

  • Delete a (Element) Winery:

    note: The ugly string after wineries in the URL is the auto-generated key.

  • Replace a (Element) Wine:


No comments:

Post a Comment