| 1 | package Net::Async::Spotify::API::Base; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 29 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 29 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 29 | our $VERSION = '0.002'; # VERSION | |||||||||||||||||||||||||||||||||||||||||
| 7 | 29 | our $AUTHORITY = 'cpan:VNEALV'; # AUTHORITY | |||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 29 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 29 | ▶ | use Log::Any qw($log); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 29 | ▶ | use Syntax::Keyword::Try; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 29 | ▶ | use Scalar::Util qw(weaken isweak); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 29 | ▶ | use URI; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 29 | ▶ | use URI::QueryParam; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 29 | ▶ | use JSON::MaybeUTF8 qw(:v1); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 29 | ▶ | use Net::Async::Spotify::Object; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | Net::Async::Spotify::API::Base - Base Package for Spotify API Classes. | ||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | Base class to be used by all Autogenerated modules of Spotify API | ||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 0 | my $self = bless { @_[1..$#_] }, $_[0]; | |||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | ▶ | die "Net::Async::Spotify has to be provided" unless $self->spotify and $self->spotify->isa('Net::Async::Spotify'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->spotify and $self->spotify->isa("Net::Async::Spotify")
MC/DC0%0/2
$self->spotify and $self->spotify->isa("Net::Async::Spotify")
$self->spotify
$self->spotify->isa("Net::Async::Spotify")
Truth table0%0/3
A and B
A= $self->spotifyB= $self->spotify->isa("Net::Async::Spotify")
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | ▶ | weaken($self->{spotify}) unless isweak($self->spotify); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | $self->{mapping} = {}; | |||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | ▶ | sub spotify { shift->{spotify} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
spotify: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | =head2 call_api | ||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | Calls Spotify API with the passed params. | ||||||||||||||||||||||||||||||||||||||||||
| 46 | Allowing also to accept user passed response type C<obj_type> as member of C<%args> | ||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | ▶ | async sub call_api { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
call_api: not called CC=22 SCAR=61.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | my ($self, $request, $response_objs, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | my ( $result, $request_headers, $request_uri, $request_content ); | |||||||||||||||||||||||||||||||||||||||||
| 54 | # Prepare needed response params. | ||||||||||||||||||||||||||||||||||||||||||
| 55 | # override if response type passed explicitly | ||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | ▶ | $response_objs = [$args{obj_type}] if exists $args{obj_type}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | # Path Parameter | ||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | ▶ | if (my $path_parameter = $request->{param}{path_parameter} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | # Path parameters should be always required. | ||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | for my $key (keys $path_parameter->%*) { | |||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | ▶ | unless ( exists $args{$key} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | $log->errorf('Required Path Parameter missing %s', $key); | |||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | $request->{uri} = $request->{uri} =~ s/{$key}/$args{$key}/r; | |||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | $request_uri = URI->new($request->{uri}); | |||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | # Headers; at the moment only Authorization | ||||||||||||||||||||||||||||||||||||||||||
| 71 | # Content header will be set only of content passed. | ||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | my $authorize = $request->{param}{header}{Authorization}; | |||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | ▶ | if ( $authorize and $authorize->{required} eq 'required' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$authorize and $authorize->{"required"} eq "required"
MC/DC0%0/2
$authorize and $authorize->{"required"} eq "required"
$authorize
$authorize->{"required"} eq "required"
Truth table0%0/3
A and B
A= $authorizeB= $authorize->{"required"} eq "required"
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | $request_headers->{Authorization} = $self->spotify->token->header_string; | |||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||
| 76 | # Unfortunately the API documentation is not consistent so check all possibilities. | ||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | ▶ | if ( exists $request->{param}{header}{'Content-Type'} | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
exists $request->{"param"}{"header"}{"Content-Type"} or exists $request->{"param"}{"json_body_parameter"} or $request->{"method"} eq "POST"
exists $request->{"param"}{"header"}{"Content-Type"} or exists $request->{"param"}{"json_body_parameter"}
MC/DC0%0/3
exists $request->{"param"}{"header"}{"Content-Type"} or exists $request->{"param"}{"json_body_parameter"} or $request->{"method"} eq "POST"
exists $request->{"param"}{"header"}{"Content-Type"}
exists $request->{"param"}{"json_body_parameter"}
$request->{"method"} eq "POST"
Truth table0%0/4
A or B or C
A= exists $request->{"param"}{"header"}{"Content-Type"}B= exists $request->{"param"}{"json_body_parameter"}C= $request->{"method"} eq "POST"
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | or exists $request->{param}{json_body_parameter} | ||||||||||||||||||||||||||||||||||||||||||
| 79 | or $request->{method} eq 'POST' | ||||||||||||||||||||||||||||||||||||||||||
| 80 | ) { | ||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | $request_content = { | |||||||||||||||||||||||||||||||||||||||||
| 82 | content => {}, | ||||||||||||||||||||||||||||||||||||||||||
| 83 | content_type => 'application/json', | ||||||||||||||||||||||||||||||||||||||||||
| 84 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | # Query parameters | ||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | for my $qp (keys $request->{param}{query_parameter}->%*) { | |||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | ▶ | $request_uri->query_param( $qp => $args{$qp} ) if exists $args{$qp}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | # There is no need to confirm data Type | ||||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | ▶ | if ( !defined $request_uri->query_param($qp) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $request_uri->query_param($qp) and $request->{"param"}{"query_parameter"}{$qp}{"required"} eq "required"
MC/DC0%0/2
not defined $request_uri->query_param($qp) and $request->{"param"}{"query_parameter"}{$qp}{"required"} eq "required"
not defined $request_uri->query_param($qp)
$request->{"param"}{"query_parameter"}{$qp}{"required"} eq "required"
Truth table0%0/3
not(A) and B
A= not defined $request_uri->query_param($qp)B= $request->{"param"}{"query_parameter"}{$qp}{"required"} eq "required"
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | and $request->{param}{query_parameter}{$qp}{required} eq 'required') { | ||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | $log->errorf('Missing Required Query Parameter: %s', $qp); | |||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | # Body parameters | ||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | for my $bp (keys $request->{param}{json_body_parameter}->%*) { | |||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | ▶ | if ( exists $args{$bp} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | ▶ | if ( $request->{param}{json_body_parameter}{$bp}{type} =~ /^array/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | if ( ref($args{$bp}) eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | $request_content->{content}{$bp} = $args{$bp}; | |||||||||||||||||||||||||||||||||||||||||
| 104 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | push $request_content->{content}{$bp}->@*, split ',', $args{$bp}; | |||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||
| 107 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | $request_content->{content}{$bp} = $args{$bp}; | |||||||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||||||
| 110 | } elsif ( $request->{param}{json_body_parameter}{$bp}{required} eq 'required' ) { | ||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | $log->errorf('Missing Required Body Parameter: %s', $bp); | |||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | ▶ | $request_content->{content} = encode_json_utf8($request_content->{content}) if exists $request_content->{content}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | try { | ||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | $log->tracef('Requesting Spotify API: request: %s | uri: %s | content: %s', $request, $request_uri, $request_content); | |||||||||||||||||||||||||||||||||||||||||
| 119 | $result = await $self->spotify->http->do_request( | ||||||||||||||||||||||||||||||||||||||||||
| 120 | method => $request->{method}, | ||||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | ▶ | uri => $request_uri, | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | defined $request_content ? ($request_content->%*) : (), | ||||||||||||||||||||||||||||||||||||||||||
| 123 | headers => $request_headers, | ||||||||||||||||||||||||||||||||||||||||||
| 124 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | $log->tracef('Spotify API response: %s', $result); | |||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | } catch ($e) { | |||||||||||||||||||||||||||||||||||||||||
| 127 | 29 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | $log->errorf('Error calling Spotify API request: %s | Error: %s', $request_uri->as_string, Dumper($e)); | |||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | ▶ | if ( $result->content ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | status_line => $result->status_line, | |||||||||||||||||||||||||||||||||||||||||
| 135 | content => $self->parse_response($self->decode_response($result->content), $response_objs) | ||||||||||||||||||||||||||||||||||||||||||
| 136 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | return { status_line => $result->status_line, content => $result->content }; | |||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||
| 140 | 29 | } | |||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | ▶ | sub decode_response { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
decode_response: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | 0 | my ( $self, $content ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | try { | ||||||||||||||||||||||||||||||||||||||||||
| 146 | 0 | return decode_json_utf8($content); | |||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | } catch ($e) { | |||||||||||||||||||||||||||||||||||||||||
| 148 | 0 | $log->warnf('Could not JSON decode Spotify API Response | Error: %s', $e); | |||||||||||||||||||||||||||||||||||||||||
| 149 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | 0 | ▶ | sub parse_response { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse_response: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | 0 | my ( $self, $decoded_res, $expected ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||
| 156 | 0 | $log->tracef('API Base Mapping: %s', $self->mapping); | |||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||
| 158 | try { | ||||||||||||||||||||||||||||||||||||||||||
| 159 | 0 | ▶ | return $decoded_res ? Net::Async::Spotify::Object->new($decoded_res, $expected) : $decoded_res; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 160 | 0 | } catch ($e) { | |||||||||||||||||||||||||||||||||||||||||
| 161 | 0 | $log->warnf('Could not Map Spotify API Response to its Object %s | Error: %s | data: %s', $expected, $e, $decoded_res); | |||||||||||||||||||||||||||||||||||||||||
| 162 | 0 | return $decoded_res; | |||||||||||||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | 29 | 1; | |||||||||||||||||||||||||||||||||||||||||