File Coverage

blib/lib/WebService/MusicBrainz/Request.pm
Criterion Covered Total %
statement 49 50 98.0
branch 14 18 77.7
condition n/a
subroutine 7 7 100.0
pod 0 2 0.0
total 70 77 90.9


line stmt bran cond sub pod time code
1             package WebService::MusicBrainz::Request;
2              
3 6     6   42 use Mojo::Base -base;
  6         12  
  6         38  
4 6     6   5062 use Mojo::UserAgent;
  6         3502452  
  6         53  
5 6     6   273 use Mojo::URL;
  6         15  
  6         24  
6 6     6   213 use Mojo::Util qw/dumper/;
  6         13  
  6         5687  
7              
8             has url_base => 'https://musicbrainz.org/ws/2';
9             has ua => sub
10             {
11             if (eval { my $ua = Mojo::UserAgent->with_roles("+Retry")->new(); 1 }) {
12             return Mojo::UserAgent->with_roles('+Retry')->new(
13             retries => 4, # try up to 4 times (total 5 attempts)
14             retry_wait_min => 1, # seconds
15             retry_wait_max => 15, # exponential backoff up to 15s
16             # Optional: custom policy (default retries on connection errors + 429/503)
17             # retry_policy => sub { ... }
18             );
19             } else {
20             return Mojo::UserAgent->new();
21             }
22             };
23             has 'format' => 'json';
24             has 'search_resource';
25             has 'mbid';
26             has 'discid';
27             has 'inc' => sub { [] };
28             has 'query_params';
29             has offset => 0;
30             has debug => sub { $ENV{MUSICBRAINZ_DEBUG} || 0 };;
31              
32             our $VERSION = '1.1';
33              
34 6     6   4083 binmode STDOUT, ":encoding(UTF-8)";
  6         100  
  6         32  
35              
36             sub make_url {
37 25     25 0 57 my $self = shift;
38              
39 25         50 my @url_parts;
40              
41 25         106 push @url_parts, $self->url_base();
42 25         206 push @url_parts, $self->search_resource();
43 25 100       133 push @url_parts, $self->mbid() if $self->mbid;
44 25 100       174 push @url_parts, $self->discid() if $self->discid;
45              
46 25         176 my $url_str = join '/', @url_parts;
47              
48 25         90 $url_str .= '?fmt=' . $self->format;
49              
50 25 100       172 if(scalar(@{ $self->inc }) > 0) {
  25         116  
51 7         43 my $inc_query = join '+', @{ $self->inc };
  7         17  
52              
53 7         37 $url_str .= '&inc=' . $inc_query;
54             }
55              
56 25         728 my @extra_params;
57              
58 25         64 foreach my $key (keys %{ $self->query_params }) {
  25         85  
59 19         124 push @extra_params, $key . ':"' . $self->query_params->{$key} . '"';
60             }
61              
62 25 100       174 if(scalar(@extra_params) > 0) {
63 12         46 my $extra_param_str = join ' AND ', @extra_params;
64              
65 12         29 $url_str .= '&query=' . $extra_param_str;
66             }
67              
68 25         105 $url_str .= '&offset=' . $self->offset();
69              
70 25 50       260 print "REQUEST URL: $url_str\n" if $self->debug();
71              
72 25         362 my $url = Mojo::URL->new($url_str);
73              
74 25         6077 return $url;
75             }
76              
77             sub result {
78 25     25 0 186 my $self = shift;
79              
80 25         102 my $request_url = $self->make_url();
81              
82 25         147 my $get_result = $self->ua->get($request_url => { 'Accept-Encoding' => 'application/json' })->result;
83              
84 25         7115544 my $result_formatted;
85              
86 25 100       167 if($self->format eq 'json') {
    50          
87 22         293 $result_formatted = $get_result->json;
88 22 50       15232 print "JSON RESULT: ", dumper($get_result->json) if $self->debug;
89             } elsif($self->format eq 'xml') {
90 3         64 $result_formatted = $get_result->dom;
91 3 50       16171 print "XML RESULT: ", $get_result->dom->to_string, "\n" if $self->debug;
92             } else {
93 0         0 warn "Unsupported format type : $self->format";
94             }
95              
96 25         298 return $result_formatted;
97             }
98              
99             =head1 NAME
100              
101             WebService::MusicBrainz::Request
102              
103             =head1 SYNOPSIS
104              
105             =head1 ABSTRACT
106              
107             WebService::MusicBrainz::Request - Handle queries using the MusicBrainz WebService API version 2
108              
109             =head1 DESCRIPTION
110              
111             =head1 METHODS
112              
113             =head1 AUTHOR
114              
115             =over 4
116              
117             =item Bob Faist
118              
119             =back
120              
121             =head1 COPYRIGHT AND LICENSE
122              
123             Copyright 2006-2017 by Bob Faist
124              
125             This library is free software; you can redistribute it and/or modify
126             it under the same terms as Perl itself.
127              
128             =head1 SEE ALSO
129              
130             https://wiki.musicbrainz.org/XMLWebService
131              
132             =cut
133              
134             1;