| 1 | package WebService::Shutterstock::Client; | ||||||||||||||||||||||||||||
| 2 | { | ||||||||||||||||||||||||||||
| 3 | 3 | $WebService::Shutterstock::Client::VERSION = '0.006'; | |||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | # ABSTRACT: Provides easy REST interactions with the Shutterstock API | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use JSON qw(decode_json); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use WebService::Shutterstock::Exception; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 3 | ▶ | use REST::Client; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 3 | extends 'REST::Client'; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 2 | ▶ | sub response { | ||||||||||||||||||||||||||
|
Subroutine
response: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 19 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 20 | 2 | ▶ | if(@_){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | 1 | $self->{_res} = $_[0]; | |||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | 2 | return $self->{_res}; | |||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 1 | ▶ | sub GET { | ||||||||||||||||||||||||||
|
Subroutine
GET: called CC=2 SCAR=7.4
Pod
documented
| |||||||||||||||||||||||||||||
| 27 | 1 | my($self, $url, $query, $headers) = @_; | |||||||||||||||||||||||||||
| 28 | 1 | ▶ | if(ref($query) eq 'HASH'){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 0 | $url .= $self->buildQuery(%$query); | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | 1 | $self->SUPER::GET($url, $headers); | |||||||||||||||||||||||||||
| 32 | 1 | return $self->response; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 0 | ▶ | sub DELETE { | ||||||||||||||||||||||||||
|
Subroutine
DELETE: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 36 | 0 | my($self, $url, $query, $headers) = @_; | |||||||||||||||||||||||||||
| 37 | 0 | ▶ | if(ref($query) eq 'HASH'){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 0 | $url .= $self->buildQuery(%$query); | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | 0 | $self->SUPER::DELETE($url, $headers); | |||||||||||||||||||||||||||
| 41 | 0 | return $self->response; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 0 | ▶ | sub PUT { | ||||||||||||||||||||||||||
|
Subroutine
PUT: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 45 | 0 | my($self, $url, $content, $headers) = @_; | |||||||||||||||||||||||||||
| 46 | 0 | ▶ | if(ref($content) eq 'HASH'){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 0 | my $uri = URI->new(); | |||||||||||||||||||||||||||
| 48 | 0 | $uri->query_form(%$content); | |||||||||||||||||||||||||||
| 49 | 0 | $content = $uri->query; | |||||||||||||||||||||||||||
| 50 | 0 | ▶ | $headers ||= {}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$headers ||= {}
MC/DC0%0/1
$headers ||= {}
$headers
Truth table0%0/2
A
A= $headers
| |||||||||||||||||||||||||||||
| 51 | 0 | $headers->{'Content-Type'} = 'application/x-www-form-urlencoded'; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | 0 | $self->SUPER::PUT($url, $content, $headers); | |||||||||||||||||||||||||||
| 54 | 0 | return $self->response; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 0 | ▶ | sub POST { | ||||||||||||||||||||||||||
|
Subroutine
POST: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 58 | 0 | my($self, $url, $content, $headers) = @_; | |||||||||||||||||||||||||||
| 59 | 0 | ▶ | if(ref($content) eq 'HASH'){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 0 | my $uri = URI->new(); | |||||||||||||||||||||||||||
| 61 | 0 | $uri->query_form(%$content); | |||||||||||||||||||||||||||
| 62 | 0 | $content = $uri->query; | |||||||||||||||||||||||||||
| 63 | 0 | ▶ | $headers ||= {}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$headers ||= {}
MC/DC0%0/1
$headers ||= {}
$headers
Truth table0%0/2
A
A= $headers
| |||||||||||||||||||||||||||||
| 64 | 0 | $headers->{'Content-Type'} = 'application/x-www-form-urlencoded'; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | 0 | $self->SUPER::POST($url, $content, $headers); | |||||||||||||||||||||||||||
| 67 | 0 | return $self->response; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 1 | ▶ | sub process_response { | ||||||||||||||||||||||||||
|
Subroutine
process_response: called CC=10 SCAR=33.6
Pod
documented
| |||||||||||||||||||||||||||||
| 72 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 73 | my %handlers = ( | ||||||||||||||||||||||||||||
| 74 | 0 | ▶ | 204 => sub { 1 }, # empty response, but success | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 75 | 0 | ▶ | 401 => sub { die WebService::Shutterstock::Exception->new(response => shift, error => "invalid api_username or api_key"); }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 76 | @_ | ||||||||||||||||||||||||||||
| 77 | 1 | ); | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 1 | my $code = $self->responseCode; | |||||||||||||||||||||||||||
| 80 | 1 | ▶ | my $content_type = $self->responseHeader('Content-Type') || ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->responseHeader('Content-Type') || ''
MC/DC0%0/1
$self->responseHeader('Content-Type') || ''
$self->responseHeader('Content-Type')
Truth table50%1/2
A
A= $self->responseHeader('Content-Type')
| |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 1 | my $response = $self->{_res}; # blech, why isn't this public? | |||||||||||||||||||||||||||
| 83 | 1 | my $request = $response->request; | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 1 | ▶ | if(my $error = $response->header('X-Died')){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 0 | die WebService::Shutterstock::Exception->new( | |||||||||||||||||||||||||||
| 87 | response => $response, | ||||||||||||||||||||||||||||
| 88 | error => sprintf( 'Transport error: %s', $error ) | ||||||||||||||||||||||||||||
| 89 | ); | ||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 1 | ▶ | if(my $h = $handlers{$code}){ | ||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||
| 93 | 0 | $h->($response); | |||||||||||||||||||||||||||
| 94 | } elsif($code <= 299){ # a success | ||||||||||||||||||||||||||||
| 95 | 1 | ▶ | return $content_type =~ m{^application/json} && $self->responseContent ? decode_json($self->responseContent) : $response->decoded_content; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$content_type =~ m[^application/json] and $self->responseContent
MC/DC0%0/2
$content_type =~ m[^application/json] and $self->responseContent
$content_type =~ m[^application/json]
$self->responseContent
Truth table33%1/3
A and B
A= $content_type =~ m[^application/json]B= $self->responseContent
| |||||||||||||||||||||||||||||
| 96 | } elsif($code <= 399){ # a redirect of some sort | ||||||||||||||||||||||||||||
| 97 | 0 | return $self->responseHeader('Location'); | |||||||||||||||||||||||||||
| 98 | } elsif($code <= 499){ # client-side error | ||||||||||||||||||||||||||||
| 99 | 0 | die WebService::Shutterstock::Exception->new( response => $response, error => sprintf('%s: %s', $response->status_line, $response->decoded_content) ); | |||||||||||||||||||||||||||
| 100 | } elsif($code >= 500){ # server-side error | ||||||||||||||||||||||||||||
| 101 | 0 | die WebService::Shutterstock::Exception->new( response => $response, error => sprintf('%s: %s', $response->status_line, $response->decoded_content) ); | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 2 | ▶ | sub BUILD { | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=2 SCAR=12.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 107 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 108 | 2 | ▶ | if($ENV{SS_API_DEBUG}){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | 0 | ▶ | $self->getUseragent->add_handler("request_send", sub { shift->dump(prefix => '> '); return }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 110 | 0 | ▶ | $self->getUseragent->add_handler("response_done", sub { shift->dump(prefix => '< '); return }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 3 | 1; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | __END__ | ||||||||||||||||||||||||||||