The Open Flickr API allows you to consolidate elements into Project Zero applications
Flickr photo Sharing Service is one of the most popular WEB applications today. It provides robust hosting services with excellent social networks, making uploading, organizing, and looking for photos incredibly simple. It's all great, but from a developer's point of view, Flickr's most fascinating part is its public API for reading and writing photo data. You can send API requests through HTTP in any programming language, and now there are many open source projects that can encapsulate this API for a variety of languages. In this article, you will learn how to "Zero" the Flickr API by providing Groovy bindings that you can easily reuse in Project Zero applications. After you've learned this article, you can read and write photos from Groovy scripts in just a few lines of code.
Before you start
This article assumes that you have downloaded Project Zero and have either completed an introductory tutorial or written your own simple application. This overview includes instructions on how to obtain the Flickr API keys, which are required to test the code provided in this article.
Brief introduction
Flickr photo Sharing Service is one of the most popular WEB applications today, and it includes a public API that allows developers to access photo data using HTTP requests. The Flickr API is RESTful and easy to understand, but some tedious steps are required to request and read the response. This article shows you how to get rid of these tedious steps and make communication between Zero and Flickr simple to just a few lines of code. This article also explores several interesting extension points for Project Zero.
Using Groovy to encapsulate the Flickr API
The Flickr API for reading and writing photo data is based on messages sent in HTTP and data structures in XML or JSON format. This article uses JSON because its data structures (simple mappings and lists) are easier to handle from Groovy's perspective. In this section, you will learn how to use the global context of Zero and Groovy scripts to create some simple wrapper methods for the Flickr API.
To create a sample project
To complete our Flickr API "zero" processing, we need to create two zero projects: A groovy code for interacting with Flickr, one that uses these groovy code to accomplish certain tasks. The first project is named Zero.services.flickr, and the second project is named Flickr.test. If you want to write your code yourself, you can use the Zero command line shown in Listing 1 to create these two simple projects:
Listing 1. To create a test project
$ zero create zero.services.flickr
$ zero create flickr.test
Edit the Ivy file for Flickr.test and add a dependency on the Zero.services.flickr. Open/config/ivy.xml and add the line of XML code as shown in Listing 2:
Listing 2. Add Ivy Dependencies
<dependency name="zero.services.flickr" org="zero" rev="1.0+"/>
The ZERO.SERVICES.FLICKR project does not require additional libraries or dependencies because the Flickr API is built on HTTP and JSON, and we will use the HTTP and JSON libraries contained in Zero Core to communicate with Flickr and create the actual graph Like the URL of a file.
Call Flickr API
Each Flickr request requires a method name and a set of parameter sets (the parameter set may also be empty). The response is returned as a JSON object, and the JSON object has state attributes (set to OK or fail) in addition to the requested data. You can take advantage of the fact that JSON objects are represented as simple mappings and lists in Groovy and create a very simple API to send requests and read responses. Let's start with a use case: You might want your users to be able to make FLICKR requests in three or four lines of code, as shown in Listing 3:
Listing 3. "Zero" Flickr API prototype #1
def method = "flickr.photosets.getPhotos";
def params = [
photoset_id: 1234567890,
per_page: 25
];
def response = invokeMethod("flickr.groovy", "flickr");