File Coverage

blib/lib/Ambassador/API/V2.pm
Criterion Covered Total %
statement 17 39 43.5
branch 0 4 0.0
condition n/a
subroutine 6 11 54.5
pod 2 2 100.0
total 25 56 44.6


line stmt bran cond sub pod time code
1             package Ambassador::API::V2;
2              
3 1     1   100914 use Moo;
  1         8242  
  1         6  
4 1     1   1032 use v5.10;
  1         3  
5              
6             with 'Ambassador::API::V2::Role::HasJSON';
7 1     1   596 use HTTP::Tiny;
  1         34382  
  1         34  
8 1     1   559 use URI;
  1         4752  
  1         24  
9 1     1   368 use Ambassador::API::V2::Error;
  1         3  
  1         30  
10 1     1   421 use Ambassador::API::V2::Result;
  1         2  
  1         402  
11              
12             our $VERSION = '0.001';
13              
14             has username => (
15             is => 'ro',
16             required => 1
17             );
18              
19             has key => (
20             is => 'ro',
21             required => 1
22             );
23              
24             has url => (
25             is => 'ro',
26             coerce => sub {
27             return URI->new($_[0]);
28             },
29             default => sub { 'https://getambassador.com/api/v2/' }
30             );
31              
32             # Configure and cache the HTTP::Tiny object
33             has http => (
34             is => 'ro',
35             default => sub {
36             return HTTP::Tiny->new(
37             agent => "Ambassador-API-V2/$VERSION",
38             default_headers => {'Content-Type' => 'application/json'}
39             );
40             }
41             );
42              
43             sub _make_url {
44 0     0     my $self = shift;
45 0           my $method = shift;
46              
47 0           my $url = $self->url->clone;
48              
49             # Ambassador is very sensitive to double slashes.
50 0           my $path = $url->path . join '/', $self->username, $self->key, 'json', $method;
51 0           $path =~ s{/{2,}}{/}g;
52              
53 0           $url->path($path);
54              
55 0           return $url;
56             }
57              
58             sub _handle_response {
59 0     0     my $self = shift;
60 0           my ($response) = @_;
61              
62 0 0         die Ambassador::API::V2::Error->new_from_response($response) if !$response->{success};
63              
64 0           return Ambassador::API::V2::Result->new_from_response($response);
65             }
66              
67             sub _request {
68 0     0     my $self = shift;
69 0           my ($type, $method, $args) = @_;
70              
71 0           my $url = $self->_make_url($method);
72              
73 0           my $opts = {};
74 0 0         $opts->{content} = $self->json->encode($args) if $args;
75 0           my $response = $self->http->request(uc $type, $url, $opts);
76              
77 0           return $self->_handle_response($response);
78             }
79              
80             sub post {
81 0     0 1   my $self = shift;
82 0           return $self->_request('POST', @_);
83             }
84              
85             sub get {
86 0     0 1   my $self = shift;
87 0           return $self->_request('GET', @_);
88             }
89              
90             1;
91              
92             __END__