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

Annotation of /FigKernelPackages/SAPserver.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.7 - (view) (download) (as text)

1 : parrello 1.1 #!/usr/bin/perl -w
2 : parrello 1.7 #
3 :     # This is a SAS Component.
4 : parrello 1.1 #
5 :     # Copyright (c) 2003-2006 University of Chicago and Fellowship
6 :     # for Interpretations of Genomes. All Rights Reserved.
7 :     #
8 :     # This file is part of the SEED Toolkit.
9 :     #
10 :     # The SEED Toolkit is free software. You can redistribute
11 :     # it and/or modify it under the terms of the SEED Toolkit
12 :     # Public License.
13 :     #
14 :     # You should have received a copy of the SEED Toolkit Public License
15 :     # along with this program; if not write to the University of Chicago
16 :     # at info@ci.uchicago.edu or the Fellowship for Interpretation of
17 :     # Genomes at veronika@thefig.info or download a copy from
18 :     # http://www.theseed.org/LICENSE.TXT.
19 :     #
20 :    
21 :     package SAPserver;
22 :    
23 :     use strict;
24 :     use LWP::UserAgent;
25 :     use YAML;
26 :    
27 :     =head1 Sapling Server Helper Object
28 :    
29 : parrello 1.5 =head2 Description
30 : parrello 1.1
31 :     This module is used to call the sapling server, which is a general-purpose
32 :     server for extracting data from the Sapling database. Each Sapling server
33 :     function correspond to a method of this object.
34 :    
35 : parrello 1.2 This package deliberately uses no internal SEED packages or scripts, only common
36 :     PERL modules.
37 :    
38 : parrello 1.1 The fields in this object are as follows.
39 :    
40 :     =over 4
41 :    
42 :     =item server_url
43 :    
44 :     The URL used to request data from the sapling server.
45 :    
46 :     =item ua
47 :    
48 :     The user agent for communication with the server.
49 :    
50 : parrello 1.5 =item singleton
51 :    
52 :     Indicates whether or not results are to be returned in singleton mode. In
53 :     singleton mode, if the return document is a hash reference with only one
54 :     entry, the entry value is returned rather than the hash.
55 :    
56 : parrello 1.1 =back
57 :    
58 :     =cut
59 :    
60 :     =head3 new
61 :    
62 :     my $ss = SAPserver->new(%options);
63 :    
64 :     Construct a new SAPserver object. The following options are supported.
65 :    
66 :     =over 4
67 :    
68 :     =item url
69 :    
70 :     URL for the sapling server. This option may be used to redirect requests to a
71 :     test version of the server, or to an older server script.
72 :    
73 : parrello 1.5 =item singleton
74 :    
75 :     If TRUE, results from methods will be returned in singleton mode. In singleton
76 :     mode, if a single result comes back, it will come back as a scalar rather than
77 :     as a hash value accessible via an incoming ID.
78 :    
79 : parrello 1.1 =back
80 :    
81 :     =cut
82 :    
83 :     sub new {
84 :     # Get the parameters.
85 :     my ($class, %options) = @_;
86 :     # Get the options.
87 : parrello 1.6 my $url = $options{url} || "http://servers.nmpdr.org/sapling/server.cgi";
88 : parrello 1.5 my $singleton = $options{singleton} || 0;
89 : parrello 1.1 # Create the fields of the object.
90 :     my $server_url = $url;
91 :     my $ua = LWP::UserAgent->new();
92 :     # Create the SAPserver object.
93 :     my $retVal = {
94 :     server_url => $server_url,
95 :     ua => $ua,
96 : parrello 1.5 singleton => $singleton,
97 : parrello 1.1 };
98 :     # Bless and return it.
99 :     bless $retVal, $class;
100 :     return $retVal;
101 :     }
102 :    
103 :     =head2 Public Methods
104 :    
105 : parrello 1.5 All L<SAP/Primary Methods> are also methods of this object.
106 :    
107 : parrello 1.1 =head3 AUTOLOAD
108 :    
109 :     my $result = $ss->method(%args);
110 :    
111 :     Call a function on the server. Any method call on this object (other than
112 :     the constructor) is translated into a request against the server. This
113 :     enables us to add new server functions without requiring an update to this
114 :     module. The parameters are specified as a hash, and the result is a scalar
115 : parrello 1.2 or object reference. If an error occurred, we will throw an exception.
116 : parrello 1.1
117 :     =cut
118 :    
119 :     # This variable will contain the method name.
120 :     our $AUTOLOAD;
121 :    
122 :     sub AUTOLOAD {
123 : parrello 1.5 # Get the parameters. We do some fancy dancing to allow the user to pass
124 :     # in a hash or a hash reference.
125 :     my $self = shift @_;
126 :     my $args = $_[0];
127 :     if (defined $args && ref $args ne 'HASH') {
128 :     my %args = @_;
129 :     $args = \%args;
130 :     }
131 : parrello 1.1 # Declare the return variable.
132 :     my $retVal;
133 :     # Get the method name.
134 :     my $function = $AUTOLOAD;
135 : parrello 1.3 # Strip off the stuff before the method name.
136 : parrello 1.1 $function =~ s/.+:://;
137 :     # Compute the argument document.
138 : parrello 1.5 my $argString = YAML::Dump($args);
139 : parrello 1.1 # Get our user agent.
140 :     my $ua = $self->{ua};
141 :     # Request the function from the server.
142 :     my $response = $ua->post($self->{server_url},
143 : parrello 1.5 [function => $function, args => $argString,
144 :     source => __PACKAGE__ ]);
145 : parrello 1.1 # Get the response content.
146 :     my $content = $response->content;
147 :     if (! $response->is_success) {
148 : parrello 1.2 die "Server error " . $response->status_line . "\n$content";
149 : parrello 1.1 } else {
150 :     $retVal = YAML::Load($content);
151 :     # Figure out what we got back.
152 :     my $returnType = ref $retVal;
153 : parrello 1.5 if ($returnType) {
154 :     if ($returnType eq 'ErrorDocument') {
155 :     # Here an error occurred, so we throw an exception using the
156 :     # error message.
157 :     die $retVal->{message};
158 :     } elsif ($self->{singleton} && $returnType eq 'HASH' &&
159 :     scalar(keys %$retVal) <= 1) {
160 :     # Here we're in singleton mode and we got a single result,
161 :     # so we dereference a bit to make it easier for the user
162 :     # to access it.
163 :     ($retVal) = values %$retVal;
164 :     }
165 : parrello 1.1 }
166 :     }
167 :     # Return the result.
168 :     return $retVal;
169 :     }
170 :    
171 :     =head3 DESTROY
172 :    
173 :     $ss->DESTROY();
174 :    
175 :     This method has no function. It's purpose is to keep the destructor from
176 :     being caught by the autoload processing.
177 :    
178 :     =cut
179 :    
180 :     sub DESTROY { }
181 :    
182 :    
183 : parrello 1.2 1;

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3