t/AlignIO/AlignIO.t: fix number of tests in plan (fixup c523e6bed866)
[bioperl-live.git] / Bio / WebAgent.pm
blob5daa1d5ee730f498ed673e9bb2e5d7e01fc7f202
2 # BioPerl module for Bio::WebAgent
4 # Please direct questions and support issues to <bioperl-l@bioperl.org>
6 # Cared for by Heikki Lehvaslaiho, heikki-at-bioperl-dot-org
7 # For copyright and disclaimer see below.
10 # POD documentation - main docs before the code
12 =head1 NAME
14 Bio::WebAgent - A base class for Web (any protocol) access
16 =head1 SYNOPSIS
18 # This is a abstract superclass for bioperl modules accessing web
19 # resources - normally you do not instantiate it but one of its
20 # subclasess.
22 =head1 DESCRIPTION
24 This abstract superclass is a subclass of L<LWP::UserAgent> which
25 allows protocol independent access of remote locations over
26 the Net.
28 It takes care of error handling, proxies and various net protocols.
29 BioPerl classes accessing the net should inherit from it. For details,
30 see L<LWP::UserAgent>.
32 The interface is still evolving. For now, two public methods have been
33 copied from Bio::DB::WebDBSeqI: delay() and delay_policy. These are
34 used to prevent overwhelming the server by rapidly repeated . Ideally
35 there should be a common abstract superclass with these. See L<delay>.
37 =head1 SEE ALSO
39 L<LWP::UserAgent>,
40 L<Bio::DB::WebDBSeqI>,
42 =head1 FEEDBACK
44 =head2 Mailing Lists
46 User feedback is an integral part of the evolution of this and other
47 Bioperl modules. Send your comments and suggestions preferably to
48 the Bioperl mailing list. Your participation is much appreciated.
50 bioperl-l@bioperl.org - General discussion
51 http://bioperl.org/wiki/Mailing_lists - About the mailing lists
53 =head2 Support
55 Please direct usage questions or support issues to the mailing list:
57 I<bioperl-l@bioperl.org>
59 rather than to the module maintainer directly. Many experienced and
60 reponsive experts will be able look at the problem and quickly
61 address it. Please include a thorough description of the problem
62 with code and data examples if at all possible.
64 =head2 Reporting Bugs
66 Report bugs to the Bioperl bug tracking system to help us keep track
67 of the bugs and their resolution. Bug reports can be submitted via the
68 web:
70 https://github.com/bioperl/bioperl-live/issues
72 =head1 AUTHOR
74 Heikki Lehvaslaiho, heikki-at-bioperl-dot-org
76 =head1 COPYRIGHT
78 Copyright (c) 2003, Heikki Lehvaslaiho and EMBL-EBI.
79 All Rights Reserved.
81 This module is free software; you can redistribute it and/or modify
82 it under the same terms as Perl itself.
84 =head1 DISCLAIMER
86 This software is provided "as is" without warranty of any kind.
88 =head1 APPENDIX
90 The rest of the documentation details each of the object
91 methods. Internal methods are usually preceded with a _
93 =cut
96 # Let the code begin...
98 package Bio::WebAgent;
99 use vars qw($LAST_INVOCATION_TIME);
100 use strict;
102 use base qw(LWP::UserAgent Bio::Root::Root);
105 sub new {
106 my $class = shift;
108 # We make env_proxy the default here, but it can be
109 # over-ridden by $self->env_proxy later,
110 # or by new(env_proxy=>0) at constructor time
112 my $self = $class->SUPER::new(env_proxy => 1);
114 while( @_ ) {
115 my $key = shift;
116 $key =~ s/^-//;
117 my $value = shift;
118 $self->can($key) || next;
119 $self->$key($value);
122 return $self; # success - we hope!
127 # -----------------------------------------------------------------------------
129 =head2 url
131 Usage : $agent->url
132 Returns : URL to reach out to Net
133 Args : string
135 =cut
137 sub url {
138 my ($self,$value) = @_;
139 if( defined $value) {
140 $self->{'_url'} = $value;
142 return $self->{'_url'};
146 =head2 delay
148 Title : delay
149 Usage : $secs = $self->delay([$secs])
150 Function: get/set number of seconds to delay between fetches
151 Returns : number of seconds to delay
152 Args : new value
154 NOTE: the default is to use the value specified by delay_policy().
155 This can be overridden by calling this method, or by passing the
156 -delay argument to new().
158 =cut
160 sub delay {
161 my ($self, $value) = @_;
162 if ($value) {
163 $self->throw("Need a positive integer, not [$value]")
164 unless $value >= 0;
165 $self->{'_delay'} = int $value;
167 return $self->{'_delay'} || $self->delay_policy;
170 =head2 delay_policy
172 Title : delay_policy
173 Usage : $secs = $self->delay_policy
174 Function: return number of seconds to delay between calls to remote db
175 Returns : number of seconds to delay
176 Args : none
178 NOTE: The default delay policy is 3s. Override in subclasses to
179 implement other delays. The timer has only second resolution, so the delay
180 will actually be +/- 1s.
182 =cut
184 sub delay_policy {
185 my $self = shift;
186 return 3;
190 =head2 sleep
192 Title : sleep
193 Usage : $self->sleep
194 Function: sleep for a number of seconds indicated by the delay policy
195 Returns : none
196 Args : none
198 NOTE: This method keeps track of the last time it was called and only
199 imposes a sleep if it was called more recently than the delay_policy()
200 allows.
202 =cut
204 sub sleep {
205 my $self = shift;
206 $LAST_INVOCATION_TIME ||= 0;
207 if (time - $LAST_INVOCATION_TIME < $self->delay) {
208 my $delay = $self->delay - (time - $LAST_INVOCATION_TIME);
209 $self->debug("sleeping for $delay seconds\n");
210 sleep $delay;
212 $LAST_INVOCATION_TIME = time;
217 __END__