Implementing RESTful API for obtaining the book details for an ISBN

SHARE & COMMENT :

Lot of developers out there at some point in time would want to know about an API to get the book details and I was in such a situation sometime back and I managed to find out about ISBNDb. But when I looked for the apis sometime later, I landed here where there are 53 APIs are listed. I am not sure how easy are others to use, but I found ISBNDb was just enough for my needs.

The base URL for the API is: https://isbndb.com/api/books.xml. It supports querying on lot of attributes like isbn, title, book_id, publisher_id (which are ISBNdb specific). In this example we are interested in using ISBN to query for the book details. But before proceeding further, you have to sign up to get access to the developer key. Once you have the developer key, to get the book details given the ISBN the URL would be: https://isbndb.com/api/books.xml?access_key=[KEY]&index1=isbn&value1=[ISBN]. The access_key query param would be assigned the key which you have obtained. index1, value1 are the params which are used to pass filter data for obtaining the book details.  We could even filter on book title wherein we add index2=title and value2=any_book_title.

also read:

Lets use this API to obtain the book details and this would be implemented in Groovy.

Our Model class for the holding a book details would be:

class Book {
  def isbn
  def title
  def authors
  def publisher
  def String toString(){
    "Title: ${title}\nISBN: ${isbn}\n"+
    "Authors: ${authors}\n"+
    "Publisher: ${publisher}"
  }
}

Building the URL for the API invocation:


def  baseURL = "https://isbndb.com/api/books.xml"
def  ACCESS_KEY = "YOUR_KEY"

def scanner = new Scanner(System.in);
println "Enter the ISBN"
def isbnInput = scanner.nextLine()
def queryParams = []
queryParams << "access_key=${ACCESS_KEY}"
queryParams << "index1=isbn"
queryParams << "value1=${isbnInput}"
def queryString = queryParams.join("&")
def completeApiURl = "${baseURL}?${queryString}"

The above code accepts the ISBN from the User and then prepares the required URL for invoking. One can see that we have made use of Java class in Groovy. The operator “<<” when used with an array appends the values to the array. And the join(“&”) would concatenate all the elements of the array using the “&” character. Once we have the complete URL, we make use of the java.net.URL class to obtain the XML Response and then use XmlSlurper (remember JsonSlurper?) to parse the XML to get the required information:

def apiResponse = new URL(completeApiURl).text
def xmlSlurper = new XmlSlurper()
def parsedXml = xmlSlurper.parseText(apiResponse)

def newBook =
  new Book(title:parsedXml.BookList.BookData.Title,
           isbn: parsedXml.BookList.BookData.@isbn,
           authors: parsedXml.BookList.BookData.AuthorsText,
           publisher: parsedXml.BookList.BookData.PublisherText)

println newBook

The complete code is:

def  baseURL = "https://isbndb.com/api/books.xml"
def  ACCESS_KEY = "MKKGCUMQ"

def scanner = new Scanner(System.in);
println "Enter the ISBN"
def isbnInput = scanner.nextLine()
def queryParams = []
queryParams << "access_key=${ACCESS_KEY}"
queryParams << "index1=isbn"
queryParams << "value1=${isbnInput}"
def queryString = queryParams.join("&")
def completeApiURl = "${baseURL}?${queryString}"
def apiResponse = new URL(completeApiURl).text
def xmlSlurper = new XmlSlurper()
def parsedXml = xmlSlurper.parseText(apiResponse)

def newBook = new Book(title:parsedXml.BookList.BookData.Title,
isbn: parsedXml.BookList.BookData.@isbn,
authors: parsedXml.BookList.BookData.AuthorsText,
publisher: parsedXml.BookList.BookData.PublisherText)
println newBook

class Book {
  def isbn
  def title
  def authors
  def publisher

  def String toString(){
    "Title: ${title}\n"+
    "ISBN: ${isbn}\n"+
    "Authors: ${authors}\n"+
    "Publisher: ${publisher}"
  }
}

The execution and output of the above code:

$ groovy BookManager.groovy
Enter the ISBN
098478280X
Title: Cracking the Coding Interview
ISBN: 098478280X
Authors: Gayle Laakmann McDowell,
Publisher: CareerCup

Comments

comments

About Mohamed Sanaulla

In his day job he works on developing enterprise applications using ADF. He is also the moderator of JavaRanch forums and an avid blogger.

Speak Your Mind

*

Close
Please support the site
By clicking any of these buttons you help our site to get better