ensembl-hive  2.7.0
Net.pm
Go to the documentation of this file.
1 =head1 LICENSE
2 
3 See the NOTICE file distributed with this work for additional information
4 regarding copyright ownership.
5 
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
9 
10  http://www.apache.org/licenses/LICENSE-2.0
11 
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.
17 
18 =cut
19 
20 package Bio::EnsEMBL::Utils::Net;
21 
22 =pod
23 
24 
25 =head1 CONTACT
26 
27  Please email comments or questions to the public Ensembl
28  developers list at <http://lists.ensembl.org/mailman/listinfo/dev>.
29 
30  Questions may also be sent to the Ensembl help desk at
31  <http://www.ensembl.org/Help/Contact>.
32 
33 =cut
34 
35 =pod
36 
37 =head1 NAME
38 
40 
41 =head1 SYNOPSIS
42 
43  use Bio::EnsEMBL::Utils::Net qw/do_GET do_FTP/;
44 
45  #Doing a HTTP get
46  my $google_contents = do_GET('http://www.google.co.uk/');
47 
48  #Doing a FTP request; delegates onto LWP
49  my $ftp_contents = do_GET('https://ftp.ensembl.org/pub/current_README');
50 
51 =head1 DESCRIPTION
52 
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.
56 
57 =head1 METHODS
58 
59 See subroutines.
60 
61 =head1 VERSION
62 
63 $Revision$
64 
65 =cut
66 
67 use strict;
68 use warnings;
69 
70 use base qw/Exporter/;
71 use Time::HiRes;
72 
73 our @EXPORT_OK;
74 
75 @EXPORT_OK = qw(
76  do_GET
77  do_FTP
78  do_FTP_to_file
79 );
80 
81 use Bio::EnsEMBL::Utils::Exception qw(throw);
82 
83 our $HTTP_TINY = 0;
84 our $LWP = 0;
85 eval {
86  require HTTP::Tiny;
87  $HTTP_TINY = 1;
88 };
89 eval {
90  require LWP::UserAgent;
91  $LWP = 1;
92 };
93 
94 =head2 do_GET
95 
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
101  supported
102  Description : Performs a HTTP GET method call to return the specified remote
103  resource.
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
108  number of attempts.
109  Status : Stable
110 
111 =cut
112 
113 sub do_GET {
114  my ($url, $total_attempts, $sleep) = @_;
115  return _retry_sleep(sub {
116  if($HTTP_TINY) {
117  return _get_http_tiny($url);
118  }
119  elsif($LWP) {
120  return _get_lwp($url);
121  }
122  else {
123  throw "Cannot continue. You do not have HTTP::Tiny or LWP available."
124  }
125  }, $total_attempts, $sleep);
126 }
127 
128 =head2 do_FTP
129 
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
135  supported
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
142  number of attempts.
143  Status : Stable
144 
145 =cut
146 
147 sub do_FTP {
148  my ($url, $total_attempts, $sleep) = @_;
149  return _retry_sleep(sub {
150  return _get_lwp($url);
151  }, $total_attempts, $sleep);
152 }
153 
154 =head2 do_FTP_to_file
155 
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
161  supported
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
167  number of attempts.
168  Status : Stable
169 
170 =cut
171 
172 sub do_FTP_to_file {
173  my ($url, $total_attempts, $sleep, $filename) = @_;
174  return _retry_sleep(sub {
175  return _get_lwp_to_file($url, $filename);
176  }, $total_attempts, $sleep);
177 }
178 
179 sub _retry_sleep {
180  my ($callback, $total_attempts, $sleep) = @_;
181  $total_attempts ||= 1;
182  $sleep ||= 0;
183  my $response;
184  my $retries = 0;
185  my $fail = 1;
186  while($retries <= $total_attempts) {
187  $response = $callback->();
188  if(defined $response) {
189  $fail = 0;
190  last;
191  }
192  $retries++;
193  Time::HiRes::sleep($sleep);
194  }
195  if($fail) {
196  throw "Could not request remote resource after $total_attempts attempts";
197  }
198  return $response;
199 }
200 
201 sub _get_http_tiny {
202  my ($url) = @_;
203  my $response = HTTP::Tiny->new->get($url);
204  return unless $response->{success};
205  return $response->{content} if length $response->{content};
206  return;
207 }
208 
209 sub _get_lwp {
210  my ($url) = @_;
211  throw "Cannot perform action as LWP::UserAgent is not available" unless $LWP;
212  my $ua = LWP::UserAgent->new();
213  $ua->env_proxy;
214  my $response = $ua->get($url);
215  return $response->decoded_content if $response->is_success;
216  return;
217 }
218 
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();
224  $ua->env_proxy;
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;
229 }
230 
231 1;
Bio::EnsEMBL::Utils::Net
Definition: Net.pm:25
Bio::EnsEMBL::Utils::Exception
Definition: Exception.pm:68