| 1 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 2 | |||||||||||||
| 3 | # $Id: API.pm,v 1.35 2009/08/02 17:16:12 asc Exp $ | ||||||||||||
| 4 | # -*-perl-*- | ||||||||||||
| 5 | |||||||||||||
| 6 | package Net::Flickr::API; | ||||||||||||
| 7 | |||||||||||||
| 8 | 1 | $Net::Flickr::API::VERSION = '1.7'; | |||||||||||
| 9 | |||||||||||||
| 10 | =head1 NAME | ||||||||||||
| 11 | |||||||||||||
| 12 | Net::Flickr::API - base API class for Net::Flickr::* libraries | ||||||||||||
| 13 | |||||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||||
| 15 | |||||||||||||
| 16 | package Net::Flickr::RDF; | ||||||||||||
| 17 | use base qw (Net::Flickr::API); | ||||||||||||
| 18 | |||||||||||||
| 19 | =head1 DESCRIPTION | ||||||||||||
| 20 | |||||||||||||
| 21 | Base API class for Net::Flickr::* libraries | ||||||||||||
| 22 | |||||||||||||
| 23 | Net::Flickr::API is a wrapper for Flickr::API that provides support for throttling | ||||||||||||
| 24 | API calls (per second), retries if the API is disabled and marshalling of API responses | ||||||||||||
| 25 | into XML::LibXML or XML::XPath objects. | ||||||||||||
| 26 | |||||||||||||
| 27 | =head1 OPTIONS | ||||||||||||
| 28 | |||||||||||||
| 29 | Options are passed to Net::Flickr::Backup using a Config::Simple object or | ||||||||||||
| 30 | a valid Config::Simple config file. Options are grouped by "block". | ||||||||||||
| 31 | |||||||||||||
| 32 | =head2 flick | ||||||||||||
| 33 | |||||||||||||
| 34 | =over 4 | ||||||||||||
| 35 | |||||||||||||
| 36 | =item * B<api_key> | ||||||||||||
| 37 | |||||||||||||
| 38 | String. I<required> | ||||||||||||
| 39 | |||||||||||||
| 40 | A valid Flickr API key. | ||||||||||||
| 41 | |||||||||||||
| 42 | =item * B<api_secret> | ||||||||||||
| 43 | |||||||||||||
| 44 | String. I<required> | ||||||||||||
| 45 | |||||||||||||
| 46 | A valid Flickr Auth API secret key. | ||||||||||||
| 47 | |||||||||||||
| 48 | =item * B<auth_token> | ||||||||||||
| 49 | |||||||||||||
| 50 | String. I<required> | ||||||||||||
| 51 | |||||||||||||
| 52 | A valid Flickr Auth API token. | ||||||||||||
| 53 | |||||||||||||
| 54 | =item * B<api_handler> | ||||||||||||
| 55 | |||||||||||||
| 56 | String. I<required> | ||||||||||||
| 57 | |||||||||||||
| 58 | The B<api_handler> defines which XML/XPath handler to use to process API responses. | ||||||||||||
| 59 | |||||||||||||
| 60 | =over 4 | ||||||||||||
| 61 | |||||||||||||
| 62 | =item * B<LibXML> | ||||||||||||
| 63 | |||||||||||||
| 64 | Use XML::LibXML. | ||||||||||||
| 65 | |||||||||||||
| 66 | =item * B<XPath> | ||||||||||||
| 67 | |||||||||||||
| 68 | Use XML::XPath. | ||||||||||||
| 69 | |||||||||||||
| 70 | =back | ||||||||||||
| 71 | |||||||||||||
| 72 | =back | ||||||||||||
| 73 | |||||||||||||
| 74 | =head2 reporting | ||||||||||||
| 75 | |||||||||||||
| 76 | =over | ||||||||||||
| 77 | |||||||||||||
| 78 | =item * B<enabled> | ||||||||||||
| 79 | |||||||||||||
| 80 | Boolean. | ||||||||||||
| 81 | |||||||||||||
| 82 | Default is false. | ||||||||||||
| 83 | |||||||||||||
| 84 | =item * B<handler> | ||||||||||||
| 85 | |||||||||||||
| 86 | String. | ||||||||||||
| 87 | |||||||||||||
| 88 | The default handler is B<Screen>, as in C<Log::Dispatch::Screen> | ||||||||||||
| 89 | |||||||||||||
| 90 | =item * B<handler_args> | ||||||||||||
| 91 | |||||||||||||
| 92 | For example, the following : | ||||||||||||
| 93 | |||||||||||||
| 94 | reporting_handler_args=name:foobar;min_level=info | ||||||||||||
| 95 | |||||||||||||
| 96 | Would be converted as : | ||||||||||||
| 97 | |||||||||||||
| 98 | (name => "foobar", | ||||||||||||
| 99 | min_level => "info"); | ||||||||||||
| 100 | |||||||||||||
| 101 | The default B<name> argument is "__report". The default B<min_level> argument | ||||||||||||
| 102 | is "info". | ||||||||||||
| 103 | |||||||||||||
| 104 | =back | ||||||||||||
| 105 | |||||||||||||
| 106 | =cut | ||||||||||||
| 107 | |||||||||||||
| 108 | 1 | ▶ | use Config::Simple; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 109 | |||||||||||||
| 110 | 1 | ▶ | use Flickr::API; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 111 | 1 | ▶ | use Flickr::Upload; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 112 | |||||||||||||
| 113 | 1 | ▶ | use Readonly; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 114 | 1 | ▶ | use Data::Dumper; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 115 | |||||||||||||
| 116 | 1 | ▶ | use Log::Dispatch; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 117 | 1 | ▶ | use Log::Dispatch::Screen; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 118 | |||||||||||||
| 119 | 1 | Readonly::Scalar my $PAUSE_SECONDS_OK => 2; | |||||||||||
| 120 | 1 | Readonly::Scalar my $PAUSE_SECONDS_UNAVAILABLE => 4; | |||||||||||
| 121 | 1 | Readonly::Scalar my $PAUSE_MAXTRIES => 10; | |||||||||||
| 122 | 1 | Readonly::Scalar my $PAUSE_ONSTATUS => 503; | |||||||||||
| 123 | |||||||||||||
| 124 | 1 | Readonly::Scalar my $RETRY_MAXTRIES => 10; | |||||||||||
| 125 | |||||||||||||
| 126 | =head1 PACKAGE METHODS | ||||||||||||
| 127 | |||||||||||||
| 128 | =cut | ||||||||||||
| 129 | |||||||||||||
| 130 | =head2 __PACKAGE__->new($cfg) | ||||||||||||
| 131 | |||||||||||||
| 132 | Where B<$cfg> is either a valid I<Config::Simple> object or the path | ||||||||||||
| 133 | to a file that can be parsed by I<Config::Simple>. | ||||||||||||
| 134 | |||||||||||||
| 135 | Returns a I<Net::Flickr::API> object. | ||||||||||||
| 136 | |||||||||||||
| 137 | =cut | ||||||||||||
| 138 | |||||||||||||
| 139 | sub new { | ||||||||||||
| 140 | 0 | ▶ | my $pkg = shift; | ||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 141 | 0 | my $cfg = shift; | |||||||||||
| 142 | |||||||||||||
| 143 | 0 | my $self = {'__wait' => time() + $PAUSE_SECONDS_OK, | |||||||||||
| 144 | '__paused' => 0, | ||||||||||||
| 145 | '__retries' => 0,}; | ||||||||||||
| 146 | |||||||||||||
| 147 | 0 | bless $self,$pkg; | |||||||||||
| 148 | |||||||||||||
| 149 | 0 | ▶ | if (! $self->init($cfg)) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 150 | 0 | undef $self; | |||||||||||
| 151 | } | ||||||||||||
| 152 | |||||||||||||
| 153 | 0 | return $self; | |||||||||||
| 154 | } | ||||||||||||
| 155 | |||||||||||||
| 156 | sub init { | ||||||||||||
| 157 | 0 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
init: not called CC=11 SCAR=47.7
Pod
undocumented
| |||||||||||||
| 158 | 0 | my $cfg = shift; | |||||||||||
| 159 | |||||||||||||
| 160 | 0 | ▶ | $self->{cfg} = (UNIVERSAL::isa($cfg, "Config::Simple")) ? $cfg : Config::Simple->new($cfg); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 161 | |||||||||||||
| 162 | 0 | ▶ | if ($self->{cfg}->param("flickr.api_handler") !~ /^(?:XPath|LibXML)$/) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 163 | 0 | warn "Invalid API handler"; | |||||||||||
| 164 | 0 | return 0; | |||||||||||
| 165 | } | ||||||||||||
| 166 | |||||||||||||
| 167 | # | ||||||||||||
| 168 | |||||||||||||
| 169 | my $log_fmt = sub { | ||||||||||||
| 170 | 0 | ▶ | my %args = @_; | ||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||
| 171 | |||||||||||||
| 172 | 0 | my $msg = $args{'message'}; | |||||||||||
| 173 | 0 | chomp $msg; | |||||||||||
| 174 | |||||||||||||
| 175 | 0 | ▶ | if ($args{'level'} eq "error") { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 176 | |||||||||||||
| 177 | 0 | my ($ln, $sub) = (caller(4))[2,3]; | |||||||||||
| 178 | 0 | $sub =~ s/.*:://; | |||||||||||
| 179 | |||||||||||||
| 180 | return sprintf("[%s][%s, ln%d] %s\n", | ||||||||||||
| 181 | 0 | $args{'level'}, $sub, $ln, $msg); | |||||||||||
| 182 | } | ||||||||||||
| 183 | |||||||||||||
| 184 | 0 | return sprintf("[%s] %s\n", $args{'level'}, $msg); | |||||||||||
| 185 | 0 | }; | |||||||||||
| 186 | |||||||||||||
| 187 | 0 | my $logger = Log::Dispatch->new(callbacks=>$log_fmt); | |||||||||||
| 188 | 0 | my $error = Log::Dispatch::Screen->new(name => '__error', | |||||||||||
| 189 | min_level => 'error', | ||||||||||||
| 190 | stderr => 1); | ||||||||||||
| 191 | |||||||||||||
| 192 | 0 | $logger->add($error); | |||||||||||
| 193 | |||||||||||||
| 194 | # | ||||||||||||
| 195 | # Custom report logging | ||||||||||||
| 196 | # | ||||||||||||
| 197 | |||||||||||||
| 198 | 0 | ▶ | if ($self->{cfg}->param("reporting.enable")) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 199 | |||||||||||||
| 200 | 0 | ▶ | my $report_handler = $self->{cfg}->param("reporting.handler") || "Screen"; | ||||||||||
|
Condition0%0/2
$self->{'cfg'}->param('reporting.handler') || 'Screen'
MC/DC0%0/1
$self->{'cfg'}->param('reporting.handler') || 'Screen'
$self->{'cfg'}->param('reporting.handler')
Truth table0%0/2
A
A= $self->{'cfg'}->param('reporting.handler')
| |||||||||||||
| 201 | 0 | $report_handler =~ s/:://g; | |||||||||||
| 202 | |||||||||||||
| 203 | 0 | my $report_pkg = "Log::Dispatch::$report_handler"; | |||||||||||
| 204 | 0 | eval "require $report_pkg"; | |||||||||||
| 205 | |||||||||||||
| 206 | 0 | ▶ | if ($@) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 207 | 0 | warn "Failed to load $report_pkg, $@"; | |||||||||||
| 208 | 0 | return 0; | |||||||||||
| 209 | } | ||||||||||||
| 210 | |||||||||||||
| 211 | 0 | my %report_args = (); | |||||||||||
| 212 | |||||||||||||
| 213 | 0 | ▶ | if (my $args = $self->{cfg}->param("reporting.handler_args")) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 214 | |||||||||||||
| 215 | 0 | foreach my $part (split(",", $args)) { | |||||||||||
| 216 | 0 | my ($key, $value) = split(":", $part); | |||||||||||
| 217 | 0 | $report_args{$key} = $value; | |||||||||||
| 218 | } | ||||||||||||
| 219 | } | ||||||||||||
| 220 | |||||||||||||
| 221 | 0 | ▶ | $report_args{'name'} ||= "__report"; | ||||||||||
|
Condition0%0/2
$report_args{"name"} ||= "__report"
MC/DC0%0/1
$report_args{"name"} ||= "__report"
$report_args{"name"}
Truth table0%0/2
A
A= $report_args{"name"}
| |||||||||||||
| 222 | 0 | ▶ | $report_args{'min_level'} ||= "info"; | ||||||||||
|
Condition0%0/2
$report_args{"min_level"} ||= "info"
MC/DC0%0/1
$report_args{"min_level"} ||= "info"
$report_args{"min_level"}
Truth table0%0/2
A
A= $report_args{"min_level"}
| |||||||||||||
| 223 | |||||||||||||
| 224 | 0 | my $reporter = $report_pkg->new(%report_args); | |||||||||||
| 225 | |||||||||||||
| 226 | 0 | ▶ | if (! $reporter) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 227 | 0 | warn "Failed to instantiate $report_pkg, $!"; | |||||||||||
| 228 | 0 | return 0; | |||||||||||
| 229 | } | ||||||||||||
| 230 | |||||||||||||
| 231 | 0 | $logger->add($reporter); | |||||||||||
| 232 | } | ||||||||||||
| 233 | |||||||||||||
| 234 | 0 | $self->{'__logger'} = $logger; | |||||||||||
| 235 | |||||||||||||
| 236 | # | ||||||||||||
| 237 | |||||||||||||
| 238 | $self->{api} = Flickr::API->new({key => $self->{cfg}->param("flickr.api_key"), | ||||||||||||
| 239 | secret => $self->{cfg}->param("flickr.api_secret"), | ||||||||||||
| 240 | 0 | handler => $self->{cfg}->param("flickr.api_handler")}); | |||||||||||
| 241 | |||||||||||||
| 242 | 0 | my $pkg = ref($self); | |||||||||||
| 243 | 0 | my $version = undef; | |||||||||||
| 244 | |||||||||||||
| 245 | 0 | do { | |||||||||||
| 246 | 0 | my $ref = join("::", $pkg, "VERSION"); | |||||||||||
| 247 | |||||||||||||
| 248 | 1 | ▶ | no strict "refs"; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 249 | 0 | $version = ${$ref}; | |||||||||||
| 250 | }; | ||||||||||||
| 251 | |||||||||||||
| 252 | 0 | my $agent_string = sprintf("%s/%s", $pkg, $version); | |||||||||||
| 253 | |||||||||||||
| 254 | 0 | $self->{api}->agent($agent_string); | |||||||||||
| 255 | 0 | return 1; | |||||||||||
| 256 | } | ||||||||||||
| 257 | |||||||||||||
| 258 | =head1 OBJECT METHODS | ||||||||||||
| 259 | |||||||||||||
| 260 | =cut | ||||||||||||
| 261 | |||||||||||||
| 262 | =head2 $obj->api_call(\%args) | ||||||||||||
| 263 | |||||||||||||
| 264 | Valid args are : | ||||||||||||
| 265 | |||||||||||||
| 266 | =over 4 | ||||||||||||
| 267 | |||||||||||||
| 268 | =item * B<method> | ||||||||||||
| 269 | |||||||||||||
| 270 | A string containing the name of the Flickr API method you are | ||||||||||||
| 271 | calling. | ||||||||||||
| 272 | |||||||||||||
| 273 | =item * B<args> | ||||||||||||
| 274 | |||||||||||||
| 275 | A hash ref containing the key value pairs you are passing to | ||||||||||||
| 276 | I<method> | ||||||||||||
| 277 | |||||||||||||
| 278 | =back | ||||||||||||
| 279 | |||||||||||||
| 280 | If the method encounters any errors calling the API, receives an API error | ||||||||||||
| 281 | or can not parse the response it will log an error event, via the B<log> method, | ||||||||||||
| 282 | and return undef. | ||||||||||||
| 283 | |||||||||||||
| 284 | Otherwise it will return a I<XML::LibXML::Document> object (if XML::LibXML is | ||||||||||||
| 285 | installed) or a I<XML::XPath> object. | ||||||||||||
| 286 | |||||||||||||
| 287 | =cut | ||||||||||||
| 288 | |||||||||||||
| 289 | sub api_call { | ||||||||||||
| 290 | 0 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
api_call: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||
| 291 | 0 | my $args = shift; | |||||||||||
| 292 | |||||||||||||
| 293 | # | ||||||||||||
| 294 | |||||||||||||
| 295 | # check to see if we need to take | ||||||||||||
| 296 | # breather (are we pounding or are | ||||||||||||
| 297 | # we not?) | ||||||||||||
| 298 | |||||||||||||
| 299 | 0 | ▶ | while (time < $self->{'__wait'}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 300 | |||||||||||||
| 301 | 0 | my $debug_msg = sprintf("trying not to beat up the Flickr servers, pause for %.2f seconds\n", | |||||||||||
| 302 | $PAUSE_SECONDS_OK); | ||||||||||||
| 303 | |||||||||||||
| 304 | 0 | $self->log()->debug($debug_msg); | |||||||||||
| 305 | 0 | sleep($PAUSE_SECONDS_OK); | |||||||||||
| 306 | } | ||||||||||||
| 307 | |||||||||||||
| 308 | # send request | ||||||||||||
| 309 | |||||||||||||
| 310 | 0 | ▶ | if (exists($args->{'args'}->{'api_sig'})) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 311 | 0 | delete $args->{'args'}->{'api_sig'}; | |||||||||||
| 312 | } | ||||||||||||
| 313 | |||||||||||||
| 314 | 0 | $args->{'args'}->{'auth_token'} = $self->{cfg}->param("flickr.auth_token"); | |||||||||||
| 315 | |||||||||||||
| 316 | # | ||||||||||||
| 317 | |||||||||||||
| 318 | 0 | my $req = Flickr::API::Request->new($args); | |||||||||||
| 319 | 0 | my $res = undef; | |||||||||||
| 320 | |||||||||||||
| 321 | 0 | $self->log()->debug("calling $args->{method} : " . Dumper($args->{args})); | |||||||||||
| 322 | |||||||||||||
| 323 | 0 | eval { | |||||||||||
| 324 | 0 | $res = $self->{'api'}->execute_request($req); | |||||||||||
| 325 | }; | ||||||||||||
| 326 | |||||||||||||
| 327 | 0 | ▶ | if ($@) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 328 | 0 | $self->log()->error("Fatal error calling the Flickr API, $@"); | |||||||||||
| 329 | |||||||||||||
| 330 | 0 | $self->{'__wait'} = time + $PAUSE_SECONDS_OK; | |||||||||||
| 331 | 0 | $self->{'__paused'} = 0; | |||||||||||
| 332 | 0 | return undef; | |||||||||||
| 333 | } | ||||||||||||
| 334 | |||||||||||||
| 335 | # | ||||||||||||
| 336 | # check for 503 status | ||||||||||||
| 337 | # | ||||||||||||
| 338 | |||||||||||||
| 339 | 0 | ▶ | if ($res->code() eq $PAUSE_ONSTATUS) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 340 | 0 | $res = $self->retry_api_call($args, $res); | |||||||||||
| 341 | } | ||||||||||||
| 342 | |||||||||||||
| 343 | 0 | $self->{'__wait'} = time + $PAUSE_SECONDS_OK; | |||||||||||
| 344 | 0 | $self->{'__paused'} = 0; | |||||||||||
| 345 | |||||||||||||
| 346 | 0 | return $self->parse_api_call($args, $res); | |||||||||||
| 347 | } | ||||||||||||
| 348 | |||||||||||||
| 349 | =head2 $obj->get_auth() | ||||||||||||
| 350 | |||||||||||||
| 351 | Return an XML I<node> element containing the Flickr auth token information for | ||||||||||||
| 352 | the current object. | ||||||||||||
| 353 | |||||||||||||
| 354 | Returns undef if no token information is present. | ||||||||||||
| 355 | |||||||||||||
| 356 | =cut | ||||||||||||
| 357 | |||||||||||||
| 358 | sub get_auth { | ||||||||||||
| 359 | 0 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
get_auth: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||
| 360 | |||||||||||||
| 361 | 0 | ▶ | if (! $self->{'__auth'}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 362 | 0 | my $auth = $self->api_call({"method" => "flickr.auth.checkToken"}); | |||||||||||
| 363 | |||||||||||||
| 364 | 0 | ▶ | if (! $auth) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 365 | 0 | return undef; | |||||||||||
| 366 | } | ||||||||||||
| 367 | |||||||||||||
| 368 | 0 | my $nsid = $auth->find("/rsp/auth/user/\@nsid")->string_value(); | |||||||||||
| 369 | |||||||||||||
| 370 | 0 | ▶ | if (! $nsid) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 371 | 0 | $self->log()->error("unabled to determine ID for token"); | |||||||||||
| 372 | 0 | return undef; | |||||||||||
| 373 | } | ||||||||||||
| 374 | |||||||||||||
| 375 | 0 | $self->{'__auth'} = $auth; | |||||||||||
| 376 | } | ||||||||||||
| 377 | |||||||||||||
| 378 | 0 | return $self->{'__auth'}; | |||||||||||
| 379 | } | ||||||||||||
| 380 | |||||||||||||
| 381 | =head2 $obj->get_auth_nsid() | ||||||||||||
| 382 | |||||||||||||
| 383 | Return the Flickr NSID of the user associated with the Flickr auth token information | ||||||||||||
| 384 | for the current object. | ||||||||||||
| 385 | |||||||||||||
| 386 | Returns undef if no token information is present. | ||||||||||||
| 387 | |||||||||||||
| 388 | =cut | ||||||||||||
| 389 | |||||||||||||
| 390 | sub get_auth_nsid { | ||||||||||||
| 391 | 0 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
get_auth_nsid: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 392 | |||||||||||||
| 393 | 0 | ▶ | if (my $auth = $self->get_auth()){ | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 394 | 0 | return $auth->find("/rsp/auth/user/\@nsid")->string_value(); | |||||||||||
| 395 | } | ||||||||||||
| 396 | |||||||||||||
| 397 | 0 | return undef; | |||||||||||
| 398 | } | ||||||||||||
| 399 | |||||||||||||
| 400 | sub parse_api_call { | ||||||||||||
| 401 | 0 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
parse_api_call: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||
| 402 | 0 | my $args = shift; | |||||||||||
| 403 | 0 | my $res = shift; | |||||||||||
| 404 | |||||||||||||
| 405 | 0 | $self->log()->debug($res->decoded_content()); | |||||||||||
| 406 | |||||||||||||
| 407 | 0 | my $xml = $self->_parse_results_xml($res); | |||||||||||
| 408 | |||||||||||||
| 409 | 0 | ▶ | if (! $xml) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 410 | 0 | $self->log()->error("failed to parse API response, calling $args->{method}"); | |||||||||||
| 411 | 0 | $self->log()->error($res->decoded_content()); | |||||||||||
| 412 | 0 | return undef; | |||||||||||
| 413 | } | ||||||||||||
| 414 | |||||||||||||
| 415 | 0 | my $stat = $xml->find("/rsp/\@stat")->string_value(); | |||||||||||
| 416 | |||||||||||||
| 417 | 0 | ▶ | if ($stat eq "fail") { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 418 | 0 | my $code = $xml->findvalue("/rsp/err/\@code"); | |||||||||||
| 419 | 0 | my $msg = $xml->findvalue("/rsp/err/\@msg"); | |||||||||||
| 420 | |||||||||||||
| 421 | 0 | $self->log()->error(sprintf("[%s] %s (calling $args->{method})\n", | |||||||||||
| 422 | $code, | ||||||||||||
| 423 | $msg)); | ||||||||||||
| 424 | |||||||||||||
| 425 | 0 | ▶ | if ($code==0) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 426 | 0 | $self->log()->info(sprintf("api disabled attempting %s/%s tries to see if it's come back up", $self->{'__retries'}, $RETRY_MAXTRIES)); | |||||||||||
| 427 | 0 | return $self->api_disabled($args, $res); | |||||||||||
| 428 | } | ||||||||||||
| 429 | } | ||||||||||||
| 430 | |||||||||||||
| 431 | 0 | $self->{'__retries'} = 0; | |||||||||||
| 432 | |||||||||||||
| 433 | 0 | ▶ | return ($@) ? undef : $xml; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 434 | } | ||||||||||||
| 435 | |||||||||||||
| 436 | sub _parse_results_xml { | ||||||||||||
| 437 | 0 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
_parse_results_xml: not called CC=5 SCAR=34.0
| |||||||||||||
| 438 | 0 | my $res = shift; | |||||||||||
| 439 | |||||||||||||
| 440 | 0 | my $xml = undef; | |||||||||||
| 441 | |||||||||||||
| 442 | # | ||||||||||||
| 443 | # Please for Cal to someday accept the patch to add | ||||||||||||
| 444 | # response handlers to Flickr::API... | ||||||||||||
| 445 | # | ||||||||||||
| 446 | |||||||||||||
| 447 | 0 | ▶ | if ($self->{cfg}->param("flickr.api_handler") eq "XPath") { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 448 | 0 | eval "require XML::XPath"; | |||||||||||
| 449 | |||||||||||||
| 450 | 0 | ▶ | if (! $@) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 451 | 0 | eval { | |||||||||||
| 452 | 0 | $xml = XML::XPath->new(xml=>$res->decoded_content()); | |||||||||||
| 453 | }; | ||||||||||||
| 454 | } | ||||||||||||
| 455 | } | ||||||||||||
| 456 | |||||||||||||
| 457 | else { | ||||||||||||
| 458 | 0 | eval "require XML::LibXML"; | |||||||||||
| 459 | |||||||||||||
| 460 | 0 | ▶ | if (! $@) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 461 | 0 | eval { | |||||||||||
| 462 | 0 | my $parser = XML::LibXML->new(); | |||||||||||
| 463 | 0 | $xml = $parser->parse_string($res->decoded_content()); | |||||||||||
| 464 | }; | ||||||||||||
| 465 | } | ||||||||||||
| 466 | } | ||||||||||||
| 467 | |||||||||||||
| 468 | # | ||||||||||||
| 469 | |||||||||||||
| 470 | 0 | ▶ | if (! $xml) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 471 | 0 | $self->log()->error("XML parse error : $@"); | |||||||||||
| 472 | 0 | return undef; | |||||||||||
| 473 | } | ||||||||||||
| 474 | |||||||||||||
| 475 | # | ||||||||||||
| 476 | |||||||||||||
| 477 | 0 | return $xml; | |||||||||||
| 478 | } | ||||||||||||
| 479 | |||||||||||||
| 480 | sub api_disabled { | ||||||||||||
| 481 | 0 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
api_disabled: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||
| 482 | 0 | my $args = shift; | |||||||||||
| 483 | 0 | my $res = shift; | |||||||||||
| 484 | |||||||||||||
| 485 | 0 | $self->{'__retries'} ++; | |||||||||||
| 486 | |||||||||||||
| 487 | 0 | ▶ | if ($self->{'__retries'} > $RETRY_MAXTRIES) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 488 | 0 | $self->log()->critical(sprintf("API still down after %s tries - exiting", $RETRY_MAXTRIES)); | |||||||||||
| 489 | 0 | exit; | |||||||||||
| 490 | } | ||||||||||||
| 491 | |||||||||||||
| 492 | 0 | $res = $self->retry_api_call($args, $res); | |||||||||||
| 493 | |||||||||||||
| 494 | 0 | ▶ | if (! $res) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 495 | 0 | $self->log()->critical("Returned false during 'api disabled' retry. That can only be bad - exiting"); | |||||||||||
| 496 | 0 | exit; | |||||||||||
| 497 | } | ||||||||||||
| 498 | |||||||||||||
| 499 | 0 | return $res; | |||||||||||
| 500 | } | ||||||||||||
| 501 | |||||||||||||
| 502 | sub retry_api_call { | ||||||||||||
| 503 | 0 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
retry_api_call: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||
| 504 | 0 | my $args = shift; | |||||||||||
| 505 | 0 | my $res = shift; | |||||||||||
| 506 | |||||||||||||
| 507 | # you are in a dark and twisty corridor | ||||||||||||
| 508 | # where all the errors look the same - | ||||||||||||
| 509 | # just give up if we hit this ceiling | ||||||||||||
| 510 | |||||||||||||
| 511 | 0 | $self->{'__paused'} ++; | |||||||||||
| 512 | |||||||||||||
| 513 | 0 | ▶ | if ($self->{'__paused'} > $PAUSE_MAXTRIES) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 514 | |||||||||||||
| 515 | 0 | my $errmsg = sprintf("service returned '%d' status %d times; exiting", | |||||||||||
| 516 | $PAUSE_ONSTATUS, $PAUSE_MAXTRIES); | ||||||||||||
| 517 | |||||||||||||
| 518 | 0 | $self->log()->error($errmsg); | |||||||||||
| 519 | 0 | return undef; | |||||||||||
| 520 | } | ||||||||||||
| 521 | |||||||||||||
| 522 | 0 | my $retry_after = $res->header("Retry-After"); | |||||||||||
| 523 | 0 | my $debug_msg = undef; | |||||||||||
| 524 | |||||||||||||
| 525 | 0 | ▶ | if ($retry_after ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 526 | 0 | $debug_msg = sprintf("service unavailable, requested to retry in %d seconds", | |||||||||||
| 527 | $retry_after); | ||||||||||||
| 528 | } | ||||||||||||
| 529 | |||||||||||||
| 530 | else { | ||||||||||||
| 531 | 0 | $retry_after = $PAUSE_SECONDS_UNAVAILABLE * $self->{'__paused'}; | |||||||||||
| 532 | 0 | $debug_msg = sprintf("service unavailable, pause for %.2f seconds", | |||||||||||
| 533 | $retry_after); | ||||||||||||
| 534 | } | ||||||||||||
| 535 | |||||||||||||
| 536 | 0 | $self->log()->debug($debug_msg); | |||||||||||
| 537 | 0 | sleep($retry_after); | |||||||||||
| 538 | |||||||||||||
| 539 | # try, try again | ||||||||||||
| 540 | |||||||||||||
| 541 | 0 | return $self->api_call($args); | |||||||||||
| 542 | } | ||||||||||||
| 543 | |||||||||||||
| 544 | =head2 $obj->upload(\%args) | ||||||||||||
| 545 | |||||||||||||
| 546 | This is a helper method that simply wraps calls to the I<Flickr::Upload> upload | ||||||||||||
| 547 | method. All the arguments are the same. For complete documentation please consult: | ||||||||||||
| 548 | |||||||||||||
| 549 | L<http://search.cpan.org/dist/Flickr-Upload/Upload.pm#upload> | ||||||||||||
| 550 | |||||||||||||
| 551 | (Note: There's no need to pass an auth_token argument as the wrapper will take care | ||||||||||||
| 552 | of for you.) | ||||||||||||
| 553 | |||||||||||||
| 554 | Returns a photo ID (or a ticket ID if the call is asynchronous) on success or false | ||||||||||||
| 555 | if there was a problem. | ||||||||||||
| 556 | |||||||||||||
| 557 | =cut | ||||||||||||
| 558 | |||||||||||||
| 559 | sub upload { | ||||||||||||
| 560 | 0 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
upload: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 561 | 0 | my $args = shift; | |||||||||||
| 562 | |||||||||||||
| 563 | 0 | $args->{'auth_token'} = $self->{cfg}->param("flickr.auth_token"); | |||||||||||
| 564 | |||||||||||||
| 565 | my $ua = Flickr::Upload->new({'key' => $self->{cfg}->param("flickr.api_key"), | ||||||||||||
| 566 | 0 | 'secret' => $self->{cfg}->param("flickr.api_secret")}); | |||||||||||
| 567 | |||||||||||||
| 568 | 0 | my $id = undef; | |||||||||||
| 569 | |||||||||||||
| 570 | 0 | eval { | |||||||||||
| 571 | 0 | $id = $ua->upload(%$args); | |||||||||||
| 572 | }; | ||||||||||||
| 573 | |||||||||||||
| 574 | 0 | ▶ | if ($@){ | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 575 | 0 | $self->log()->error("upload failed: $@"); | |||||||||||
| 576 | 0 | return 0; | |||||||||||
| 577 | } | ||||||||||||
| 578 | |||||||||||||
| 579 | 0 | return $id; | |||||||||||
| 580 | } | ||||||||||||
| 581 | |||||||||||||
| 582 | =head2 $obj->log() | ||||||||||||
| 583 | |||||||||||||
| 584 | Returns a I<Log::Dispatch> object. | ||||||||||||
| 585 | |||||||||||||
| 586 | =cut | ||||||||||||
| 587 | |||||||||||||
| 588 | sub log { | ||||||||||||
| 589 | 0 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
log: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 590 | 0 | return $self->{'__logger'}; | |||||||||||
| 591 | } | ||||||||||||
| 592 | |||||||||||||
| 593 | =head1 VERSION | ||||||||||||
| 594 | |||||||||||||
| 595 | 1.7 | ||||||||||||
| 596 | |||||||||||||
| 597 | =head1 DATE | ||||||||||||
| 598 | |||||||||||||
| 599 | $Date: 2009/08/02 17:16:12 $ | ||||||||||||
| 600 | |||||||||||||
| 601 | =head1 AUTHOR | ||||||||||||
| 602 | |||||||||||||
| 603 | Aaron Straup Cope E<lt>ascope@cpan.orgE<gt> | ||||||||||||
| 604 | |||||||||||||
| 605 | =head1 SEE ALSO | ||||||||||||
| 606 | |||||||||||||
| 607 | L<Config::Simple> | ||||||||||||
| 608 | |||||||||||||
| 609 | L<Flickr::API> | ||||||||||||
| 610 | |||||||||||||
| 611 | L<XML::XPath> | ||||||||||||
| 612 | |||||||||||||
| 613 | L<XML::LibXML> | ||||||||||||
| 614 | |||||||||||||
| 615 | =head1 BUGS | ||||||||||||
| 616 | |||||||||||||
| 617 | Please report all bugs via http://rt.cpan.org/ | ||||||||||||
| 618 | |||||||||||||
| 619 | =head1 LICENSE | ||||||||||||
| 620 | |||||||||||||
| 621 | Copyright (c) 2005-2008 Aaron Straup Cope. All Rights Reserved. | ||||||||||||
| 622 | |||||||||||||
| 623 | This is free software. You may redistribute it and/or | ||||||||||||
| 624 | modify it under the same terms as Perl itself. | ||||||||||||
| 625 | |||||||||||||
| 626 | =cut | ||||||||||||
| 627 | |||||||||||||
| 628 | 1 | return 1; | |||||||||||
| 629 | |||||||||||||
| 630 | __END__ | ||||||||||||