line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catmandu::RKD::API::Search; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
13
|
use Moo; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
17
|
|
4
|
3
|
|
|
3
|
|
666
|
use LWP::UserAgent; |
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
46
|
|
5
|
|
|
|
|
|
|
|
6
|
3
|
|
|
3
|
|
8
|
use Catmandu::Sane; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
13
|
|
7
|
|
|
|
|
|
|
|
8
|
3
|
|
|
3
|
|
1422
|
use Catmandu::RKD::API::Parse; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
use Catmandu::RKD::API::Extract; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
has url => (is => 'ro', required => 1); |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
has engine => (is => 'lazy'); |
14
|
|
|
|
|
|
|
has results => (is => 'lazy'); |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub _build_engine { |
17
|
|
|
|
|
|
|
my $self = shift; |
18
|
|
|
|
|
|
|
my $engine = LWP::UserAgent->new(); |
19
|
|
|
|
|
|
|
return $engine; |
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub _build_results { |
23
|
|
|
|
|
|
|
my $self = shift; |
24
|
|
|
|
|
|
|
my $items_raw = $self->__search(); |
25
|
|
|
|
|
|
|
my $items = []; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
# The results are in $items_raw->{'items'} |
28
|
|
|
|
|
|
|
my $parser = Catmandu::RKD::API::Parse->new(results => $items_raw); |
29
|
|
|
|
|
|
|
my $parsed_items = $parser->items; |
30
|
|
|
|
|
|
|
push @{$items}, @{$parsed_items->{'items'}}; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
# The API paginates, but we want all the results. So continue as long as there are items |
33
|
|
|
|
|
|
|
while ($parsed_items->{'total'} > $parsed_items->{'start'} * $parsed_items->{'per_page'}) { |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
my $startIndex = $parsed_items->{'start'} + $parsed_items->{'per_page'}; |
36
|
|
|
|
|
|
|
$items_raw = $self->__search($startIndex); |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
$parser = Catmandu::RKD::API::Parse->new(results => $items_raw); |
39
|
|
|
|
|
|
|
$parsed_items = $parser->items; |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
push @{$items}, @{$parsed_items->{'items'}}; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
# Return [{ |
45
|
|
|
|
|
|
|
# guid => foo, |
46
|
|
|
|
|
|
|
# title => bar, |
47
|
|
|
|
|
|
|
# description => biz, |
48
|
|
|
|
|
|
|
# artist_link => xyz |
49
|
|
|
|
|
|
|
#}] |
50
|
|
|
|
|
|
|
my $extracter = Catmandu::RKD::API::Extract->new(results => $items); |
51
|
|
|
|
|
|
|
return $extracter->items; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
## |
55
|
|
|
|
|
|
|
# Query the API |
56
|
|
|
|
|
|
|
sub __search { |
57
|
|
|
|
|
|
|
my ($self, $startIndex) = @_; |
58
|
|
|
|
|
|
|
my $url; |
59
|
|
|
|
|
|
|
if (defined($startIndex)) { |
60
|
|
|
|
|
|
|
my $template = '%s&startIndex=%s'; |
61
|
|
|
|
|
|
|
$url = sprintf($template, $self->url, $startIndex); |
62
|
|
|
|
|
|
|
} else { |
63
|
|
|
|
|
|
|
$url = $self->url; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
my $response = $self->engine->get($url); |
66
|
|
|
|
|
|
|
if ($response->is_success) { |
67
|
|
|
|
|
|
|
return $response->decoded_content; |
68
|
|
|
|
|
|
|
} else { |
69
|
|
|
|
|
|
|
Catmandu::HTTPError->throw({ |
70
|
|
|
|
|
|
|
code => $response->code, |
71
|
|
|
|
|
|
|
message => $response->status_line, |
72
|
|
|
|
|
|
|
url => $response->request->uri, |
73
|
|
|
|
|
|
|
method => $response->request->method, |
74
|
|
|
|
|
|
|
request_headers => [], |
75
|
|
|
|
|
|
|
request_body => $response->request->decoded_content, |
76
|
|
|
|
|
|
|
response_headers => [], |
77
|
|
|
|
|
|
|
response_body => $response->decoded_content, |
78
|
|
|
|
|
|
|
}); |
79
|
|
|
|
|
|
|
return undef; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
1; |