Skip to content

Web Services Perl Client

Shahim Essaid edited this page Feb 2, 2015 · 1 revision

writing clients to access OWLTools web server

Example generic client:

#!/usr/bin/perl -w

use strict;
use LWP::Simple;                # From CPAN
use JSON qw( decode_json );     # From CPAN
use Data::Dumper;               # Perl core module

my $svr = "http://localhost:9001/";

my @params = ();
my @cmds = ();
while (@ARGV) {
    my $opt = shift @ARGV;
    if ($opt =~ /^\-/) {
        if ($opt eq '-h' || $opt eq '--help') {
            print usage();
            exit 0;
        }
        elsif ($opt eq '-s') {
            $svr = shift @ARGV;
        }
        else {
            $opt =~ s/^\-//;
            push(@params, $opt."=".(shift @ARGV));
        }
    }
    else {
        push(@cmds, $opt);
    }
}
my $cmd = shift @cmds;
die "no command!" unless $cmd;
die ">1 command!" if @cmds;

my $url = $svr . $cmd . ".json" . "?".join("&",@params);
print STDERR "$url\n";
my $json = get( $url );
die "Could not get $url!" unless defined $json;

# Decode the entire JSON
my $decoded_json = decode_json( $json );

# you'll get this (it'll print out); comment this when done.
print Dumper $decoded_json;

sub scriptname {
    my @p = split(/\//,$0);
    pop @p;
}


sub usage {
    my $sn = scriptname();
$sn [BASEURL](-s) COMMAND [VAL...](-PARAM)

Example:
powl-test.pl allSubClassOf -direct false
    <<EOM;
$sn 



EOM
}