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