BioPerl
view release on metacpan or search on metacpan
Bio/DB/GenericWebAgent.pm view on Meta::CPAN
-file - dumps the response to a file (handy for large responses)
Note: can't use file and callback at the same time
-read_size_hint - bytes of content to read in at a time to pass to callback
Note : Caching and parameter checking are set
=cut
# TODO deal with small state-related bug with file
sub get_Response {
my ($self, @args) = @_;
my ($cache, $file, $cb, $size) = $self->_rearrange([qw(CACHE_RESPONSE FILE CB READ_SIZE_HINT)],@args);
$self->throw("Can't have both callback and file") if $file && $cb;
# make -file accept more perl-like write-append type data.
$file =~ s{^>}{} if $file;
my @opts = grep {defined $_} ($file || $cb, $size);
$cache = (defined $cache && $cache == 0) ? 0 : 1;
my $pobj = $self->parameter_base;
if ($pobj->parameters_changed ||
!$cache ||
!$self->{_response_cache} ||
!$self->{_response_cache}->content) {
my $ua = $self->ua;
$self->_sleep; # institute delay policy
$self->throw('No parameter object set; cannot form a suitable remote request') unless $pobj;
my $request = $pobj->to_request;
if ($self->authentication) {
$request->proxy_authorization_basic($self->authentication)
}
$self->debug("Request is: \n",$request->as_string);
# I'm relying on the useragent to throw the proper errors here
my $response = $ua->request($request, @opts);
if ($response->is_error) {
$self->throw("Response Error\n".$response->message);
}
return $self->{_response_cache} = $response;
} else {
$self->debug("Returning cached HTTP::Response object\n");
if ($file) {
$self->_dump_request_content($file);
# size isn't passed here, as the content is completely retrieved above
} elsif ($cb) {
$cb && ref($cb) eq 'CODE' && $cb->($self->{_response_cache}->content);
}
return $self->{_response_cache};
}
}
=head2 get_Parser
Title : get_Parser
Usage : $agent->get_Parser;
Function: Return HTTP::Response content (file, fh, object) attached to defined parser
Returns : None
Args : None
Note : Abstract method; defined by implementation
=cut
sub get_Parser {
shift->throw_not_implemented;
}
=head2 delay
Title : delay
Usage : $secs = $self->delay($secs)
Function: get/set number of seconds to delay between fetches
Returns : number of seconds to delay
Args : new value
NOTE: the default is to use the value specified by delay_policy().
This can be overridden by calling this method.
=cut
sub delay {
my $self = shift;
return $self->{'_delay'} = shift if @_;
return $self->{'_delay'};
}
=head2 delay_policy
Title : delay_policy
Usage : $secs = $self->delay_policy
Function: return number of seconds to delay between calls to remote db
Returns : number of seconds to delay
Args : none
NOTE: The default delay policy is 3s. Override in subclasses to
implement delays. The timer has only second resolution, so the delay
will actually be +/- 1s.
=cut
sub delay_policy {
my $self = shift;
return 3;
}
=head2 _sleep
Title : _sleep
Usage : $self->_sleep
Function: sleep for a number of seconds indicated by the delay policy
Returns : none
Args : none
NOTE: This method keeps track of the last time it was called and only
imposes a sleep if it was called more recently than the delay_policy()
allows.
=cut
sub _sleep {
my $self = shift;
my $last_invocation = $LAST_INVOCATION_TIME;
if (time - $LAST_INVOCATION_TIME < $self->delay) {
my $delay = $self->delay - (time - $LAST_INVOCATION_TIME);
$self->debug("sleeping for $delay seconds\n");
( run in 1.443 second using v1.01-cache-2.11-cpan-39bf76dae61 )