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