| 1 | package Etcd; | ||||||||||||||||||||||||||||
| 2 | 4 | $Etcd::VERSION = '0.002'; | |||||||||||||||||||||||||||
| 3 | # ABSTRACT: Client library for etcd | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use namespace::sweep; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use HTTP::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use URI::Escape qw(uri_escape); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 4 | ▶ | use Type::Utils qw(class_type); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 4 | ▶ | use Types::Standard qw(Str Int Bool HashRef); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 4 | has host => ( is => 'ro', isa => Str, default => '127.0.0.1' ); | |||||||||||||||||||||||||||
| 16 | 4 | has port => ( is => 'ro', isa => Int, default => 4001 ); | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 4 | has ssl => ( is => 'ro', isa => Bool, default => 0 ); | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 4 | has http => ( is => 'lazy', isa => class_type('HTTP::Tiny') ); | |||||||||||||||||||||||||||
| 21 | 6 | ▶ | sub _build_http { HTTP::Tiny->new }; | ||||||||||||||||||||||||||
|
Subroutine
_build_http: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 4 | has version_prefix => ( is => 'ro', isa => Str, default => '/v2' ); | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 4 | has _url_base => ( is => 'lazy' ); | |||||||||||||||||||||||||||
| 26 | sub _build__url_base { | ||||||||||||||||||||||||||||
| 27 | 6 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_build__url_base: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||
| 28 | 6 | ▶ | ($self->ssl ? 'https' : 'http') .'://'.$self->host.':'.$self->port; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | sub _prep_url { | ||||||||||||||||||||||||||||
| 32 | 6 | ▶ | my ($self, $path, %args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_prep_url: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 33 | 6 | my $trailing = $path =~ m{/$}; | |||||||||||||||||||||||||||
| 34 | 6 | my $url = $self->_url_base.join('/', map { uri_escape($_) } split('/', $path)); | |||||||||||||||||||||||||||
| 35 | 6 | ▶ | $url .= '/' if $trailing; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 6 | ▶ | $url .= '?'.$self->http->www_form_urlencode(\%args) if %args; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 6 | $url; | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | sub api_exec { | ||||||||||||||||||||||||||||
| 41 | 6 | ▶ | my ($self, $path, $method, %args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
api_exec: called CC=5 SCAR=19.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 42 | 6 | my $res = $self->http->request($method, $self->_prep_url($path, %args)); | |||||||||||||||||||||||||||
| 43 | $res = $self->http->request($method, $res->{headers}->{location}) | ||||||||||||||||||||||||||||
| 44 | 6 | ▶ | if $res && $res->{status} eq 307; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$res and $res->{"status"} eq 307
MC/DC50%1/2
$res and $res->{"status"} eq 307
$res
$res->{"status"} eq 307
Truth table66%2/3
A and B
A= $resB= $res->{"status"} eq 307
| |||||||||||||||||||||||||||||
| 45 | 6 | ▶ | return $res if $res->{success}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 0 | ▶ | croak "$res->{status} $res->{reason}: $res->{content}" if $res->{status} >= 500; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 0 | require Etcd::Error; | |||||||||||||||||||||||||||
| 48 | 0 | die Etcd::Error->new_from_http($res); | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | sub server_version { | ||||||||||||||||||||||||||||
| 52 | 1 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
server_version: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 53 | 1 | my $res = $self->api_exec('/version', 'GET'); | |||||||||||||||||||||||||||
| 54 | 1 | $res->{content}; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 4 | with 'Etcd::Keys'; | |||||||||||||||||||||||||||
| 58 | 4 | with 'Etcd::Stats'; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 4 | 1; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | __END__ | ||||||||||||||||||||||||||||