NAME
AnyEvent::Net::Curl::Queued::Easy - Net::Curl::Easy wrapped by Moo
VERSION
version 0.047
SYNOPSIS
package MyIEDownloader;
use strict;
use utf8;
use warnings qw(all);
use Moo;
use Net::Curl::Easy qw(/^CURLOPT_/);
extends 'AnyEvent::Net::Curl::Queued::Easy';
after init => sub {
my ($self) = @_;
$self->setopt(CURLOPT_ENCODING, '');
$self->setopt(CURLOPT_FOLLOWLOCATION, 1);
$self->setopt(CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)');
$self->setopt(CURLOPT_VERBOSE, 1);
};
after finish => sub {
my ($self, $result) = @_;
if ($self->has_error) {
printf "error downloading %s: %s\n", $self->final_url, $result;
} else {
printf "finished downloading %s: %d bytes\n", $self->final_url, length ${$self->data};
}
};
around has_error => sub {
my $orig = shift;
my $self = shift;
return 1 if $self->$orig(@_);
return 1 if $self->getinfo(Net::Curl::Easy::CURLINFO_RESPONSE_CODE) =~ m{^5[0-9]{2}$};
};
1;
WARNING: GONE MOO!
This module isn't using Any::Moose anymore due to the announced deprecation status of that module. The switch to the Moo is known to break modules that do extend 'AnyEvent::Net::Curl::Queued::Easy'
/ extend 'YADA::Worker'
! To keep the compatibility, make sure that you are using MooseX::NonMoose:
package YourSubclassingModule;
use Moose;
use MooseX::NonMoose;
extends 'AnyEvent::Net::Curl::Queued::Easy';
...
Or MouseX::NonMoose:
package YourSubclassingModule;
use Mouse;
use MouseX::NonMoose;
extends 'AnyEvent::Net::Curl::Queued::Easy';
...
Or the Any::Moose equivalent:
package YourSubclassingModule;
use Any::Moose;
use Any::Moose qw(X::NonMoose);
extends 'AnyEvent::Net::Curl::Queued::Easy';
...
However, the recommended approach is to switch your subclassing module to Moo altogether (you can use MooX::late to smoothen the transition):
package YourSubclassingModule;
use Moo;
use MooX::late;
extends 'AnyEvent::Net::Curl::Queued::Easy';
...
DESCRIPTION
The class you should overload to fetch stuff your own way.
ATTRIBUTES
curl_result
libcurl return code (Net::Curl::Easy::Code
).
data
Receive buffer.
force
Force request processing, despite the uniqueness signature.
header
Header buffer.
http_response
Encapsulate the response with HTTP::Response (only when the scheme is HTTP/HTTPS). Default: disabled.
post_content
Cache POST content to perform retries.
initial_url
URL to fetch (string).
final_url
Final URL (after redirections).
opts
HashRef
to be passed to setopt()
during initialization (inside init()
, before on_init()
callback).
queue
AnyEvent::Net::Curl::Queued circular reference.
sha
Uniqueness detection helper. Setup via sign
and access through unique
.
response
Encapsulated HTTP::Response instance, if "http_response" was set.
res
Deprecated alias for "response".
retry
Number of retries (default: 10).
stats
AnyEvent::Net::Curl::Queued::Stats instance.
use_stats
Set to true to enable stats computation. Note that extracting libcurl
time/size data degrades performance slightly.
on_init
Callback you can define instead of extending the init
method. Almost the same as after init => sub { ... }
on_finish
Callback you can define instead of extending the finish
method. Almost the same as after finish => sub { ... }
METHODS
unique()
Returns the unique signature of the request. By default, the signature is derived from Digest::SHA of the initial_url
.
sign($str)
Use $str
to compute the unique
value. Useful to successfully enqueue POST parameters.
init()
Initialize the instance. We can't use the default BUILD
method as we need the initialization to be done after the instance is in the queue.
You are supposed to build your own stuff after/around/before this method using method modifiers.
has_error()
Error handling: if has_error
returns true, the request is re-enqueued (until the retries number is exhausted).
You are supposed to build your own stuff after/around/before this method using method modifiers. For example, to retry on server error (HTTP 5xx response code):
around has_error => sub {
my $orig = shift;
my $self = shift;
return 1 if $self->$orig(@_);
return 1 if $self->getinfo('response_code') =~ m{^5[0-9]{2}$};
};
finish($result)
Called when the download is finished. $result
holds the Net::Curl::Easy::Code
.
You are supposed to build your own stuff after/around/before this method using method modifiers.
clone()
Clones the instance, for re-enqueuing purposes.
You are supposed to build your own stuff after/around/before this method using method modifiers.
setopt(OPTION => VALUE [, OPTION => VALUE])
Extends Net::Curl::Easy setopt()
, allowing option lists:
$self->setopt(
CURLOPT_ENCODING, '',
CURLOPT_FOLLOWLOCATION, 1,
CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',
CURLOPT_VERBOSE, 1,
);
Or even shorter:
$self->setopt(
encoding => '',
followlocation => 1,
useragent => 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)',
verbose => 1,
);
Complete list of options: http://curl.haxx.se/libcurl/c/curl_easy_setopt.html
If CURLOPT_POSTFIELDS
is a HashRef
or looks like a valid JSON (validates via JSON), it is encoded as UTF-8 and Content-Type: application/json; charset=utf-8
header is set automatically.
getinfo(VAR_NAME [, VAR_NAME])
Extends Net::Curl::Easy getinfo()
so it is able to get several variables at once; HashRef
parameter under void context will fill respective values in the HashRef
:
my $x = {
content_type => 0,
speed_download => 0,
primary_ip => 0,
};
$self->getinfo($x);
HashRef
parameter will return another HashRef
:
my $x = $self->getinfo({
content_type => 0,
speed_download => 0,
primary_ip => 0,
});
ArrayRef
parameter will return a list:
my ($content_type, $speed_download, $primary_ip) =
$self->getinfo([qw(content_type speed_download primary_ip)]);
Complete list of options: http://curl.haxx.se/libcurl/c/curl_easy_getinfo.html
FUNCTIONS
FOREIGNBUILDARGS
Internal. Required for Moo to operate properly on new
parameters.
SEE ALSO
AUTHOR
Stanislaw Pusep <stas@sysd.org>
COPYRIGHT AND LICENSE
This software is copyright (c) 2014 by Stanislaw Pusep.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.