line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package App::Presto::Client; |
2
|
|
|
|
|
|
|
BEGIN { |
3
|
1
|
|
|
1
|
|
75190
|
$App::Presto::Client::AUTHORITY = 'cpan:BPHILLIPS'; |
4
|
|
|
|
|
|
|
} |
5
|
|
|
|
|
|
|
{ |
6
|
|
|
|
|
|
|
$App::Presto::Client::VERSION = '0.009'; |
7
|
|
|
|
|
|
|
} |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
# ABSTRACT: The REST client |
10
|
|
|
|
|
|
|
|
11
|
1
|
|
|
1
|
|
9
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
37
|
|
12
|
1
|
|
|
1
|
|
221
|
use warnings; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
28
|
|
13
|
1
|
|
|
1
|
|
8447
|
use Moo; |
|
1
|
|
|
|
|
19635
|
|
|
1
|
|
|
|
|
7
|
|
14
|
1
|
|
|
1
|
|
2802
|
use REST::Client; |
|
1
|
|
|
|
|
193950
|
|
|
1
|
|
|
|
|
33
|
|
15
|
1
|
|
|
1
|
|
10
|
use URI; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
20
|
|
16
|
1
|
|
|
1
|
|
1080
|
use URI::QueryParam; |
|
1
|
|
|
|
|
866
|
|
|
1
|
|
|
|
|
39
|
|
17
|
|
|
|
|
|
|
use Module::Pluggable |
18
|
1
|
|
|
|
|
12
|
instantiate => 'new', |
19
|
|
|
|
|
|
|
sub_name => 'content_handlers', |
20
|
1
|
|
|
1
|
|
1112
|
search_path => ['App::Presto::Client::ContentHandlers']; |
|
1
|
|
|
|
|
27706
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
has config => ( is => 'ro', required => 1 ); |
23
|
|
|
|
|
|
|
has _rest_client => ( is => 'lazy', ); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub _build__rest_client { |
26
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
27
|
0
|
|
|
|
|
0
|
return REST::Client->new; |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub all_headers { |
31
|
2
|
|
|
2
|
0
|
70
|
my $self = shift; |
32
|
2
|
|
50
|
|
|
49
|
my $headers = $self->_rest_client->{_headers} || {}; |
33
|
2
|
|
|
|
|
27
|
return %$headers; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub set_header { |
37
|
1
|
|
|
1
|
0
|
920
|
my $self = shift; |
38
|
1
|
|
|
|
|
3
|
my($name, $value) = @_; |
39
|
1
|
|
|
|
|
26
|
return $self->_rest_client->addHeader(lc $name, $value); |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub get_header { |
43
|
1
|
|
|
1
|
0
|
3
|
my $self = shift; |
44
|
1
|
|
|
|
|
2
|
my $header = lc shift; |
45
|
1
|
|
50
|
|
|
26
|
my $headers = $self->_rest_client->{_headers} || {}; |
46
|
1
|
50
|
|
|
|
14
|
return exists $headers->{$header} ? $headers->{$header} : undef; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub clear_header { |
50
|
3
|
|
|
3
|
0
|
6
|
my $self = shift; |
51
|
3
|
|
|
|
|
8
|
my $name = lc shift; |
52
|
3
|
|
|
|
|
98
|
return delete $self->_rest_client->{_headers}->{$name}; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub clear_headers { |
56
|
1
|
|
|
1
|
0
|
3
|
my $self = shift; |
57
|
1
|
50
|
|
|
|
2
|
%{$self->_rest_client->{_headers} || {}} = (); |
|
1
|
|
|
|
|
27
|
|
58
|
1
|
|
|
|
|
21
|
return; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub GET { |
62
|
2
|
|
|
2
|
0
|
6093
|
my $self = shift; |
63
|
2
|
|
|
|
|
11
|
my $uri = $self->_make_uri(@_); |
64
|
2
|
|
|
|
|
42
|
my $existing_content_type = $self->clear_header('content-type'); # GET shouldn't have a content type |
65
|
2
|
|
|
|
|
544
|
my $response = $self->_rest_client->GET($uri); |
66
|
2
|
50
|
|
|
|
156
|
$self->set_header('content-type', $existing_content_type) if $existing_content_type; |
67
|
2
|
|
|
|
|
6
|
return $response; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub HEAD { |
71
|
1
|
|
|
1
|
0
|
12649
|
my $self = shift; |
72
|
1
|
|
|
|
|
6
|
my $uri = $self->_make_uri(@_); |
73
|
1
|
|
|
|
|
10
|
my $existing_content_type = $self->clear_header('content-type'); # HEAD shouldn't have a content type |
74
|
1
|
|
|
|
|
29
|
my $response = $self->_rest_client->HEAD($uri); |
75
|
1
|
50
|
|
|
|
57
|
$self->set_header('content-type', $existing_content_type) if $existing_content_type; |
76
|
1
|
|
|
|
|
3
|
return $response; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
sub DELETE { |
80
|
1
|
|
|
1
|
|
1258
|
my $self = shift; |
81
|
1
|
|
|
|
|
27
|
my $uri = $self->_make_uri(shift); |
82
|
1
|
|
|
|
|
33
|
my $response = $self->_rest_client->request('DELETE',$uri, shift); |
83
|
1
|
|
|
|
|
75
|
return $response; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub POST { |
87
|
1
|
|
|
1
|
0
|
979
|
my $self = shift; |
88
|
1
|
|
|
|
|
5
|
my $uri = $self->_make_uri(shift); |
89
|
1
|
|
|
|
|
29
|
$self->_rest_client->POST( $uri, shift ); |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub PUT { |
93
|
2
|
|
|
2
|
0
|
2495
|
my $self = shift; |
94
|
2
|
|
|
|
|
8
|
my $uri = $self->_make_uri(shift); |
95
|
2
|
|
|
|
|
90
|
$self->_rest_client->PUT( $uri, shift ); |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
sub _make_uri { |
99
|
7
|
|
|
7
|
|
14
|
my $self = shift; |
100
|
7
|
|
|
|
|
12
|
my $local_uri = shift; |
101
|
7
|
|
|
|
|
17
|
my @args = @_; |
102
|
7
|
|
|
|
|
28
|
my $config = $self->config; |
103
|
|
|
|
|
|
|
|
104
|
7
|
|
|
|
|
10
|
my $endpoint; |
105
|
7
|
100
|
|
|
|
25
|
$local_uri = '/' if ! defined $local_uri; |
106
|
7
|
|
|
|
|
48
|
$local_uri = URI->new($local_uri); |
107
|
7
|
100
|
|
|
|
12812
|
if ( $local_uri->scheme ) { |
108
|
1
|
|
|
|
|
23
|
$endpoint = $local_uri; |
109
|
|
|
|
|
|
|
} else { |
110
|
6
|
|
|
|
|
197
|
my $local_path = $local_uri->path; |
111
|
6
|
|
|
|
|
126
|
$endpoint = $config->endpoint; |
112
|
6
|
100
|
|
|
|
427
|
$endpoint .= '*' unless $endpoint =~ m/\*/; |
113
|
6
|
|
|
|
|
26
|
$endpoint =~ s{\*}{$local_path}; |
114
|
6
|
100
|
|
|
|
31
|
$endpoint .= '?' . $local_uri->query if $local_uri->query; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
7
|
|
|
|
|
105
|
my $u = $self->_append_query_params( $endpoint, @args ); |
118
|
7
|
|
|
|
|
190
|
return "$u"; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
sub _append_query_params { |
122
|
7
|
|
|
7
|
|
14
|
my $self = shift; |
123
|
7
|
|
|
|
|
29
|
my $u = URI->new(shift); |
124
|
7
|
|
|
|
|
4609
|
my @args = @_; |
125
|
7
|
|
|
|
|
18
|
foreach my $next (@args) { |
126
|
3
|
|
|
|
|
256
|
$u->query_param_append( split( /=/, $next, 2 ) ); |
127
|
|
|
|
|
|
|
} |
128
|
7
|
|
|
|
|
137
|
return $u; |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
sub response { |
132
|
0
|
|
0
|
0
|
0
|
|
return shift->_rest_client->{_res} || undef; |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
sub has_response_content { |
136
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
137
|
0
|
|
0
|
|
|
|
return $self->response->content_length |
138
|
|
|
|
|
|
|
|| length( $self->response->content ); |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
sub response_data { |
142
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
143
|
0
|
|
|
|
|
|
my $response = $self->response; |
144
|
0
|
0
|
|
|
|
|
if ( my $content_type = $response->header('Content-type') ) { |
145
|
0
|
|
|
|
|
|
foreach my $h ( $self->content_handlers ) { |
146
|
0
|
0
|
|
|
|
|
if ( $h->can_deserialize($content_type) ) { |
147
|
0
|
|
|
|
|
|
return $h->deserialize( $response->content ); |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
} |
151
|
0
|
|
|
|
|
|
return $response->decoded_content; |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
1; |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
__END__ |