#!/usr/bin/env perl

=head1 NAME

dapiclient_2

=head1 DESCRIPTION

This is an example perl 2-legged oauth client that makes a request to
the full Data API. Replace OAUTH_CONSUMER_SECRET and
OAUTH_CONSUMER_KEY (below) with the keys you received from
http://babel.hathitrust.org/cgi/kgs and MY_IP_ADDRESS by the IP
address of workstation or server running this script.

It uses a hardcoded URL to request a single page image for a public domain item.

It is intended to aid development of a fully function Data API client
in Perl or other languages that implement HMAC_SHA1 OAuth libraries.

=head1 SYNOPSIS

At the Unix command line:

  % cd /path/to/dapiclient_2/directory
  % chmod u+x dapiclient_2

then:

  % curl 'http://yourhost/path_to_client/dapiclient_2' > mdp.39015000000128.png

If your png is corrupt, run without i/o redirection to see the error code:

  % curl 'http://yourhost/path_to_client/dapiclient_2'

or paste the URL to your browser.

=head1 OUTPUT

Stream of data for the above mentioned page image file or error code.

=cut

use strict;
use warnings;

use CGI;
use OAuth::Lite::Consumer;
use OAuth::Lite::AuthMethod;

# read_file()
# Derived from:
#   https://stackoverflow.com/questions/953707/in-perl-how-can-i-read-an-entire-file-into-a-string
sub read_file
{
    my ($file) = @_;
 
    my $text = do {
	local $/ = undef;
	open my $fh, "<", $file
	    || die "could not open $file: $!";
	<$fh>;
    };

    chomp($text);
    return $text;
}

my $DEBUG=1;
my $WEB_DISPLAY=0;

my $access_key = 'OAUTH_CONSUMER_KEY';    # replace with your access_key
my $secret_key = 'OAUTH_CONSUMER_SECRET'; # replace with your secret_key
my $ip_address = 'MY_IP_ADDRESS';         # replace with the IP address of your workstation or server

if (-f "my_access_key.txt") {
    $access_key = read_file("my_access_key.txt");
}

if (-f "my_secret_key.txt") {
    $secret_key = read_file("my_secret_key.txt");
}

if (-f "my_ip_address.txt") {
    $ip_address = read_file("my_ip_address.txt");
}
    
my $base_request_url = "https://babel.hathitrust.org/cgi/htd/volume/meta/";


my $request_url;
if (defined $ARGV[0]) {
    $request_url = $base_request_url.$ARGV[0];
}
else {
    # The following ID is to the example given in the Data API documentation 
    # $request_url = $base_request_url."mdp.39015000000128/1";
    
    # The following is an example of a 'open-open' sheet music doc
    $request_url = $base_request_url."mdp.39015070661841/1";
}
    
my $consumer = OAuth::Lite::Consumer->new
  (
   consumer_key    => $access_key,
   consumer_secret => $secret_key,
   auth_method     => OAuth::Lite::AuthMethod::URL_QUERY,
  );

if ($DEBUG) {
    my $params = $consumer->gen_auth_params('GET', $request_url);
    print STDERR "(Info) Resolved URL for retrieval:\n";
    print STDERR "$request_url?v=2&";
    print STDERR "oauth_consumer_key=$params->{'oauth_consumer_key'}&";
    print STDERR "oauth_timestamp=$params->{'oauth_timestamp'}&";
    print STDERR "oauth_nonce=$params->{'oauth_nonce'}&";
    print STDERR "oauth_signature_method=$params->{'oauth_signature_method'}&";
    print STDERR "oauth_signature=$params->{'oauth_signature'}&";
    print STDERR "oauth_version=$params->{'oauth_version'}\n";
}

my $response = $consumer->request
  (
   method  => 'GET',
   url     => $request_url,
   params  => {
               v => '2',
               #cformat => 'png',
               ip => $ip_address,
              },
  );

my $success = $response->is_success;
my $content_type = 'image/png';

unless ($success) {
    #$content_type = 'text/html';
}

if ($WEB_DISPLAY) {
    print CGI::header(
                      Content_type => $content_type,
                      Status => $response->status_line,
                     );

    unless ($success) {
        print "<p><b>Error: " . $response->content . "</b><br/>";
    }
    else {
        print $response->content;
    }
}
else {
    print $response->content;
}

if ($success) {
    exit 0;
}
else {
    exit 1;
}




