Astro/Catalog/Transport/REST.pm

filter: Statementstmt 80.3% 98 / 122 Branchbran 50.0% 13 / 26 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 80.0% 20 / 25 Podpod 100% 8 / 8 totaltotal 75.2% 140 / 186 CC 16 SCAR 10.8
CC
16
Cov
74%
url
7.5
proxy
6.0
timeout
6.0
CRAP 2.9
« Astro/Catalog/Transport/WebService.pm ↑ Summary Astro/Catalog/Query/Vizier.pm »
1 package Astro::Catalog::Transport::REST;
2  
3 =head1 NAME
4  
5 Astro::Catalog::Transport::REST - A base class for REST query modules
6  
7 =head1 SYNOPSIS
8  
9 use base qw/Astro::Catalog::Transport::REST/;
10  
11  
12 =head1 DESCRIPTION
13  
14 This class forms a base class for all the REST based query classes provided
15 in the C<Astro::Catalog> distribution (eg C<Astro::Catalog::Query::GSC>).
16  
17 =cut
18  
19 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 15 ▶use warnings::register;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 15 ▶use base qw/Astro::Catalog::Query/;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 15 ▶use LWP::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 15 ▶use Net::Domain qw/hostname hostdomain/;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 15 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 15 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 15 ▶use Astro::Catalog;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 15 ▶use Astro::Catalog::Item;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 15 our $VERSION = '4.38';
33  
34 =head1 METHODS
35  
36 =head2 Constructor
37  
38 =over 4
39  
40 =item B<new>
41  
42 Create a new instance from a hash of options
43  
44 $q = new Astro::Catalog::Transport::REST(
45 Coords => new Astro::Coords(),
46 Radius => $radius,
47 Bright => $magbright,
48 Faint => $magfaint,
49 Sort => $sort_type,
50 Number => $number_out);
51  
52 returns a reference to an query object. Must only called from
53 sub-classed constructors.
54  
55 RA and Dec are also allowed but are deprecated (since with only
56 RA/Dec the coordinates must always be supplied as J2000 space-separated
57 sexagesimal format).
58  
59 =cut
60  
61 4 ▶sub new {
Subroutine new: called CC=2 SCAR=7.2
Pod documented
62 4 my $proto = shift;
63 4 ▶ my $class = ref($proto) || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
040
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
64  
65 # bless the query hash into the class
66 4 my $block = bless {
67 OPTIONS => {},
68 COORDS => undef,
69 URL => undef,
70 QUERY => undef,
71 USERAGENT => undef,
72 BUFFER => undef,
73 }, $class;
74  
75 # Configure the object [even if there are no args]
76 4 $block->configure(@_);
77  
78 3 return $block;
79 }
80  
81 =item B<useragent>
82  
83 The LWP user agent mediating the web transaction.
84  
85 $ua = $q->useragent();
86  
87 Created automatically the first time it is requested.
88  
89 =cut
90  
91 6 ▶sub useragent {
Subroutine useragent: called CC=3 SCAR=11.0
Pod documented
92 6 my $self = shift;
93 6 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 3 3 6
94 3 my $ua = shift;
95 3 ▶ croak "Must be a LWP::UserAgent"
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($ua, "LWP::UserAgent") 0 3 3
96 unless UNIVERSAL::isa($ua, "LWP::UserAgent");
97 3 $self->{USERAGENT} = $ua;
98 }
99 else {
100 # If we have no UA but we have requested one, create it ourself
101 # This overcomes a chicken and egg situation if a subclass
102 # wants to go out on the net during object instantiation
103 # before configure() has been called
104  
105 # Setup the LWP::UserAgent
106 3 my $ua = new LWP::UserAgent(timeout => 30);
107  
108 3 $self->useragent($ua);
109 3 $ua->agent($self->_default_useragent_id);
110  
111 # Grab Proxy details from local environment
112 3 $ua->env_proxy();
113 }
114 6 return $self->{USERAGENT};
115 }
116  
117 =item B<querydb>
118  
119 Returns an Astro::Catalog object resulting from the specific query.
120  
121 $catalog = $q->querydb();
122  
123 =cut
124  
125 3 ▶sub querydb {
Subroutine querydb: called CC=2 SCAR=7.0
Pod documented
126 3 my $self = shift;
127  
128 # call the private method to make the actual query
129 3 $self->_make_query();
130  
131 # check for failed connect
132 1 ▶ return undef unless defined $self->{BUFFER};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"BUFFER"} 0 1 1
133  
134 # return catalog
135 1 return $self->_parse_query();
136 }
137  
138 =item B<proxy>
139  
140 Return (or set) the current proxy for the catalog request.
141  
142 $usno->proxy('http://wwwcache.ex.ac.uk:8080/');
143 $proxy_url = $usno->proxy();
144  
145 =cut
146  
147 0 ▶sub proxy {
Subroutine proxy: not called CC=2 SCAR=17.9
Pod documented
148 0 my $self = shift;
149  
150 # grab local reference to user agent
151 0 my $ua = $self->useragent;
152  
153 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
154 0 my $proxy_url = shift;
155 0 $ua->proxy('http', $proxy_url);
156 }
157  
158 # return the current proxy
159 0 return $ua->proxy('http');
160 }
161  
162 =item B<timeout>
163  
164 Return (or set) the current timeout in seconds for the request.
165  
166 $usno->timeout(30);
167 $proxy_timeout = $usno->timeout();
168  
169 Default is 30 seconds.
170  
171 =cut
172  
173 0 ▶sub timeout {
Subroutine timeout: not called CC=2 SCAR=17.9
Pod documented
174 0 my $self = shift;
175  
176 # grab local reference to user agent
177 0 my $ua = $self->useragent;
178  
179 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
180 0 my $time = shift;
181 0 $ua->timeout($time);
182 }
183  
184 # return the current timeout
185 0 return $ua->timeout();
186 }
187  
188 =item B<query_url>
189  
190 The URL formed to build up a query. Made up of a root host name
191 (that can be set using the C<url> method) and a fixed suffix that
192 specifies the path to the service (CGI or otherwise). This query URL
193 does not include the arguments to the CGI script (but will include
194 the question mark if appropriate).
195  
196 $query_url = $q->query();
197 $q->query_url('http://www.blah.org/cgi-bin/xxx.pl?');
198  
199 Care must be taken when setting this value.
200  
201 The argument is not validated. There may also need to be a new method
202 that returns the full URL including arguments.
203  
204 If no value has been supplied, a default will be returned.
205  
206 =cut
207  
208 3 ▶sub query_url {
Subroutine query_url: called CC=3 SCAR=13.5
Pod documented
209 3 my $self = shift;
210 3 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 3 3
211 0 $self->{QUERY} = shift;
212 }
213 3 ▶ if (defined $self->{QUERY}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'QUERY'}) { } 0 3 3
214 0 return $self->{QUERY};
215 }
216 else {
217 3 return "http://". $self->url .
218 "/" . $self->_default_url_path;
219 }
220  
221 0 return $self->{QUERY};
222 }
223  
224 =item B<url>
225  
226 Return the current remote host for the query (the full URL
227 can be returned using the C<query_url> method).
228  
229 $host = $q->url();
230  
231 Can also be used to set the root host for the URL (ie the
232 machine name but no path component)
233  
234 $q->url("archive.eso.org");
235  
236 if not defined the default URL is used (specified in the sub class).
237 This method should really be called C<remote_host>.
238  
239 Returns the default host name specified by the particular subclass
240 if a value has not been defined.
241  
242 =cut
243  
244 3 ▶sub url {
Subroutine url: called CC=4 SCAR=20.1
Pod documented
245 3 my $self = shift;
246  
247 # SETTING URL
248 3 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 3 3
249 # set the url option
250 0 my $base_url = shift;
251 0 $self->{URL} = $base_url;
252 0 ▶ if (defined $base_url) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $base_url) 0 0 0
253 0 $self->query_url(
254 "http://$base_url/" . $self->_default_url_path);
255 }
256 }
257  
258 # RETURNING remote host
259 3 ▶ if (defined $self->{URL}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'URL'}) { } 0 3 3
260 0 return $self->{URL};
261 }
262 else {
263 3 return $self->_default_remote_host();
264 }
265 }
266  
267 =item B<agent>
268  
269 Returns the user agent tag sent by the module to the server.
270  
271 $agent_tag = $q->agent();
272  
273 The user agent tag can not be set by this method.
274  
275 =cut
276  
277 0 ▶sub agent {
Subroutine agent: not called CC=1 SCAR=6.9
Pod documented
278 0 my $self = shift;
279 0 return $self->useragent->agent();
280 }
281  
282 =back
283  
284 =begin __PRIVATE_METHODS__
285  
286 =head2 Private methods
287  
288 These methods are for internal use only.
289  
290 =over 4
291  
292 =item B<_default_remote_host>
293  
294 The default host name to use to build up the full URL.
295 Must be specified in a sub-class.
296  
297 $host = $q->_default_remote_host();
298  
299 =cut
300  
301 0 ▶sub _default_remote_host {
Subroutine _default_remote_host: not called CC=1 SCAR=6.9
302 0 croak "default remote host must be specified in subclass\n";
303 }
304  
305 =item B<_default_url_path>
306  
307 The path information after the host in the remote URL.
308 Must be overridden in a subclass.
309  
310 =cut
311  
312 0 ▶sub _default_url_path {
Subroutine _default_url_path: not called CC=1 SCAR=6.9
313 0 croak "default url path information must be subclassed\n";
314 }
315  
316 =item B<_default_useragent_id>
317  
318 Default user agent ID used to declare the agent to the remote server.
319 Default format is
320  
321 __PACKAGE__/$VERSION ($HOST.$DOMAIN)
322  
323 This can be overridden in a subclass if necessary.
324  
325 =cut
326  
327 3 ▶sub _default_useragent_id {
Subroutine _default_useragent_id: called CC=2 SCAR=6.9
328 3 my $self = shift;
329 3 my $HOST = hostname();
330 3 my $DOMAIN = hostdomain();
331 3 my $package = ref($self);
332 3 my $pack_version;
333 {
334 # Need a symbolic reference
335 15 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
336 3 $pack_version = ${ $package."::VERSION" };
337 }
338 3 ▶ $pack_version = 'UNKNOWN' unless defined $pack_version;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pack_version 0 3 3
339 3 return "Astro::Catalog::REST/$pack_version ($HOST.$DOMAIN)";
340 }
341  
342  
343 =item B<_make_query>
344  
345 Private function used to make an query. Should not be called directly,
346 since it does not parse the results. Instead use the querydb()
347 method.
348  
349 =cut
350  
351 3 ▶sub _make_query {
Subroutine _make_query: called CC=1 SCAR=0.0
352 3 my $self = shift;
353  
354 # clean out the buffer
355 3 $self->{BUFFER} = "";
356  
357 # Build the query URL
358 3 my $URL = $self->_build_query();
359  
360 # Run the actual HTTP query
361 # and get the retrieved buffer
362 3 $self->{BUFFER} = $self->_fetch_url( $URL );
363  
364 1 return;
365 }
366  
367 =item B<_fetch_url>
368  
369 Simple wrapper around LWP to retrieve content from a remote
370 URL and return it as a single string.
371  
372 $result = $q->_fetch_url($URL);
373  
374 =cut
375  
376 3 ▶sub _fetch_url {
Subroutine _fetch_url: called CC=2 SCAR=6.9
377 3 my $self = shift;
378 3 my $URL = shift;
379  
380 # grab the user agent
381 3 my $ua = $self->useragent;
382  
383 # build request
384 3 my $request = new HTTP::Request('GET', $URL);
385  
386 # grab page from web
387 3 my $reply = $ua->request($request);
388  
389 # Look at the result to see if it worked
390 3 ▶ if (${$reply}{"_rc"} eq 200) {
Branch: 1 branch
BranchTrueFalseTotal
if (do { $reply }->{'_rc'} eq 200) { } 1 2 3
391 # stuff the page contents into the buffer
392 1 return ${$reply}{"_content"};
393 }
394 else {
395 2 croak("Error ${$reply}{_rc}: Failed to establish network connection using url $URL");
396 }
397 }
398  
399 =item B<_build_query>
400  
401 Build the URL to be sent to the remote service. The default method
402 concatenates the C<query_url> along with all the defined query options
403 combined using key=value pairs separated by &.
404  
405 $url = $q->_build_query();
406  
407 If the URL can not be built simply by concatenation (eg it requires
408 token replacement), then a subclassed method will be required.
409  
410 =cut
411  
412 3 ▶sub _build_query {
Subroutine _build_query: called CC=3 SCAR=11.0
413 3 my $self = shift;
414  
415 # grab the base URL
416 3 my $URL = $self->query_url;
417 3 my $options = "";
418  
419 # loop round all the options keys and build the query
420 3 my %allow = $self->_get_allowed_options;
421  
422 # Translate options
423 3 my %translated = $self->_translate_options();
424  
425 3 foreach my $key ( keys %translated) {
426 $options .= "&$key=" . $translated{$key}
427 38 ▶ if defined $translated{$key};
Branch: 1 branch
BranchTrueFalseTotal
if defined $translated{$key} 35 3 38
428 }
429  
430 # Remove the leading ampersand from the options list because
431 # it can cause some forms to fail.
432 3 $options =~ s/^&//;
433  
434 # build final query URL
435 3 $URL = $URL . $options;
436  
437 3 return $URL;
438 }
439  
440 15 1;
441  
442 __END__
« Astro/Catalog/Transport/WebService.pm ↑ Summary Astro/Catalog/Query/Vizier.pm »