1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
use strict;
use warnings;
package MetaCPAN::Client::Role::HasUA;
# ABSTRACT: Role for supporting user-agent attribute
$MetaCPAN::Client::Role::HasUA::VERSION = '2.033000';
use Moo::Role;
use Carp;
use HTTP::Tiny;
has _user_ua => (
init_arg => 'ua',
is => 'ro',
predicate => '_has_user_ua',
);
has ua => (
init_arg => undef,
is => 'ro',
lazy => 1,
builder => '_build_ua',
);
has ua_args => (
is => 'ro',
default => sub {
[ agent => 'MetaCPAN::Client/'.($MetaCPAN::Client::VERSION||'xx'),
verify_SSL => 1 ]
},
);
sub _build_ua {
my $self = shift;
# This level of indirection is so that if a user has not specified a custom UA
# MetaCPAN::Client will have its own UA's
#
# But if the user **has** specified a custom UA, that UA is used for both.
if ( $self->_has_user_ua ) {
my $ua = $self->_user_ua;
croak "cannot use given ua (must support 'get' and 'post' methods)"
unless $ua->can("get") and $ua->can("post");
return $self->_user_ua;
}
return HTTP::Tiny->new( @{ $self->ua_args } );
}
1;
__END__
=pod
=encoding UTF-8
=head1 NAME
MetaCPAN::Client::Role::HasUA - Role for supporting user-agent attribute
=head1 VERSION
version 2.033000
=head1 ATTRIBUTES
=head2 ua
my $mcpan = MetaCPAN::Client->new( ua => HTTP::Tiny->new(...) );
The user agent object for running requests.
It must provide an interface that matches L<HTTP::Tiny>. Explicitly:
=over 4
=item * Implement post()
Method C<post> must be available that accepts a request URL and a hashref of
options.
=item * Implement get()
Method C<get> must be available that accepts a request URL.
=item * Return result hashref
Must return a result hashref which has key C<success> and key C<content>.
=back
Default: L<HTTP::Tiny>,
=head2 ua_args
my $mcpan = MetaCPAN::Client->new(
ua_args => [ agent => 'MyAgent' ],
);
Arguments sent to the user agent.
Default: user agent string: B<MetaCPAN::Client/$version>.
=head1 AUTHORS
=over 4
=item *
Sawyer X <xsawyerx@cpan.org>
=item *
Mickey Nasriachi <mickey@cpan.org>
=back
=head1 COPYRIGHT AND LICENSE
This software is copyright (c) 2016 by Sawyer X.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.
=cut
|