3 # BioPerl module for Bio::WebAgent
5 # Cared for by Heikki Lehvaslaiho, heikki-at-bioperl-dot-org
6 # For copyright and disclaimer see below.
9 # POD documentation - main docs before the code
13 Bio::WebAgent - A base class for Web (any protocol) access
17 # This is a abstract superclass for bioperl modules accessing web
18 # resources - normally you do not instantiate it but one of its
23 This abstract superclass is a subclass of L<LWP::UserAgent> which
24 allows protocol independent access of remote locations over
27 It takes care of error handling, proxies and various net protocols.
28 BioPerl classes accessing the net should inherit from it. For details,
29 see L<LWP::UserAgent>.
31 The interface is still evolving. For now, two public methods have been
32 copied from Bio::DB::WebDBSeqI: delay() and delay_policy. These are
33 used to prevent overwhelming the server by rapidly repeated . Ideally
34 there should be a common abstract superclass with these. See L<delay>.
39 L<Bio::DB::WebDBSeqI>,
45 User feedback is an integral part of the evolution of this and other
46 Bioperl modules. Send your comments and suggestions preferably to
47 the Bioperl mailing list. Your participation is much appreciated.
49 bioperl-l@bioperl.org - General discussion
50 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
54 Report bugs to the Bioperl bug tracking system to help us keep track
55 of the bugs and their resolution. Bug reports can be submitted via the
58 http://bugzilla.open-bio.org/
62 Heikki Lehvaslaiho, heikki-at-bioperl-dot-org
66 Copyright (c) 2003, Heikki Lehvaslaiho and EMBL-EBI.
69 This module is free software; you can redistribute it and/or modify
70 it under the same terms as Perl itself.
74 This software is provided "as is" without warranty of any kind.
78 The rest of the documentation details each of the object
79 methods. Internal methods are usually preceded with a _
84 # Let the code begin...
86 package Bio
::WebAgent
;
87 use vars
qw($LAST_INVOCATION_TIME);
90 use base qw(LWP::UserAgent Bio::Root::Root);
96 # We make env_proxy the default here, but it can be
97 # over-ridden by $self->env_proxy later,
98 # or by new(env_proxy=>0) at constructor time
100 my $self = $class->SUPER::new
(env_proxy
=> 1);
106 $self->can($key) || next;
110 return $self; # success - we hope!
115 # -----------------------------------------------------------------------------
120 Returns : URL to reach out to Net
126 my ($self,$value) = @_;
127 if( defined $value) {
128 $self->{'_url'} = $value;
130 return $self->{'_url'};
137 Usage : $secs = $self->delay([$secs])
138 Function: get/set number of seconds to delay between fetches
139 Returns : number of seconds to delay
142 NOTE: the default is to use the value specified by delay_policy().
143 This can be overridden by calling this method, or by passing the
144 -delay argument to new().
149 my ($self, $value) = @_;
151 $self->throw("Need a positive integer, not [$value]")
153 $self->{'_delay'} = int $value;
155 return $self->{'_delay'} || $self->delay_policy;
161 Usage : $secs = $self->delay_policy
162 Function: return number of seconds to delay between calls to remote db
163 Returns : number of seconds to delay
166 NOTE: The default delay policy is 3s. Override in subclasses to
167 implement other delays. The timer has only second resolution, so the delay
168 will actually be +/- 1s.
182 Function: sleep for a number of seconds indicated by the delay policy
186 NOTE: This method keeps track of the last time it was called and only
187 imposes a sleep if it was called more recently than the delay_policy()
194 $LAST_INVOCATION_TIME ||= 0;
195 if (time - $LAST_INVOCATION_TIME < $self->delay) {
196 my $delay = $self->delay - (time - $LAST_INVOCATION_TIME);
197 $self->debug("sleeping for $delay seconds\n");
200 $LAST_INVOCATION_TIME = time;