| 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.0600'; | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 1 |  |  | 1 |  | 1325 | use Moo; | 
|  | 1 |  |  |  |  | 11151 |  | 
|  | 1 |  |  |  |  | 5 |  | 
| 9 | 1 |  |  | 1 |  | 2058 | use strictures 2; | 
|  | 1 |  |  |  |  | 1704 |  | 
|  | 1 |  |  |  |  | 40 |  | 
| 10 | 1 |  |  | 1 |  | 737 | use namespace::clean; | 
|  | 1 |  |  |  |  | 11976 |  | 
|  | 1 |  |  |  |  | 6 |  | 
| 11 |  |  |  |  |  |  |  | 
| 12 | 1 |  |  | 1 |  | 291 | use Carp; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 53 |  | 
| 13 | 1 |  |  | 1 |  | 7 | use Mojo::UserAgent; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 22 |  | 
| 14 | 1 |  |  | 1 |  | 85 | use Mojo::JSON qw( decode_json ); | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 45 |  | 
| 15 | 1 |  |  | 1 |  | 5 | use Mojo::URL; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 19 |  | 
| 16 | 1 |  |  | 1 |  | 47 | use Try::Tiny; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 654 |  | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | has username => ( | 
| 20 |  |  |  |  |  |  | is => 'ro', | 
| 21 |  |  |  |  |  |  | ); | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | has password => ( | 
| 25 |  |  |  |  |  |  | is => 'ro', | 
| 26 |  |  |  |  |  |  | ); | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | has activkey => ( | 
| 30 |  |  |  |  |  |  | is => 'ro', | 
| 31 |  |  |  |  |  |  | ); | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  |  | 
| 34 |  |  |  |  |  |  | has base => ( | 
| 35 |  |  |  |  |  |  | is      => 'rw', | 
| 36 |  |  |  |  |  |  | default => sub { 'https://api.hooktheory.com' }, | 
| 37 |  |  |  |  |  |  | ); | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  |  | 
| 40 |  |  |  |  |  |  | has ua => ( | 
| 41 |  |  |  |  |  |  | is      => 'rw', | 
| 42 |  |  |  |  |  |  | default => sub { Mojo::UserAgent->new() }, | 
| 43 |  |  |  |  |  |  | ); | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | sub BUILD { | 
| 47 | 2 |  |  | 2 | 0 | 49 | my ( $self, $args ) = @_; | 
| 48 |  |  |  |  |  |  |  | 
| 49 | 2 | 0 | 66 |  |  | 30 | if ( !$args->{activkey} && $args->{username} && $args->{password} ) { | 
|  |  |  | 33 |  |  |  |  | 
| 50 |  |  |  |  |  |  | my $tx = $self->ua->post( | 
| 51 |  |  |  |  |  |  | $self->base . 'users/auth', | 
| 52 |  |  |  |  |  |  | { 'Content-Type' => 'application/json' }, | 
| 53 |  |  |  |  |  |  | json => { username => $args->{username}, password => $args->{password} }, | 
| 54 | 0 |  |  |  |  | 0 | ); | 
| 55 |  |  |  |  |  |  |  | 
| 56 | 0 |  |  |  |  | 0 | my $data = _handle_response($tx); | 
| 57 |  |  |  |  |  |  |  | 
| 58 |  |  |  |  |  |  | $self->{activkey} = $data->{activkey} | 
| 59 | 0 | 0 | 0 |  |  | 0 | if $data && $data->{activkey}; | 
| 60 |  |  |  |  |  |  | } | 
| 61 |  |  |  |  |  |  | } | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  | sub fetch { | 
| 65 | 4 |  |  | 4 | 1 | 28991 | my ( $self, %args ) = @_; | 
| 66 |  |  |  |  |  |  |  | 
| 67 | 4 | 100 |  |  |  | 49 | croak 'No activkey provided' unless $self->activkey; | 
| 68 | 3 | 100 |  |  |  | 20 | croak 'No endpoint provided' unless $args{endpoint}; | 
| 69 | 2 | 100 |  |  |  | 15 | croak 'No query provided' unless $args{query}; | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | my $url = Mojo::URL->new($self->base) | 
| 72 |  |  |  |  |  |  | ->path('v1' . $args{endpoint}) | 
| 73 | 1 |  |  |  |  | 12 | ->query(%{ $args{query} }); | 
|  | 1 |  |  |  |  | 496 |  | 
| 74 |  |  |  |  |  |  |  | 
| 75 | 1 |  |  |  |  | 125 | my $tx = $self->ua->get( $url, { Authorization => 'Bearer ' . $self->activkey } ); | 
| 76 |  |  |  |  |  |  |  | 
| 77 | 1 |  |  |  |  | 24725 | my $data = _handle_response($tx); | 
| 78 |  |  |  |  |  |  |  | 
| 79 | 1 |  |  |  |  | 10 | return $data; | 
| 80 |  |  |  |  |  |  | } | 
| 81 |  |  |  |  |  |  |  | 
| 82 |  |  |  |  |  |  | sub _handle_response { | 
| 83 | 1 |  |  | 1 |  | 4 | my ($tx) = @_; | 
| 84 |  |  |  |  |  |  |  | 
| 85 | 1 |  |  |  |  | 3 | my $data; | 
| 86 |  |  |  |  |  |  |  | 
| 87 | 1 |  |  |  |  | 9 | my $res = $tx->result; | 
| 88 |  |  |  |  |  |  |  | 
| 89 | 1 | 50 |  |  |  | 29 | if ( $res->is_success ) { | 
| 90 | 1 |  |  |  |  | 21 | my $body = $res->body; | 
| 91 |  |  |  |  |  |  | try { | 
| 92 | 1 |  |  | 1 |  | 152 | $data = decode_json($body); | 
| 93 |  |  |  |  |  |  | } | 
| 94 |  |  |  |  |  |  | catch { | 
| 95 | 0 |  |  | 0 |  | 0 | croak $body, "\n"; | 
| 96 | 1 |  |  |  |  | 33 | }; | 
| 97 |  |  |  |  |  |  | } | 
| 98 |  |  |  |  |  |  | else { | 
| 99 | 0 |  |  |  |  | 0 | croak "Connection error: ", $res->message; | 
| 100 |  |  |  |  |  |  | } | 
| 101 |  |  |  |  |  |  |  | 
| 102 | 1 |  |  |  |  | 163 | return $data; | 
| 103 |  |  |  |  |  |  | } | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | 1; | 
| 106 |  |  |  |  |  |  |  | 
| 107 |  |  |  |  |  |  | __END__ |