Working with Web Services > Searching the Catalog using Perl > Searching the Catalog using REST

Searching the Catalog using REST

The first REST subroutine in the SearchingSamples code is searchCatalogRest. This performs the searchCatalog operation (using the value assigned to the searchTerm variable) and returns the query ID and hits.

In lines 187-189, we make the request.

my $response = $ua->post($BASE_URL.'rest/standard/searchCatalog',
{clientID=>$clientID,
term1=>$searchTerm});

In these lines, we specify the endpoint for the searchCatalog request. The searchCatalog operation is part of the standard service. We also pass in some required data: the clientID and the first search term. The results of this call get returned to the response variable.

Also, notice that the parameter “term” has had a number appended to it. This is allowed (and expected) for parameters where more than one are allowed in a request. Check the details of each parameter in the operation to learn more about the cardinality. So, if you wanted to pass in two terms, the first parameter key would be “term1” and the second parameter key would be “term2.”

Next, we convert the result to something Perl can handle.

my $data = $xml->XMLin($response->content);

XMLin helps us to convert the XML data to an internal Perl representation. You can use the following call to see the internal representation of the XML data:

print Dumper($data);

The final part of this subroutine returns the queryID:

return ($data->{queryID});

The next subroutine called is searchCatalogPagingRest. The queryID is passed in and it returns titleID. This subroutine only displays hits 21 through 40 of the previously done query.

In lines 210-214, we make the request.

my $response = $ua->post($BASE_URL.'rest/standard/searchCatalogPaging',
{clientID=>$clientID,
queryID=>$queryID,
firstHitToDisplay=>'21',
lastHitToDisplay=>'40'});

This call uses the passed in queryID to identify the results list from the previous searchCatalog operation. The searchCatalogPaging operation allows you to specify beginning and ending ranges of results to display. This is useful for when you only want to display a limited number of results or when you want to allow the user to specify how many results to display.

After converting the XML to a Perl representation, on line 222 the application returns the titleID of the 21st result.

return $data->{HitlistTitleInfo}[0]->{titleID};

The XML result for the paged search returns a HitlistTitleInfo node for each result. Within that node, there is a titleID node. This line gets the first element of HitlistTitleInfo, which is titleID, and returns it as a scalar variable.

The last subroutine, lookupTitleInfoRest, is passed the titleID returned from the previous subroutine. This subroutine retrieves the item information for that titleID.

In lines 235-240 of the code, we make the request:

my $response = $ua->post($BASE_URL.'rest/standard/lookupTitleInfo',
{clientID=>$clientID,
titleID=>$titleID,
includeAvailabilityInfo=>'true',
includeItemInfo=>'true',
marcEntryFilter=>'NONE'});

The lookupTitleInfo operation is part of the standard service, so we use that for the endpoint. The passed-in titleID is used to identify the requested item. We also have specified that we want to include the availability information (availability would need to be turned on for this to work) and item information, but not MARC data.

The application then displays the returned information.

See the SearchingSamples application for the complete code to achieve this type of functionality.

 


© 2009-2012 SirsiDynix