File Coverage

blib/lib/Etcd.pm
Criterion Covered Total %
statement 38 41 92.6
branch 7 12 58.3
condition 2 3 66.6
subroutine 12 12 100.0
pod 1 2 50.0
total 60 70 85.7


line stmt bran cond sub pod time code
1             package Etcd;
2             $Etcd::VERSION = '0.002';
3             # ABSTRACT: Client library for etcd
4              
5 4     4   54597 use namespace::sweep;
  4         84296  
  4         23  
6              
7 4     4   2903 use HTTP::Tiny;
  4         149653  
  4         177  
8 4     4   1783 use URI::Escape qw(uri_escape);
  4         3780  
  4         221  
9 4     4   20 use Carp qw(croak);
  4         4  
  4         139  
10              
11 4     4   1976 use Moo;
  4         31371  
  4         24  
12 4     4   6794 use Type::Utils qw(class_type);
  4         93705  
  4         32  
13 4     4   3907 use Types::Standard qw(Str Int Bool HashRef);
  4         132308  
  4         65  
14              
15             has host => ( is => 'ro', isa => Str, default => '127.0.0.1' );
16             has port => ( is => 'ro', isa => Int, default => 4001 );
17              
18             has ssl => ( is => 'ro', isa => Bool, default => 0 );
19              
20             has http => ( is => 'lazy', isa => class_type('HTTP::Tiny') );
21 6     6   1871 sub _build_http { HTTP::Tiny->new };
22              
23             has version_prefix => ( is => 'ro', isa => Str, default => '/v2' );
24              
25             has _url_base => ( is => 'lazy' );
26             sub _build__url_base {
27 6     6   1313 my ($self) = @_;
28 6 50       89 ($self->ssl ? 'https' : 'http') .'://'.$self->host.':'.$self->port;
29             }
30              
31             sub _prep_url {
32 6     6   797 my ($self, $path, %args) = @_;
33 6         21 my $trailing = $path =~ m{/$};
34 6         68 my $url = $self->_url_base.join('/', map { uri_escape($_) } split('/', $path));
  22         224  
35 6 50       69 $url .= '/' if $trailing;
36 6 100       74 $url .= '?'.$self->http->www_form_urlencode(\%args) if %args;
37 6         173 $url;
38             }
39              
40             sub api_exec {
41 6     6 0 123 my ($self, $path, $method, %args) = @_;
42 6         65 my $res = $self->http->request($method, $self->_prep_url($path, %args));
43 6 100 66     104 $res = $self->http->request($method, $res->{headers}->{location})
44             if $res && $res->{status} eq 307;
45 6 50       65 return $res if $res->{success};
46 0 0       0 croak "$res->{status} $res->{reason}: $res->{content}" if $res->{status} >= 500;
47 0         0 require Etcd::Error;
48 0         0 die Etcd::Error->new_from_http($res);
49             }
50              
51             sub server_version {
52 1     1 1 4312 my ($self) = @_;
53 1         5 my $res = $self->api_exec('/version', 'GET');
54 1         17 $res->{content};
55             }
56              
57             with 'Etcd::Keys';
58             with 'Etcd::Stats';
59              
60             1;
61              
62             __END__