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

Annotation of /FigKernelPackages/SAPserver.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

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

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3