| 1 | package WWW::Marvel::Client; | ||
| 2 | 1 | ▶ | use base qw/ WWW::Marvel /; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use Digest::MD5; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use JSON; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 6 | use LWP::UserAgent; | ||
| 7 | use URI; | ||
| 8 | __PACKAGE__->follow_best_practice; | ||
| 9 | __PACKAGE__->mk_accessors(qw/ timestamp /); | ||
| 10 | |||
| 11 | sub characters { | ||
| 12 | my ($self, $params) = @_; | ||
| 13 | my %p = %$params; | ||
| 14 | my $uri = $self->uri({ path => 'characters', params => \%p }); | ||
| 15 | my $http_res = $self->_get_http_res($uri); | ||
| 16 | return $self->_get_json_content($http_res); | ||
| 17 | } | ||
| 18 | |||
| 19 | sub _get_http_res { | ||
| 20 | my ($self, $uri) = @_; | ||
| 21 | my $http_res = $self->ua->get($uri); | ||
| 22 | croak $http_res->status_line if !$http_res->is_success; | ||
| 23 | return $http_res; | ||
| 24 | } | ||
| 25 | |||
| 26 | sub _get_json_content { | ||
| 27 | my ($self, $http_res) = @_; | ||
| 28 | my $json = JSON->new(); | ||
| 29 | my $content = $json->allow_nonref->relaxed->decode( $http_res->decoded_content ); | ||
| 30 | return $content; | ||
| 31 | } | ||
| 32 | |||
| 33 | # ref: https://developer.marvel.com/documentation/authorization | ||
| 34 | sub hash { | ||
| 35 | my ($self, $time) = @_; | ||
| 36 | |||
| 37 | if (!defined $time) { | ||
| 38 | croak "need a timestamp to create a md5 hash" if !defined $self->get_timestamp; | ||
| 39 | $time = $self->get_timestamp; | ||
| 40 | } | ||
| 41 | |||
| 42 | my $md5 = Digest::MD5->new; | ||
| 43 | $md5->add( $time, $self->get_private_key, $self->get_public_key ); | ||
| 44 | return $md5->hexdigest; | ||
| 45 | } | ||
| 46 | |||
| 47 | sub ua { LWP::UserAgent->new() } | ||
| 48 | |||
| 49 | sub uri { | ||
| 50 | my ($self, $args) = @_; | ||
| 51 | |||
| 52 | my $time = $self->get_timestamp || time; | ||
| 53 | |||
| 54 | my @paths; | ||
| 55 | if (exists $args->{path} && ref $args->{path} eq 'ARRAY') { | ||
| 56 | @paths = @{ $args->{path} }; | ||
| 57 | } | ||
| 58 | elsif (exists $args->{path} && ref $args->{path} eq '') { | ||
| 59 | push(@paths, $args->{path}); | ||
| 60 | } | ||
| 61 | |||
| 62 | my %params; | ||
| 63 | if (exists $args->{params} && ref $args->{params} eq 'HASH') { | ||
| 64 | %params = %{ $args->{params} }; | ||
| 65 | } | ||
| 66 | |||
| 67 | my %query_params = ( | ||
| 68 | %params, | ||
| 69 | ts => $time, | ||
| 70 | hash => $self->hash($time), | ||
| 71 | apikey => $self->get_public_key, | ||
| 72 | ); | ||
| 73 | |||
| 74 | my $uri = URI->new( $self->get_endpoint ); | ||
| 75 | $uri->path( $self->uri_path(@paths) ); | ||
| 76 | $uri->query_form(%query_params); | ||
| 77 | return $uri; | ||
| 78 | } | ||
| 79 | |||
| 80 | sub uri_path { | ||
| 81 | my ($self, @resources) = @_; | ||
| 82 | croak "need a resource to query" if @resources < 1; | ||
| 83 | return join('/', $self->get_api_version, 'public', @resources ); | ||
| 84 | } | ||
| 85 | |||
| 86 | 1; | ||