3 See the NOTICE file distributed with
this work
for additional information
4 regarding copyright ownership.
6 Licensed under the Apache License, Version 2.0 (the
"License");
7 you may not use
this file except in compliance with the License.
8 You may obtain a copy of the License at
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an
"AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License
for the specific language governing permissions and
16 limitations under the License.
20 package Bio::EnsEMBL::Utils::Net;
27 Please email comments or questions to the
public Ensembl
28 developers list at <http:
30 Questions may also be sent to the Ensembl help desk at
46 my $google_contents = do_GET(
'http://www.google.co.uk/');
48 #Doing a FTP request; delegates onto LWP
49 my $ftp_contents = do_GET(
'https://ftp.ensembl.org/pub/current_README');
53 A collection of subroutines aimed to helping network based operations. The code
54 will use HTTP::Tiny
for all HTTP operations
if available. Otherwise it
55 will delegate to LWP. LWP is currently the only supported target
for FTP.
70 use base qw/Exporter/;
90 require LWP::UserAgent;
96 Arg [1] :
string $url The URL to fetch including all parameters
97 Arg [2] : int; $total_attempts The number of times to
try the URL
98 before throwing an exception
99 Arg [3] : number; $sleep Amount of time to sleep between attempts.
100 Delegates onto Time::HiRes so floating point numbers are
102 Description : Performs a HTTP GET method call to
return the specified remote
104 Returntype : Scalar of the contents of the remote URL. Do not use to
105 retrieve very large amounts of data.
106 Example : my $contents = do_GET(
'http://www.google.co.uk/');
107 Exceptions : If we could not retrieve the resource after the specified
114 my ($url, $total_attempts, $sleep) = @_;
115 return _retry_sleep(sub {
117 return _get_http_tiny($url);
120 return _get_lwp($url);
123 throw "Cannot continue. You do not have HTTP::Tiny or LWP available."
125 }, $total_attempts, $sleep);
130 Arg [1] :
string $uri
131 Arg [2] : int; $total_attempts The number of times to
try the URI
132 before throwing an exception
133 Arg [3] : number; $sleep Amount of time to sleep between attempts.
134 Delegates onto Time::HiRes so floating point numbers are
136 Description : Performs a FTP fetch
using a non-authenticated connection (
137 however some servers will allow you to encode
this in the URI).
138 Returntype : Scalar of the contents of the remote URL. Do not use to
139 retrieve very large amounts of data.
140 Example : my $contents = do_GET(
'http://www.google.co.uk/');
141 Exceptions : If we could not retrieve the resource after the specified
148 my ($url, $total_attempts, $sleep) = @_;
149 return _retry_sleep(sub {
150 return _get_lwp($url);
151 }, $total_attempts, $sleep);
154 =head2 do_FTP_to_file
156 Arg [1] :
string $uri
157 Arg [2] : int; $total_attempts The number of times to
try the URI
158 before throwing an exception
159 Arg [3] : number; $sleep Amount of time to sleep between attempts.
160 Delegates onto Time::HiRes so floating point numbers are
162 Description : Performs a FTP fetch
using a non-authenticated connection (
163 however some servers will allow you to encode
this in the URI).
164 Returntype : Boolean
true if download was successful.
165 Example : my $contents = do_GET(
'http://www.google.co.uk/');
166 Exceptions : If we could not retrieve the resource after the specified
173 my ($url, $total_attempts, $sleep, $filename) = @_;
174 return _retry_sleep(sub {
175 return _get_lwp_to_file($url, $filename);
176 }, $total_attempts, $sleep);
180 my ($callback, $total_attempts, $sleep) = @_;
181 $total_attempts ||= 1;
186 while($retries <= $total_attempts) {
187 $response = $callback->();
188 if(defined $response) {
193 Time::HiRes::sleep($sleep);
196 throw "Could not request remote resource after $total_attempts attempts";
203 my $response = HTTP::Tiny->new->get($url);
204 return unless $response->{success};
205 return $response->{content}
if length $response->{content};
211 throw "Cannot perform action as LWP::UserAgent is not available" unless $LWP;
212 my $ua = LWP::UserAgent->new();
214 my $response = $ua->get($url);
215 return $response->decoded_content
if $response->is_success;
219 sub _get_lwp_to_file {
220 my ($url, $filename) = @_;
221 throw "Cannot perform action as LWP::UserAgent is not available" unless $LWP;
222 throw "Filename required for download to proceed." unless $filename;
223 my $ua = LWP::UserAgent->new();
225 my $response = $ua->get($url,
":content_file" => $filename);
226 print
'UA Response: '.$response->is_success.
"\n";
227 if ($response->is_success) {
return 1}
228 throw $response->status_line;