| 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.001'; # 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 | sub new { | ||||||||||||||||||||||||||||||||||||||||||
| 33 | 0 | ▶ | my $self = bless { @_[1..$#_] }, $_[0]; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 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; | |||||||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | 0 | ▶ | sub spotify { shift->{spotify} } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
spotify: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | =head2 call_api | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | Calls Spotify API with the passed params. | ||||||||||||||||||||||||||||||||||||||||||
| 45 | Allowing also to accept user passed response type C<obj_type> as member of C<%args> | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | ▶ | async sub call_api { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
call_api: not called CC=23 SCAR=62.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | my ($self, $request, $response_objs, %args) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | $log->tracef('CALL API CALLED: %s | %s | %s', $request, $response_objs, \%args); | |||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | my ( $result, $request_headers, $request_uri, $request_content, $res_hash, $decoded_res, $mapped_res ); | |||||||||||||||||||||||||||||||||||||||||
| 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 | 0 | $res_hash = {response_objs => $response_objs, uri => $request->{uri}}; | |||||||||||||||||||||||||||||||||||||||||
| 58 | # Path Parameter | ||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | ▶ | if (my $path_parameter = $request->{param}{path_parameter} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | # Path parameters should be always required. | ||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | for my $key (keys $path_parameter->%*) { | |||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | ▶ | unless ( exists $args{$key} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | $log->errorf('Required Path Parameter missing %s', $key); | |||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | $request->{uri} = $request->{uri} =~ s/{$key}/$args{$key}/r; | |||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | $request_uri = URI->new($request->{uri}); | |||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | # Headers; at the moment only Authorization | ||||||||||||||||||||||||||||||||||||||||||
| 72 | # Content header will be set only of content passed. | ||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | my $authorize = $request->{param}{header}{Authorization}; | |||||||||||||||||||||||||||||||||||||||||
| 74 | 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"
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | $request_headers->{Authorization} = $self->spotify->token->header_string; | |||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||
| 77 | # Unfortunately the API documentation is not consistent so check all possibilities. | ||||||||||||||||||||||||||||||||||||||||||
| 78 | 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"
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | or exists $request->{param}{json_body_parameter} | ||||||||||||||||||||||||||||||||||||||||||
| 80 | or $request->{method} eq 'POST' | ||||||||||||||||||||||||||||||||||||||||||
| 81 | ) { | ||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | $request_content = { | |||||||||||||||||||||||||||||||||||||||||
| 83 | content => {}, | ||||||||||||||||||||||||||||||||||||||||||
| 84 | content_type => 'application/json', | ||||||||||||||||||||||||||||||||||||||||||
| 85 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | # Query parameters | ||||||||||||||||||||||||||||||||||||||||||
| 89 | 0 | for my $qp (keys $request->{param}{query_parameter}->%*) { | |||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | ▶ | $request_uri->query_param( $qp => $args{$qp} ) if exists $args{$qp}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 91 | # There is no need to confirm data Type | ||||||||||||||||||||||||||||||||||||||||||
| 92 | 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"
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | and $request->{param}{query_parameter}{$qp}{required} eq 'required') { | ||||||||||||||||||||||||||||||||||||||||||
| 94 | 0 | $log->errorf('Missing Required Query Parameter: %s', $qp); | |||||||||||||||||||||||||||||||||||||||||
| 95 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | # Body parameters | ||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | for my $bp (keys $request->{param}{json_body_parameter}->%*) { | |||||||||||||||||||||||||||||||||||||||||
| 101 | 0 | ▶ | if ( exists $args{$bp} ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | ▶ | unless ( $request->{param}{json_body_parameter}{$bp}{type} =~ /^array/ | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$request->{"param"}{"json_body_parameter"}{$bp}{"type"} =~ /^array/ or ref $args{$bp} eq "ARRAY"
MC/DC0%0/2
$request->{"param"}{"json_body_parameter"}{$bp}{"type"} =~ /^array/ or ref $args{$bp} eq "ARRAY"
$request->{"param"}{"json_body_parameter"}{$bp}{"type"} =~ /^array/
ref $args{$bp} eq "ARRAY"
Truth table0%0/3
A or B
A= $request->{"param"}{"json_body_parameter"}{$bp}{"type"} =~ /^array/B= ref $args{$bp} eq "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | or ref($args{$bp}) eq 'ARRAY' ) { | ||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | $request_content->{content}{$bp} = $args{$bp}; | |||||||||||||||||||||||||||||||||||||||||
| 105 | } else { | ||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | push $request_content->{content}{$bp}->@*, split ',', $args{$bp}; | |||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||
| 108 | } elsif ( $request->{param}{json_body_parameter}{$bp}{required} eq 'required' ) { | ||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | $log->errorf('Missing Required Body Parameter: %s', $bp); | |||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | ▶ | $request_content->{content} = encode_json_utf8($request_content->{content}) if exists $request_content->{content}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | try { | ||||||||||||||||||||||||||||||||||||||||||
| 116 | $result = await $self->spotify->http->do_request( | ||||||||||||||||||||||||||||||||||||||||||
| 117 | method => $request->{method}, | ||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | ▶ | uri => $request_uri, | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | defined $request_content ? ($request_content->%*) : (), | ||||||||||||||||||||||||||||||||||||||||||
| 120 | headers => $request_headers, | ||||||||||||||||||||||||||||||||||||||||||
| 121 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | } catch ($e) { | |||||||||||||||||||||||||||||||||||||||||
| 123 | 29 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | $log->errorf('Error calling Spotify API request: %s | Error: %s', $request_uri->as_string, Dumper($e)); | |||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | try { | ||||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | ▶ | $decoded_res = $result->content ? decode_json_utf8($result->content) : $result->content; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | } catch ($e) { | |||||||||||||||||||||||||||||||||||||||||
| 131 | 0 | $log->warnf('Could not JSON decode Spotify API Response | Error: %s', $e); | |||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | return { status_line => $result->status_line, content => $result->content }; | |||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||
| 134 | # Separate Try/Catch for better error isolation | ||||||||||||||||||||||||||||||||||||||||||
| 135 | try { | ||||||||||||||||||||||||||||||||||||||||||
| 136 | 0 | ▶ | $mapped_res = $decoded_res ? Net::Async::Spotify::Object->new($decoded_res, $res_hash) : $decoded_res; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 137 | 0 | } catch ($e) { | |||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | $log->warnf('Could not Map Spotify API Response to its Object %s | Error: %s | data: %s', $res_hash, $e, $decoded_res); | |||||||||||||||||||||||||||||||||||||||||
| 139 | 0 | return { status_line => $result->status_line, content => $decoded_res }; | |||||||||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | return { status_line => $result->status_line, content => $mapped_res }; | |||||||||||||||||||||||||||||||||||||||||
| 143 | 29 | } | |||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | 29 | 1; | |||||||||||||||||||||||||||||||||||||||||