sync from trunk
[bioperl-live.git] / Bio / WebAgent.pm
blob117f5dd2e186c9d6db5d533044b2f3c2747a46ac
1 # $Id$
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
11 =head1 NAME
13 Bio::WebAgent - A base class for Web (any protocol) access
15 =head1 SYNOPSIS
17 # This is a abstract superclass for bioperl modules accessing web
18 # resources - normally you do not instantiate it but one of its
19 # subclasess.
21 =head1 DESCRIPTION
23 This abstract superclass is a subclass of L<LWP::UserAgent> which
24 allows protocol independent access of remote locations over
25 the Net.
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>.
36 =head1 SEE ALSO
38 L<LWP::UserAgent>,
39 L<Bio::DB::WebDBSeqI>,
41 =head1 FEEDBACK
43 =head2 Mailing Lists
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
52 =head2 Reporting Bugs
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
56 web:
58 http://bugzilla.open-bio.org/
60 =head1 AUTHOR
62 Heikki Lehvaslaiho, heikki-at-bioperl-dot-org
64 =head1 COPYRIGHT
66 Copyright (c) 2003, Heikki Lehvaslaiho and EMBL-EBI.
67 All Rights Reserved.
69 This module is free software; you can redistribute it and/or modify
70 it under the same terms as Perl itself.
72 =head1 DISCLAIMER
74 This software is provided "as is" without warranty of any kind.
76 =head1 APPENDIX
78 The rest of the documentation details each of the object
79 methods. Internal methods are usually preceded with a _
81 =cut
84 # Let the code begin...
86 package Bio::WebAgent;
87 use vars qw($LAST_INVOCATION_TIME);
88 use strict;
90 use base qw(LWP::UserAgent Bio::Root::Root);
93 sub new {
94 my $class = shift;
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);
102 while( @_ ) {
103 my $key = shift;
104 $key =~ s/^-//;
105 my $value = shift;
106 $self->can($key) || next;
107 $self->$key($value);
110 return $self; # success - we hope!
115 # -----------------------------------------------------------------------------
117 =head2 url
119 Usage : $agent->url
120 Returns : URL to reach out to Net
121 Args : string
123 =cut
125 sub url {
126 my ($self,$value) = @_;
127 if( defined $value) {
128 $self->{'_url'} = $value;
130 return $self->{'_url'};
134 =head2 delay
136 Title : delay
137 Usage : $secs = $self->delay([$secs])
138 Function: get/set number of seconds to delay between fetches
139 Returns : number of seconds to delay
140 Args : new value
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().
146 =cut
148 sub delay {
149 my ($self, $value) = @_;
150 if ($value) {
151 $self->throw("Need a positive integer, not [$value]")
152 unless $value >= 0;
153 $self->{'_delay'} = int $value;
155 return $self->{'_delay'} || $self->delay_policy;
158 =head2 delay_policy
160 Title : 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
164 Args : none
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.
170 =cut
172 sub delay_policy {
173 my $self = shift;
174 return 3;
178 =head2 sleep
180 Title : sleep
181 Usage : $self->sleep
182 Function: sleep for a number of seconds indicated by the delay policy
183 Returns : none
184 Args : none
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()
188 allows.
190 =cut
192 sub sleep {
193 my $self = shift;
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");
198 sleep $delay;
200 $LAST_INVOCATION_TIME = time;
205 __END__