File Coverage

blib/lib/WebService/Hooktheory.pm
Criterion Covered Total %
statement 43 48 89.5
branch 7 12 58.3
condition 3 9 33.3
subroutine 12 13 92.3
pod 1 2 50.0
total 66 84 78.5


line stmt bran cond sub pod time code
1             package WebService::Hooktheory;
2             our $AUTHORITY = 'cpan:GENE';
3              
4             # ABSTRACT: Access to the Hooktheory API
5              
6             our $VERSION = '0.0601';
7              
8 1     1   977 use strictures 2;
  1         1420  
  1         35  
9 1     1   184 use Carp qw(croak);
  1         2  
  1         42  
10 1     1   6 use Mojo::UserAgent ();
  1         1  
  1         15  
11 1     1   4 use Mojo::JSON qw(decode_json);
  1         2  
  1         35  
12 1     1   5 use Mojo::URL ();
  1         2  
  1         12  
13 1     1   442 use Moo;
  1         8971  
  1         4  
14 1     1   1736 use Try::Tiny;
  1         1166  
  1         46  
15 1     1   410 use namespace::clean;
  1         8020  
  1         6  
16              
17              
18             has username => (
19             is => 'ro',
20             );
21              
22              
23             has password => (
24             is => 'ro',
25             );
26              
27              
28             has activkey => (
29             is => 'ro',
30             );
31              
32              
33             has base => (
34             is => 'rw',
35             default => sub { 'https://api.hooktheory.com' },
36             );
37              
38              
39             has ua => (
40             is => 'rw',
41             default => sub { Mojo::UserAgent->new() },
42             );
43              
44              
45             sub BUILD {
46 2     2 0 48 my ( $self, $args ) = @_;
47              
48 2 0 66     18 if ( !$args->{activkey} && $args->{username} && $args->{password} ) {
      33        
49             my $tx = $self->ua->post(
50             $self->base . 'users/auth',
51             { 'Content-Type' => 'application/json' },
52             json => { username => $args->{username}, password => $args->{password} },
53 0         0 );
54              
55 0         0 my $data = _handle_response($tx);
56              
57             $self->{activkey} = $data->{activkey}
58 0 0 0     0 if $data && $data->{activkey};
59             }
60             }
61              
62              
63             sub fetch {
64 4     4 1 34338 my ( $self, %args ) = @_;
65              
66 4 100       43 croak 'No activkey provided' unless $self->activkey;
67 3 100       27 croak 'No endpoint provided' unless $args{endpoint};
68 2 100       15 croak 'No query provided' unless $args{query};
69              
70             my $url = Mojo::URL->new($self->base)
71             ->path('v1' . $args{endpoint})
72 1         13 ->query(%{ $args{query} });
  1         432  
73              
74 1         114 my $tx = $self->ua->get( $url, { Authorization => 'Bearer ' . $self->activkey } );
75              
76 1         22734 my $data = _handle_response($tx);
77              
78 1         10 return $data;
79             }
80              
81             sub _handle_response {
82 1     1   4 my ($tx) = @_;
83              
84 1         2 my $data;
85              
86 1         9 my $res = $tx->result;
87              
88 1 50       82 if ( $res->is_success ) {
89 1         25 my $body = $res->body;
90             try {
91 1     1   137 $data = decode_json($body);
92             }
93             catch {
94 0     0   0 croak $body, "\n";
95 1         62 };
96             }
97             else {
98 0         0 croak "Connection error: ", $res->message;
99             }
100              
101 1         140 return $data;
102             }
103              
104             1;
105              
106             __END__