| 1 | package WWW::Marvel::Client; | ||||||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use Digest::MD5; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use JSON; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use URI; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | sub new { | ||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | my ($class, $args) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 10 | 1 | my $self = bless {}, $class; | |||||||||||||||||||||||||||||||
| 11 | 1 | for my $k (qw/ private_key public_key /) { | |||||||||||||||||||||||||||||||
| 12 | 2 | ▶ | $self->{$k} = $args->{$k} if exists $args->{$k}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 13 | } | ||||||||||||||||||||||||||||||||
| 14 | 1 | return $self; | |||||||||||||||||||||||||||||||
| 15 | } | ||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||
| 17 | 3 | ▶ | sub get_private_key { $_[0]->{private_key} } | ||||||||||||||||||||||||||||||
|
Subroutine
get_private_key: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 18 | 4 | ▶ | sub get_public_key { $_[0]->{public_key} } | ||||||||||||||||||||||||||||||
|
Subroutine
get_public_key: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 19 | 4 | ▶ | sub get_timestamp { $_[0]->{timestamp} } | ||||||||||||||||||||||||||||||
|
Subroutine
get_timestamp: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 20 | sub set_timestamp { | ||||||||||||||||||||||||||||||||
| 21 | 1 | ▶ | my ($self, $timestamp) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
set_timestamp: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 22 | 1 | $self->{timestamp} = $timestamp; | |||||||||||||||||||||||||||||||
| 23 | 1 | return $self; | |||||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||
| 26 | 1 | ▶ | sub get_endpoint { "http://gateway.marvel.com" } | ||||||||||||||||||||||||||||||
|
Subroutine
get_endpoint: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 27 | 1 | ▶ | sub get_api_version { "v1" } | ||||||||||||||||||||||||||||||
|
Subroutine
get_api_version: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | sub auth_query_params { | ||||||||||||||||||||||||||||||||
| 30 | 1 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
auth_query_params: called CC=2 SCAR=7.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||
| 32 | 1 | ▶ | my $time = $self->get_timestamp || time; | ||||||||||||||||||||||||||||||
|
Condition33%1/3
$self->get_timestamp || time
MC/DC0%0/2
$self->get_timestamp || time
$self->get_timestamp
time
Truth table33%1/3
A || B
A= $self->get_timestampB= time
| |||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||
| 34 | 1 | my %params = ( | |||||||||||||||||||||||||||||||
| 35 | ts => $time, | ||||||||||||||||||||||||||||||||
| 36 | apikey => $self->get_public_key, | ||||||||||||||||||||||||||||||||
| 37 | hash => $self->hash($time), | ||||||||||||||||||||||||||||||||
| 38 | ); | ||||||||||||||||||||||||||||||||
| 39 | 1 | return \%params; | |||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||
| 42 | sub characters { | ||||||||||||||||||||||||||||||||
| 43 | 0 | ▶ | my ($self, $params) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
characters: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 44 | 0 | my %p = %$params; | |||||||||||||||||||||||||||||||
| 45 | 0 | my $uri = $self->uri({ path => 'characters', params => \%p }); | |||||||||||||||||||||||||||||||
| 46 | 0 | my $http_res = $self->get($uri); | |||||||||||||||||||||||||||||||
| 47 | 0 | return $self->_get_json_content($http_res); | |||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||
| 50 | sub get { | ||||||||||||||||||||||||||||||||
| 51 | 0 | ▶ | my ($self, $uri) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
get: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 52 | 0 | my $attempts_limit = 3; | |||||||||||||||||||||||||||||||
| 53 | 0 | my $http_res; | |||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | while ($attempts_limit > 0) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 55 | 0 | $http_res = $self->ua->get($uri); | |||||||||||||||||||||||||||||||
| 56 | 0 | ▶ | last if $http_res->is_success; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||
| 58 | 0 | carp sprintf("Status line: '%s'", $http_res->status_line); | |||||||||||||||||||||||||||||||
| 59 | 0 | $attempts_limit--; | |||||||||||||||||||||||||||||||
| 60 | 0 | ▶ | next if $attempts_limit && $http_res->status_line eq "500 read timeout"; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$attempts_limit and $http_res->status_line eq "500 read timeout"
MC/DC0%0/2
$attempts_limit and $http_res->status_line eq "500 read timeout"
$attempts_limit
$http_res->status_line eq "500 read timeout"
Truth table0%0/3
A and B
A= $attempts_limitB= $http_res->status_line eq "500 read timeout"
| |||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||
| 62 | 0 | croak sprintf("Status line: '%s'\nReq: %s\nRes: %s", | |||||||||||||||||||||||||||||||
| 63 | $http_res->status_line, | ||||||||||||||||||||||||||||||||
| 64 | $http_res->request->as_string, | ||||||||||||||||||||||||||||||||
| 65 | $http_res->as_string); | ||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||
| 68 | 0 | return $http_res; | |||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||
| 71 | # ref: https://developer.marvel.com/documentation/authorization | ||||||||||||||||||||||||||||||||
| 72 | sub hash { | ||||||||||||||||||||||||||||||||
| 73 | 3 | ▶ | my ($self, $time) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
hash: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||
| 75 | 3 | ▶ | if (!defined $time) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 76 | 1 | ▶ | croak "need a timestamp to create a md5 hash" if !defined $self->get_timestamp; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 77 | 1 | $time = $self->get_timestamp; | |||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||
| 80 | 3 | my $md5 = Digest::MD5->new; | |||||||||||||||||||||||||||||||
| 81 | 3 | $md5->add( $time, $self->get_private_key, $self->get_public_key ); | |||||||||||||||||||||||||||||||
| 82 | 3 | return $md5->hexdigest; | |||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||
| 85 | sub _get_json_content { | ||||||||||||||||||||||||||||||||
| 86 | 0 | ▶ | my ($self, $http_res) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
_get_json_content: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||
| 87 | 0 | my $content = $self->json->decode( $http_res->decoded_content ); | |||||||||||||||||||||||||||||||
| 88 | 0 | return $content; | |||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||
| 91 | 0 | ▶ | sub json { $_[0]->{_json} //= do { JSON->new()->allow_nonref->relaxed } } | ||||||||||||||||||||||||||||||
|
Subroutine
json: not called CC=2 SCAR=17.9
Pod
undocumented
Condition0%0/3
$_[0]{"_json"} //= do {
"JSON"->new->allow_nonref->relaxed
}
MC/DC0%0/2
$_[0]{"_json"} //= do {
"JSON"->new->allow_nonref->relaxed
}
$_[0]{"_json"}
do {
"JSON"->new->allow_nonref->relaxed
}
Truth table0%0/3
A //= B
A= $_[0]{"_json"}B= do {
"JSON"->new->allow_nonref->relaxed
}
| |||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||
| 93 | 0 | ▶ | sub ua { LWP::UserAgent->new() } | ||||||||||||||||||||||||||||||
|
Subroutine
ua: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||
| 95 | sub uri { | ||||||||||||||||||||||||||||||||
| 96 | 1 | ▶ | my ($self, $args) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
uri: called CC=7 SCAR=23.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||
| 99 | 1 | my @paths; | |||||||||||||||||||||||||||||||
| 100 | 1 | ▶ | if (exists $args->{path} && ref $args->{path} eq 'ARRAY') { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%1/3
exists $args->{"path"} and ref $args->{"path"} eq "ARRAY"
MC/DC0%0/2
exists $args->{"path"} and ref $args->{"path"} eq "ARRAY"
exists $args->{"path"}
ref $args->{"path"} eq "ARRAY"
Truth table33%1/3
A and B
A= exists $args->{"path"}B= ref $args->{"path"} eq "ARRAY"
| |||||||||||||||||||||||||||||||||
| 101 | 0 | @paths = @{ $args->{path} }; | |||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||
| 103 | ▶ | elsif (exists $args->{path} && ref $args->{path} eq '') { | |||||||||||||||||||||||||||||||
|
Condition33%1/3
exists $args->{"path"} and ref $args->{"path"} eq ""
MC/DC0%0/2
exists $args->{"path"} and ref $args->{"path"} eq ""
exists $args->{"path"}
ref $args->{"path"} eq ""
Truth table33%1/3
A and B
A= exists $args->{"path"}B= ref $args->{"path"} eq ""
| |||||||||||||||||||||||||||||||||
| 104 | 1 | push(@paths, $args->{path}); | |||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||
| 107 | 1 | my %params; | |||||||||||||||||||||||||||||||
| 108 | 1 | ▶ | if (exists $args->{params} && ref $args->{params} eq 'HASH') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $args->{"params"} and ref $args->{"params"} eq "HASH"
MC/DC0%0/2
exists $args->{"params"} and ref $args->{"params"} eq "HASH"
exists $args->{"params"}
ref $args->{"params"} eq "HASH"
Truth table33%1/3
A and B
A= exists $args->{"params"}B= ref $args->{"params"} eq "HASH"
| |||||||||||||||||||||||||||||||||
| 109 | 0 | %params = %{ $args->{params} }; | |||||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||
| 113 | 1 | my $auth_query_params = $self->auth_query_params; | |||||||||||||||||||||||||||||||
| 114 | 1 | my %query_params = ( %params, %$auth_query_params ); | |||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||
| 116 | 1 | my $uri = URI->new( $self->get_endpoint ); | |||||||||||||||||||||||||||||||
| 117 | 1 | $uri->path( $self->uri_path(@paths) ); | |||||||||||||||||||||||||||||||
| 118 | 1 | $uri->query_form(%query_params); | |||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||
| 120 | 1 | return $uri; | |||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||
| 123 | sub uri_path { | ||||||||||||||||||||||||||||||||
| 124 | 1 | ▶ | my ($self, @resources) = @_; | ||||||||||||||||||||||||||||||
|
Subroutine
uri_path: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 125 | 1 | ▶ | croak "need a resource to query" if @resources < 1; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 126 | 1 | return join('/', $self->get_api_version, 'public', @resources ); | |||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||
| 129 | 2 | 1; | |||||||||||||||||||||||||||||||