MusicBrainz/Request.pm

filter: Statementstmt 97.1% 68 / 70 Branchbran 72.7% 16 / 22 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 0.0% 0 / 2 totaltotal 90.3% 94 / 104 CC 13 SCAR 14.0
CC
13
Cov
91%
make_url
7.0
result
5.2
__ANON__
2.1
CRAP 4.1
« MusicBrainz.pm ↑ Summary
1 package WebService::MusicBrainz::Request;
2  
3 6 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Mojo::Util qw/dumper/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 6 has url_base => 'https://musicbrainz.org/ws/2';
9 has ua => sub
10 25 ▶{
Subroutine __ANON__: called CC=2 SCAR=7.4
11 25 ▶ if (eval { my $ua = Mojo::UserAgent->with_roles("+Retry")->new(); 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { my $ua = 'Mojo::UserAgent'->with_roles('+Retry')->new; 1 } }) { } 25 0 25
12 25 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 0 return Mojo::UserAgent->new();
21 }
22 6 };
23 6 has 'format' => 'json';
24 6 has 'search_resource';
25 6 has 'mbid';
26 6 has 'discid';
27 18 ▶has 'inc' => sub { [] };
Subroutine __ANON__: called CC=1 SCAR=0.0
28 6 has 'query_params';
29 6 has offset => 0;
30 25 ▶has debug => sub { $ENV{MUSICBRAINZ_DEBUG} || 0 };;
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$ENV{"MUSICBRAINZ_DEBUG"} or 0 0 25 25
31  
32 6 our $VERSION = '1.1';
33  
34 6 ▶binmode STDOUT, ":encoding(UTF-8)";
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 25 ▶sub make_url {
Subroutine make_url: called CC=7 SCAR=19.5
Pod undocumented
37 25 my $self = shift;
38  
39 25 my @url_parts;
40  
41 25 push @url_parts, $self->url_base();
42 25 push @url_parts, $self->search_resource();
43 25 ▶ push @url_parts, $self->mbid() if $self->mbid;
Branch: 1 branch
BranchTrueFalseTotal
if $self->mbid 10 15 25
44 25 ▶ push @url_parts, $self->discid() if $self->discid;
Branch: 1 branch
BranchTrueFalseTotal
if $self->discid 3 22 25
45  
46 25 my $url_str = join '/', @url_parts;
47  
48 25 $url_str .= '?fmt=' . $self->format;
49  
50 25 ▶ if(scalar(@{ $self->inc }) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->inc;} > 0) 7 18 25
51 7 my $inc_query = join '+', @{ $self->inc };
52  
53 7 $url_str .= '&inc=' . $inc_query;
54 }
55  
56 25 my @extra_params;
57  
58 25 foreach my $key (keys %{ $self->query_params }) {
59 19 push @extra_params, $key . ':"' . $self->query_params->{$key} . '"';
60 }
61  
62 25 ▶ if(scalar(@extra_params) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @extra_params > 0) 12 13 25
63 12 my $extra_param_str = join ' AND ', @extra_params;
64  
65 12 $url_str .= '&query=' . $extra_param_str;
66 }
67  
68 25 $url_str .= '&offset=' . $self->offset();
69  
70 25 ▶ print "REQUEST URL: $url_str\n" if $self->debug();
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 25 25
71  
72 25 my $url = Mojo::URL->new($url_str);
73  
74 25 return $url;
75 }
76  
77 25 ▶sub result {
Subroutine result: called CC=5 SCAR=16.6
Pod undocumented
78 25 my $self = shift;
79  
80 25 my $request_url = $self->make_url();
81  
82 25 my $get_result = $self->ua->get($request_url => { 'Accept-Encoding' => 'application/json' })->result;
83  
84 25 my $result_formatted;
85  
86 25 ▶ if($self->format eq 'json') {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->format eq 'json') { } 22 3 25
elsif ($self->format eq 'xml') { } 3 0 3
87 22 $result_formatted = $get_result->json;
88 22 ▶ print "JSON RESULT: ", dumper($get_result->json) if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 22 22
89 } elsif($self->format eq 'xml') {
90 3 $result_formatted = $get_result->dom;
91 3 ▶ print "XML RESULT: ", $get_result->dom->to_string, "\n" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 3 3
92 } else {
93 0 warn "Unsupported format type : $self->format";
94 }
95  
96 25 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 <bob.faist@gmail.com>
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 6 1;
« MusicBrainz.pm ↑ Summary