| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package WebService::Shutterstock::Client; |
|
2
|
|
|
|
|
|
|
{ |
|
3
|
|
|
|
|
|
|
$WebService::Shutterstock::Client::VERSION = '0.006'; |
|
4
|
|
|
|
|
|
|
} |
|
5
|
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
# ABSTRACT: Provides easy REST interactions with the Shutterstock API |
|
7
|
|
|
|
|
|
|
|
|
8
|
11
|
|
|
11
|
|
8012
|
use strict; |
|
|
11
|
|
|
|
|
21
|
|
|
|
11
|
|
|
|
|
419
|
|
|
9
|
11
|
|
|
11
|
|
60
|
use warnings; |
|
|
11
|
|
|
|
|
23
|
|
|
|
11
|
|
|
|
|
469
|
|
|
10
|
11
|
|
|
11
|
|
56
|
use Moo; |
|
|
11
|
|
|
|
|
20
|
|
|
|
11
|
|
|
|
|
73
|
|
|
11
|
11
|
|
|
11
|
|
7249
|
use JSON qw(decode_json); |
|
|
11
|
|
|
|
|
52588
|
|
|
|
11
|
|
|
|
|
108
|
|
|
12
|
11
|
|
|
11
|
|
9124
|
use WebService::Shutterstock::Exception; |
|
|
11
|
|
|
|
|
49
|
|
|
|
11
|
|
|
|
|
1007
|
|
|
13
|
|
|
|
|
|
|
|
|
14
|
11
|
|
|
11
|
|
1990
|
use REST::Client; |
|
|
11
|
|
|
|
|
154790
|
|
|
|
11
|
|
|
|
|
10684
|
|
|
15
|
|
|
|
|
|
|
extends 'REST::Client'; |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub response { |
|
19
|
52
|
|
|
52
|
1
|
85884
|
my $self = shift; |
|
20
|
52
|
100
|
|
|
|
235
|
if(@_){ |
|
21
|
20
|
|
|
|
|
71
|
$self->{_res} = $_[0]; |
|
22
|
|
|
|
|
|
|
} |
|
23
|
52
|
|
|
|
|
464
|
return $self->{_res}; |
|
24
|
|
|
|
|
|
|
} |
|
25
|
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
sub GET { |
|
27
|
21
|
|
|
21
|
1
|
1162
|
my($self, $url, $query, $headers) = @_; |
|
28
|
21
|
100
|
|
|
|
110
|
if(ref($query) eq 'HASH'){ |
|
29
|
19
|
|
|
|
|
214
|
$url .= $self->buildQuery(%$query); |
|
30
|
|
|
|
|
|
|
} |
|
31
|
21
|
|
|
|
|
78732
|
$self->SUPER::GET($url, $headers); |
|
32
|
21
|
|
|
|
|
6034
|
return $self->response; |
|
33
|
|
|
|
|
|
|
} |
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub DELETE { |
|
36
|
1
|
|
|
1
|
|
3
|
my($self, $url, $query, $headers) = @_; |
|
37
|
1
|
50
|
|
|
|
7
|
if(ref($query) eq 'HASH'){ |
|
38
|
1
|
|
|
|
|
60
|
$url .= $self->buildQuery(%$query); |
|
39
|
|
|
|
|
|
|
} |
|
40
|
1
|
|
|
|
|
209
|
$self->SUPER::DELETE($url, $headers); |
|
41
|
1
|
|
|
|
|
1959
|
return $self->response; |
|
42
|
|
|
|
|
|
|
} |
|
43
|
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub PUT { |
|
45
|
1
|
|
|
1
|
1
|
156
|
my($self, $url, $content, $headers) = @_; |
|
46
|
1
|
50
|
|
|
|
6
|
if(ref($content) eq 'HASH'){ |
|
47
|
0
|
|
|
|
|
0
|
my $uri = URI->new(); |
|
48
|
0
|
|
|
|
|
0
|
$uri->query_form(%$content); |
|
49
|
0
|
|
|
|
|
0
|
$content = $uri->query; |
|
50
|
0
|
|
0
|
|
|
0
|
$headers ||= {}; |
|
51
|
0
|
|
|
|
|
0
|
$headers->{'Content-Type'} = 'application/x-www-form-urlencoded'; |
|
52
|
|
|
|
|
|
|
} |
|
53
|
1
|
|
|
|
|
9
|
$self->SUPER::PUT($url, $content, $headers); |
|
54
|
1
|
|
|
|
|
2020
|
return $self->response; |
|
55
|
|
|
|
|
|
|
} |
|
56
|
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub POST { |
|
58
|
8
|
|
|
8
|
1
|
72
|
my($self, $url, $content, $headers) = @_; |
|
59
|
8
|
50
|
|
|
|
38
|
if(ref($content) eq 'HASH'){ |
|
60
|
8
|
|
|
|
|
52
|
my $uri = URI->new(); |
|
61
|
8
|
|
|
|
|
19380
|
$uri->query_form(%$content); |
|
62
|
8
|
|
|
|
|
1323
|
$content = $uri->query; |
|
63
|
8
|
|
50
|
|
|
131
|
$headers ||= {}; |
|
64
|
8
|
|
|
|
|
50
|
$headers->{'Content-Type'} = 'application/x-www-form-urlencoded'; |
|
65
|
|
|
|
|
|
|
} |
|
66
|
8
|
|
|
|
|
100
|
$self->SUPER::POST($url, $content, $headers); |
|
67
|
8
|
|
|
|
|
4915
|
return $self->response; |
|
68
|
|
|
|
|
|
|
} |
|
69
|
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub process_response { |
|
72
|
31
|
|
|
31
|
1
|
124
|
my $self = shift; |
|
73
|
|
|
|
|
|
|
my %handlers = ( |
|
74
|
0
|
|
|
0
|
|
0
|
204 => sub { 1 }, # empty response, but success |
|
75
|
1
|
|
|
1
|
|
38
|
401 => sub { die WebService::Shutterstock::Exception->new(response => shift, error => "invalid api_username or api_key"); }, |
|
76
|
|
|
|
|
|
|
@_ |
|
77
|
31
|
|
|
|
|
309
|
); |
|
78
|
|
|
|
|
|
|
|
|
79
|
31
|
|
|
|
|
569
|
my $code = $self->responseCode; |
|
80
|
31
|
|
100
|
|
|
558
|
my $content_type = $self->responseHeader('Content-Type') || ''; |
|
81
|
|
|
|
|
|
|
|
|
82
|
31
|
|
|
|
|
4203
|
my $response = $self->{_res}; # blech, why isn't this public? |
|
83
|
31
|
|
|
|
|
261
|
my $request = $response->request; |
|
84
|
|
|
|
|
|
|
|
|
85
|
31
|
50
|
|
|
|
511
|
if(my $error = $response->header('X-Died')){ |
|
86
|
0
|
|
|
|
|
0
|
die WebService::Shutterstock::Exception->new( |
|
87
|
|
|
|
|
|
|
response => $response, |
|
88
|
|
|
|
|
|
|
error => sprintf( 'Transport error: %s', $error ) |
|
89
|
|
|
|
|
|
|
); |
|
90
|
|
|
|
|
|
|
} |
|
91
|
|
|
|
|
|
|
|
|
92
|
31
|
100
|
|
|
|
1747
|
if(my $h = $handlers{$code}){ |
|
|
|
100
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
93
|
1
|
|
|
|
|
4
|
$h->($response); |
|
94
|
|
|
|
|
|
|
} elsif($code <= 299){ # a success |
|
95
|
27
|
100
|
66
|
|
|
648
|
return $content_type =~ m{^application/json} && $self->responseContent ? decode_json($self->responseContent) : $response->decoded_content; |
|
96
|
|
|
|
|
|
|
} elsif($code <= 399){ # a redirect of some sort |
|
97
|
0
|
|
|
|
|
0
|
return $self->responseHeader('Location'); |
|
98
|
|
|
|
|
|
|
} elsif($code <= 499){ # client-side error |
|
99
|
3
|
|
|
|
|
52
|
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
|
|
|
|
|
0
|
die WebService::Shutterstock::Exception->new( response => $response, error => sprintf('%s: %s', $response->status_line, $response->decoded_content) ); |
|
102
|
|
|
|
|
|
|
} |
|
103
|
|
|
|
|
|
|
} |
|
104
|
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
sub BUILD { |
|
107
|
12
|
|
|
12
|
0
|
182348
|
my $self = shift; |
|
108
|
12
|
50
|
|
|
|
473
|
if($ENV{SS_API_DEBUG}){ |
|
109
|
0
|
|
|
0
|
|
|
$self->getUseragent->add_handler("request_send", sub { shift->dump(prefix => '> '); return }); |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
110
|
0
|
|
|
0
|
|
|
$self->getUseragent->add_handler("response_done", sub { shift->dump(prefix => '< '); return }); |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
} |
|
112
|
|
|
|
|
|
|
} |
|
113
|
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
1; |
|
115
|
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
__END__ |