[Bio] / FigKernelPackages / SAPserver.pm Repository:
ViewVC logotype

View of /FigKernelPackages/SAPserver.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (download) (as text) (annotate)
Tue Jun 30 20:11:54 2009 UTC (10 years, 5 months ago) by parrello
Branch: MAIN
CVS Tags: rast_rel_2009_07_09
Changes since 1.2: +1 -0 lines
Additional functionality for sapling server.

#!/usr/bin/perl -w

#
# Copyright (c) 2003-2006 University of Chicago and Fellowship
# for Interpretations of Genomes. All Rights Reserved.
#
# This file is part of the SEED Toolkit.
#
# The SEED Toolkit is free software. You can redistribute
# it and/or modify it under the terms of the SEED Toolkit
# Public License.
#
# You should have received a copy of the SEED Toolkit Public License
# along with this program; if not write to the University of Chicago
# at info@ci.uchicago.edu or the Fellowship for Interpretation of
# Genomes at veronika@thefig.info or download a copy from
# http://www.theseed.org/LICENSE.TXT.
#

package SAPserver;

    use strict;
    use LWP::UserAgent;
    use YAML;

=head1 Sapling Server Helper Object

=head2 Introduction

This module is used to call the sapling server, which is a general-purpose
server for extracting data from the Sapling database. Each Sapling server
function correspond to a method of this object.

This package deliberately uses no internal SEED packages or scripts, only common
PERL modules.

The fields in this object are as follows.

=over 4

=item server_url

The URL used to request data from the sapling server.

=item ua

The user agent for communication with the server.

=back

=cut

=head3 new

    my $ss = SAPserver->new(%options);

Construct a new SAPserver object. The following options are supported.

=over 4

=item url

URL for the sapling server. This option may be used to redirect requests to a
test version of the server, or to an older server script.

=back

=cut

sub new {
    # Get the parameters.
    my ($class, %options) = @_;
    # Get the options.
    my $url = $options{url} || "http://servers.nmpdr.org/sap/server.cgi";
    # Create the fields of the object.
    my $server_url = $url;
    my $ua = LWP::UserAgent->new();
    # Create the SAPserver object.
    my $retVal = { 
                    server_url => $server_url,
                    ua => $ua,
                 };
    # Bless and return it.
    bless $retVal, $class;
    return $retVal;
}

=head2 Public Methods

=head3 AUTOLOAD

    my $result = $ss->method(%args);

Call a function on the server. Any method call on this object (other than
the constructor) is translated into a request against the server. This
enables us to add new server functions without requiring an update to this
module. The parameters are specified as a hash, and the result is a scalar
or object reference. If an error occurred, we will throw an exception.

=cut

# This variable will contain the method name.
our $AUTOLOAD;

sub AUTOLOAD {
    # Get the parameters.
    my ($self, %args) = @_;
    # Declare the return variable.
    my $retVal;
    # Get the method name.
    my $function = $AUTOLOAD;
    # Strip off the stuff before the method name.
    $function =~ s/.+:://;
    # Compute the argument document.
    my $argString = YAML::Dump(\%args);
    # Get our user agent.
    my $ua = $self->{ua};
    # Request the function from the server.
    my $response = $ua->post($self->{server_url},
                             [function => $function, args => $argString]);
    # Get the response content.
    my $content = $response->content;
    if (! $response->is_success) {
        die "Server error " . $response->status_line . "\n$content";
    } else {
        $retVal = YAML::Load($content);
        # Figure out what we got back.
        my $returnType = ref $retVal;
        if ($returnType && $returnType eq 'ErrorDocument') {
            die $retVal->{message};
        }
    }
    # Return the result.
    return $retVal;
}

=head3 DESTROY

    $ss->DESTROY();

This method has no function. It's purpose is to keep the destructor from
being caught by the autoload processing.

=cut

sub DESTROY { }


1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3