| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package WebService::MusicBrainz::Request; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 5 |  |  | 5 |  | 32 | use Mojo::Base -base; | 
|  | 5 |  |  |  |  | 9 |  | 
|  | 5 |  |  |  |  | 27 |  | 
| 4 | 5 |  |  | 5 |  | 2858 | use Mojo::UserAgent; | 
|  | 5 |  |  |  |  | 1077392 |  | 
|  | 5 |  |  |  |  | 38 |  | 
| 5 | 5 |  |  | 5 |  | 192 | use Mojo::URL; | 
|  | 5 |  |  |  |  | 10 |  | 
|  | 5 |  |  |  |  | 19 |  | 
| 6 | 5 |  |  | 5 |  | 117 | use Mojo::Util qw/dumper/; | 
|  | 5 |  |  |  |  | 10 |  | 
|  | 5 |  |  |  |  | 3252 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | has url_base => 'https://musicbrainz.org/ws/2'; | 
| 9 |  |  |  |  |  |  | has ua => sub { Mojo::UserAgent->new() }; | 
| 10 |  |  |  |  |  |  | has 'format' => 'json'; | 
| 11 |  |  |  |  |  |  | has 'search_resource'; | 
| 12 |  |  |  |  |  |  | has 'mbid'; | 
| 13 |  |  |  |  |  |  | has 'discid'; | 
| 14 |  |  |  |  |  |  | has 'inc' => sub { [] }; | 
| 15 |  |  |  |  |  |  | has 'query_params'; | 
| 16 |  |  |  |  |  |  | has offset => 0; | 
| 17 |  |  |  |  |  |  | has debug => sub { $ENV{MUSICBRAINZ_DEBUG} || 0 };; | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | our $VERSION = '1.0'; | 
| 20 |  |  |  |  |  |  |  | 
| 21 | 5 |  |  | 5 |  | 29 | binmode STDOUT, ":encoding(UTF-8)"; | 
|  | 5 |  |  |  |  | 8 |  | 
|  | 5 |  |  |  |  | 35 |  | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | sub make_url { | 
| 24 | 22 |  |  | 22 | 0 | 46 | my $self = shift; | 
| 25 |  |  |  |  |  |  |  | 
| 26 | 22 |  |  |  |  | 46 | my @url_parts; | 
| 27 |  |  |  |  |  |  |  | 
| 28 | 22 |  |  |  |  | 104 | push @url_parts, $self->url_base(); | 
| 29 | 22 |  |  |  |  | 175 | push @url_parts, $self->search_resource(); | 
| 30 | 22 | 100 |  |  |  | 110 | push @url_parts, $self->mbid() if $self->mbid; | 
| 31 | 22 | 50 |  |  |  | 162 | push @url_parts, $self->discid() if $self->discid; | 
| 32 |  |  |  |  |  |  |  | 
| 33 | 22 |  |  |  |  | 177 | my $url_str = join '/', @url_parts; | 
| 34 |  |  |  |  |  |  |  | 
| 35 | 22 |  |  |  |  | 88 | $url_str .= '?fmt=' . $self->format; | 
| 36 |  |  |  |  |  |  |  | 
| 37 | 22 | 100 |  |  |  | 145 | if(scalar(@{ $self->inc }) > 0) { | 
|  | 22 |  |  |  |  | 68 |  | 
| 38 | 5 |  |  |  |  | 32 | my $inc_query = join '+', @{ $self->inc }; | 
|  | 5 |  |  |  |  | 15 |  | 
| 39 |  |  |  |  |  |  |  | 
| 40 | 5 |  |  |  |  | 30 | $url_str .= '&inc=' . $inc_query; | 
| 41 |  |  |  |  |  |  | } | 
| 42 |  |  |  |  |  |  |  | 
| 43 | 22 |  |  |  |  | 49 | my @extra_params; | 
| 44 |  |  |  |  |  |  |  | 
| 45 | 22 |  |  |  |  | 47 | foreach my $key (keys %{ $self->query_params }) { | 
|  | 22 |  |  |  |  | 178 |  | 
| 46 | 19 |  |  |  |  | 127 | push @extra_params, $key . ':"' . $self->query_params->{$key} . '"'; | 
| 47 |  |  |  |  |  |  | } | 
| 48 |  |  |  |  |  |  |  | 
| 49 | 22 | 100 |  |  |  | 167 | if(scalar(@extra_params) > 0) { | 
| 50 | 12 |  |  |  |  | 49 | my $extra_param_str = join ' AND ', @extra_params; | 
| 51 |  |  |  |  |  |  |  | 
| 52 | 12 |  |  |  |  | 39 | $url_str .= '&query=' . $extra_param_str; | 
| 53 |  |  |  |  |  |  | } | 
| 54 |  |  |  |  |  |  |  | 
| 55 | 22 |  |  |  |  | 104 | $url_str .= '&offset=' . $self->offset(); | 
| 56 |  |  |  |  |  |  |  | 
| 57 | 22 | 50 |  |  |  | 189 | print "REQUEST URL: $url_str\n" if $self->debug(); | 
| 58 |  |  |  |  |  |  |  | 
| 59 | 22 |  |  |  |  | 237 | my $url = Mojo::URL->new($url_str); | 
| 60 |  |  |  |  |  |  |  | 
| 61 | 22 |  |  |  |  | 4348 | return $url; | 
| 62 |  |  |  |  |  |  | } | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  | sub result { | 
| 65 | 22 |  |  | 22 | 0 | 163 | my $self = shift; | 
| 66 |  |  |  |  |  |  |  | 
| 67 | 22 |  |  |  |  | 85 | my $request_url = $self->make_url(); | 
| 68 |  |  |  |  |  |  |  | 
| 69 | 22 |  |  |  |  | 96 | my $get_result = $self->ua->get($request_url => { 'Accept-Encoding' => 'application/json' })->result; | 
| 70 |  |  |  |  |  |  |  | 
| 71 | 22 |  |  |  |  | 4544658 | my $result_formatted; | 
| 72 |  |  |  |  |  |  |  | 
| 73 | 22 | 100 |  |  |  | 135 | if($self->format eq 'json') { | 
|  |  | 50 |  |  |  |  |  | 
| 74 | 19 |  |  |  |  | 196 | $result_formatted = $get_result->json; | 
| 75 | 19 | 50 |  |  |  | 1249281 | print "JSON RESULT: ", dumper($get_result->json) if $self->debug; | 
| 76 |  |  |  |  |  |  | } elsif($self->format eq 'xml') { | 
| 77 | 3 |  |  |  |  | 55 | $result_formatted = $get_result->dom; | 
| 78 | 3 | 50 |  |  |  | 9534 | print "XML RESULT: ", $get_result->dom->to_string, "\n" if $self->debug; | 
| 79 |  |  |  |  |  |  | } else { | 
| 80 | 0 |  |  |  |  | 0 | warn "Unsupported format type : $self->format"; | 
| 81 |  |  |  |  |  |  | } | 
| 82 |  |  |  |  |  |  |  | 
| 83 | 22 |  |  |  |  | 267 | return $result_formatted; | 
| 84 |  |  |  |  |  |  | } | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  | =head1 NAME | 
| 87 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  | WebService::MusicBrainz::Request | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 91 |  |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  | =head1 ABSTRACT | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | WebService::MusicBrainz::Request - Handle queries using the MusicBrainz WebService API version 2 | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 97 |  |  |  |  |  |  |  | 
| 98 |  |  |  |  |  |  | =head1 METHODS | 
| 99 |  |  |  |  |  |  |  | 
| 100 |  |  |  |  |  |  | =head1 AUTHOR | 
| 101 |  |  |  |  |  |  |  | 
| 102 |  |  |  |  |  |  | =over 4 | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | =item Bob Faist | 
| 105 |  |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  | =back | 
| 107 |  |  |  |  |  |  |  | 
| 108 |  |  |  |  |  |  | =head1 COPYRIGHT AND LICENSE | 
| 109 |  |  |  |  |  |  |  | 
| 110 |  |  |  |  |  |  | Copyright 2006-2017 by Bob Faist | 
| 111 |  |  |  |  |  |  |  | 
| 112 |  |  |  |  |  |  | This library is free software; you can redistribute it and/or modify | 
| 113 |  |  |  |  |  |  | it under the same terms as Perl itself. | 
| 114 |  |  |  |  |  |  |  | 
| 115 |  |  |  |  |  |  | =head1 SEE ALSO | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  | https://wiki.musicbrainz.org/XMLWebService | 
| 118 |  |  |  |  |  |  |  | 
| 119 |  |  |  |  |  |  | =cut | 
| 120 |  |  |  |  |  |  |  | 
| 121 |  |  |  |  |  |  | 1; |