Home‎ > ‎API‎ > ‎

x2dbi-soap-perl


#!/usr/bin/perl
# (C) CBiB / INRA - DJ - 2010
# Updated by DJ in 2011
# SOAP module to use the X2DBI services
# This script is distributed under the GNU general public license (http://www.gnu.org/copyleft/gpl.html)

use SOAP::Lite;
use MIME::Base64;

use strict;

# Usage: perl $0.pl <service> <URL_OF_WSDL> <parameters>
# service : browse, getqueries, query or login


die "No service parameter" unless scalar(@ARGV);
my $switchkey = shift @ARGV;
die "WSDL is not specified" unless scalar(@ARGV);
my $wsdl = shift @ARGV;

my $SOAP = SOAP::Lite->service($wsdl);
   $SOAP->soapversion('1.1');

my ($xmlfile,$xml, $method, $result, $auth);
my @params;

SWITCH: {
    if ($switchkey =~ m/^(browse|getqueries)/i) {
        $method = $switchkey eq 'browse' ? 'Browse' : 'GetQueries';
        @params = ( @ARGV );
        last SWITCH;
    }
    if ($switchkey =~ m/^(login|query)/i) {

        # XML query file (if Query)
        if ($switchkey eq 'query') {
           die "XML is not specified" unless scalar(@ARGV);
           $xmlfile=shift @ARGV;
           open (FH,$xmlfile) || die "Unable to open XML file $xmlfile";
           while (<FH>){ $xml .= $_; }
           close(FH);
        }

        # Authentification
        $auth = scalar(@ARGV) ? shift @ARGV : 0 ;

        # Login
        if ($switchkey eq 'login') {
           $method = 'Authentification';
           @params = ( @ARGV, $auth );
           last SWITCH;
        }

        # Type of result
        my $type = scalar(@ARGV) ? shift @ARGV : 0 ;

        # Query
        $method = 'Xquery';
        @params = ( encode_base64($xml),$auth,$type );
        last SWITCH;
    }
}

eval { ($result) = $SOAP->$method (@params) };
if ($@) { die "Service execution failed: $@"}
print $result;

exit(0);