#!/usr/bin/perl

#################################################################
#                                                               #
#    nemine_client.pl: REST client for term neminealization         #
#                                                               #
#    Disclaimer: This sample code is distributed under LGPL     #
#                 (http://www.gnu.org/licenses/lgpl.html)       #
#                                                               #
#     Function: Recognize gene/protein names in text            #
#                                                               #
#################################################################

$usage = "
      Usage: bootstrep-nemine.pl FILENAME [id]
";

use CGI;
use URI::Escape;
use HTTP::Request::Common qw(POST);
use LWP::UserAgent;
use LWP::Simple;

if ( @ARGV == 0 ) { print $usage; exit(0); }

$filename =  $ARGV[0];
$id  =  $ARGV[1];

$word = `cat $filename`;


#$METHOD = "GET";   # select one, GET or POST
$METHOD = "POST"; # 

$SITE = "http://www.nactem.ac.uk/nemine/nemine.cgi";

$result = &call_bootstrep_webservice( $METHOD, $SITE, $word, "", $id );
print $result;

exit(0);

# --------------------------------------------------------------
sub call_bootstrep_webservice {

    my $method = $_[0];
    my $site = $_[1];
    my $Input = $_[2];
    my $Argument = $_[3];
    my $SubscriptionID = $_[4];

    my $request;
    my $result;

## Escape special symbols

    my $esc = uri_escape( $Input );

## Define a URI
    my $uri = "$site?Input=$esc&Argument=$Argument&SubscriptionID=$SubscriptionID";

#    print "[$uri]\n";

## Prepartion
    my $ua = LWP::UserAgent->new;

    if ( $method eq "GET" ) {
       $request = HTTP::Request->new( GET => $uri );
    } else {
       $request = POST( $site, [ Input=> $Input, Argument=>$Argument, 
                                 SubscriptionID=>$SubscriptionID ] );
    }

## Call the URI

    $result = $ua->request( $request );

## Return the reuslt;

    if ( $result->is_error) {
	return "*** Webservice Failed ***\n";
    } else  {
       return $result->content; 
    }

} # end of call_bootstrep_webservice

# --------------------------------------------------------------
### end of program
