Graphite/HTTPClient.pm untested

filter: Statementstmt 0.0% 0 / 32 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 47 SCAR 17.9
CC
2
Cov
0%
_download
6.0
has_auth
2.0
BEGIN
2.0
CRAP 6.0
1 package Net::Async::Graphite::HTTPClient;
2  
3 our $VERSION = '0.0_1';
4  
5 =head1 NAME
6  
7 Net::Async::Graphite::HTTPClient - Handle HTTP for Net::Async::Graphite
8  
9 =head1 SYNOPSIS
10  
11 with 'Net::Async::Graphite::HTTPClient';
12  
13 =head1 DESCRIPTION
14  
15 Don't use this module directly, use L<Net::Async::Graphite> and create
16 objects using its C<new> method in the normal way. Those objects will
17 include the functionality documented here.
18  
19 This role brings the capacity to communicate with an HTTP server.
20  
21 It's mixed in by L<Net::Async::Graphite::API> and uses a
22 L<Net::Async::HTTP> object to do its work.
23  
24 =head1 BUGS
25  
26 C<_http> is added to L<IO::Async>'s loop but there's no mechanism to
27 remove it. For now C<_http> exists until the application exits but if
28 that ever changes this will be the source of memory leaks at best.
29  
30 =cut
31  
32 use v5.14;
33 use strictures 2;
34 use Moo::Role;
35 use Carp;
36  
37 use Net::Async::HTTP;
38 use namespace::clean;
39  
40 =head1 ROLE
41  
42 This role requires the L<IO::Async> functionality defined in
43 L<Net::Async::Graphite::Async> (essentially just a C<_loop> attribute
44 to hold the L<IO::Async::Loop> object).
45  
46 =cut
47  
48 with 'Net::Async::Graphite::Async';
49  
50 =head1 ATTRIBUTES
51  
52 =over
53  
54 =item max_connections (read-only)
55  
56 The maximum number of simultaneous HTTP connections to allow. See
57 C<max_connections_per_host> in L<Net::Async::HTTP>.
58  
59 Default: 0; no limit.
60  
61 =cut
62  
63 has max_connections => is => ro => default => 0;
64  
65 =item username (read-only, predicable)
66  
67 =item password (read-only, predicable)
68  
69 The username and password to authenticate with.
70  
71 In addition to the predicates C<has_username> and C<has_password>
72 there is C<has_auth> which returns true if there is a username I<and>
73 a password.
74  
75 =cut
76  
77 has [qw(username password)] => is => ro => predicate => 1;
78  
79 sub has_auth { $_[0]->has_username and $_[0]->has_password }
80  
81 =item _http (read-only)
82  
83 The L<Net::Async::HTTP> object which performs the HTTP queries.
84  
85 =cut
86  
87 has _http => is => lazy => init_arg => undef, builder => sub {
88 my $self = shift;
89 my $http = Net::Async::HTTP->new(
90 user_agent => blessed($self) .'/0.0',
91 max_connections_per_host => $self->max_connections,
92 );
93 $self->_loop->add($http);
94 $http;
95 };
96  
97 =back
98  
99 =head1 PRIVATE METHODS
100  
101 =over
102  
103 =item _download ($uri)
104  
105 Return (a L<Future> which completes with) an L<HTTP::Response> object
106 when the HTTP request has finished.
107  
108 =cut
109  
110 sub _download {
111 my $self = shift;
112 my ($uri) = @_;
113 #$self->_debugf("Downloading from uri <%s>.", $uri);
114 $self->_http->do_request(
115 uri => "$uri",
116 # timeout=> ?,
117 on_error => sub {
118 #$self->_errorf('Error conducting HTTP transaction to %s?%s: %s',
119 # $self->endpoint, $uri, $_[0]);
120 # Future has already been fail()ed elsewhere.
121 },
122 ($self->has_auth
123 ? (user => $self->username, pass => $self->password)
124 : ()));
125 }
126  
127 1;
128  
129 =back
130  
131 =head1 SEE ALSO
132  
133 L<Net::Async::Graphite>
134  
135 L<Net::Async::Graphite::Async>
136  
137 L<Net::Async::HTTP>
138  
139 L<Future>
140  
141 L<Moo>
142  
143 =head1 AUTHOR
144  
145 Matthew King <matthew.king@cloudbeds.com>
146  
147 =cut