EnsEMBL/Utils/Net.pm untested

filter: Statementstmt 0.0% 0 / 84 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 4 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 131 CC 12 SCAR 22.7
CC
12
Cov
0%
_get_lwp_to_file
20.0
do_GET
12.0
_retry_sleep
12.0
CRAP 9.7
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 $Bio::EnsEMBL::Utils::Net::VERSION = '114.0.0_50'; # TRIAL
22 $Bio::EnsEMBL::Utils::Net::VERSION = '114.0.050';
23 =pod
24  
25  
26 =head1 CONTACT
27  
28 Please email comments or questions to the public Ensembl
29 developers list at <http://lists.ensembl.org/mailman/listinfo/dev>.
30  
31 Questions may also be sent to the Ensembl help desk at
32 <http://www.ensembl.org/Help/Contact>.
33  
34 =cut
35  
36 =pod
37  
38 =head1 NAME
39  
40 Bio::EnsEMBL::Utils::Net
41  
42 =head1 SYNOPSIS
43  
44 use Bio::EnsEMBL::Utils::Net qw/do_GET do_FTP/;
45  
46 #Doing a HTTP get
47 my $google_contents = do_GET('http://www.google.co.uk/');
48
49 #Doing a FTP request; delegates onto LWP
50 my $ftp_contents = do_GET('https://ftp.ensembl.org/pub/current_README');
51  
52 =head1 DESCRIPTION
53  
54 A collection of subroutines aimed to helping network based operations. The code
55 will use HTTP::Tiny for all HTTP operations if available. Otherwise it
56 will delegate to LWP. LWP is currently the only supported target for FTP.
57  
58 =head1 METHODS
59  
60 See subroutines.
61  
62 =head1 VERSION
63  
64 $Revision$
65  
66 =cut
67  
68 use strict;
69 use warnings;
70  
71 use base qw/Exporter/;
72 use Time::HiRes;
73  
74 our @EXPORT_OK;
75  
76 @EXPORT_OK = qw(
77 do_GET
78 do_FTP
79 do_FTP_to_file
80 );
81  
82 use Bio::EnsEMBL::Utils::Exception qw(throw);
83  
84 our $HTTP_TINY = 0;
85 our $LWP = 0;
86 eval {
87 require HTTP::Tiny;
88 $HTTP_TINY = 1;
89 };
90 eval {
91 require LWP::UserAgent;
92 $LWP = 1;
93 };
94  
95 =head2 do_GET
96  
97 Arg [1] : string $url The URL to fetch including all parameters
98 Arg [2] : int; $total_attempts The number of times to try the URL
99 before throwing an exception
100 Arg [3] : number; $sleep Amount of time to sleep between attempts.
101 Delegates onto Time::HiRes so floating point numbers are
102 supported
103 Description : Performs a HTTP GET method call to return the specified remote
104 resource.
105 Returntype : Scalar of the contents of the remote URL. Do not use to
106 retrieve very large amounts of data.
107 Example : my $contents = do_GET('http://www.google.co.uk/');
108 Exceptions : If we could not retrieve the resource after the specified
109 number of attempts.
110 Status : Stable
111  
112 =cut
113  
114 sub do_GET {
115 my ($url, $total_attempts, $sleep) = @_;
116 return _retry_sleep(sub {
117 if($HTTP_TINY) {
118 return _get_http_tiny($url);
119 }
120 elsif($LWP) {
121 return _get_lwp($url);
122 }
123 else {
124 throw "Cannot continue. You do not have HTTP::Tiny or LWP available."
125 }
126 }, $total_attempts, $sleep);
127 }
128  
129 =head2 do_FTP
130  
131 Arg [1] : string $uri
132 Arg [2] : int; $total_attempts The number of times to try the URI
133 before throwing an exception
134 Arg [3] : number; $sleep Amount of time to sleep between attempts.
135 Delegates onto Time::HiRes so floating point numbers are
136 supported
137 Description : Performs a FTP fetch using a non-authenticated connection (
138 however some servers will allow you to encode this in the URI).
139 Returntype : Scalar of the contents of the remote URL. Do not use to
140 retrieve very large amounts of data.
141 Example : my $contents = do_GET('http://www.google.co.uk/');
142 Exceptions : If we could not retrieve the resource after the specified
143 number of attempts.
144 Status : Stable
145  
146 =cut
147  
148 sub do_FTP {
149 my ($url, $total_attempts, $sleep) = @_;
150 return _retry_sleep(sub {
151 return _get_lwp($url);
152 }, $total_attempts, $sleep);
153 }
154  
155 =head2 do_FTP_to_file
156  
157 Arg [1] : string $uri
158 Arg [2] : int; $total_attempts The number of times to try the URI
159 before throwing an exception
160 Arg [3] : number; $sleep Amount of time to sleep between attempts.
161 Delegates onto Time::HiRes so floating point numbers are
162 supported
163 Description : Performs a FTP fetch using a non-authenticated connection (
164 however some servers will allow you to encode this in the URI).
165 Returntype : Boolean true if download was successful.
166 Example : my $contents = do_GET('http://www.google.co.uk/');
167 Exceptions : If we could not retrieve the resource after the specified
168 number of attempts.
169 Status : Stable
170  
171 =cut
172  
173 sub do_FTP_to_file {
174 my ($url, $total_attempts, $sleep, $filename) = @_;
175 return _retry_sleep(sub {
176 return _get_lwp_to_file($url, $filename);
177 }, $total_attempts, $sleep);
178 }
179  
180 sub _retry_sleep {
181 my ($callback, $total_attempts, $sleep) = @_;
182 $total_attempts ||= 1;
183 $sleep ||= 0;
184 my $response;
185 my $retries = 0;
186 my $fail = 1;
187 while($retries <= $total_attempts) {
188 $response = $callback->();
189 if(defined $response) {
190 $fail = 0;
191 last;
192 }
193 $retries++;
194 Time::HiRes::sleep($sleep);
195 }
196 if($fail) {
197 throw "Could not request remote resource after $total_attempts attempts";
198 }
199 return $response;
200 }
201  
202 sub _get_http_tiny {
203 my ($url) = @_;
204 my $response = HTTP::Tiny->new->get($url);
205 return unless $response->{success};
206 return $response->{content} if length $response->{content};
207 return;
208 }
209  
210 sub _get_lwp {
211 my ($url) = @_;
212 throw "Cannot perform action as LWP::UserAgent is not available" unless $LWP;
213 my $ua = LWP::UserAgent->new();
214 $ua->env_proxy;
215 my $response = $ua->get($url);
216 return $response->decoded_content if $response->is_success;
217 return;
218 }
219  
220 sub _get_lwp_to_file {
221 my ($url, $filename) = @_;
222 throw "Cannot perform action as LWP::UserAgent is not available" unless $LWP;
223 throw "Filename required for download to proceed." unless $filename;
224 my $ua = LWP::UserAgent->new();
225 $ua->env_proxy;
226 my $response = $ua->get($url, ":content_file" => $filename);
227 print 'UA Response: '.$response->is_success."\n";
228 if ($response->is_success) {return 1}
229 throw $response->status_line;
230 }
231  
232 1;