line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package CPAN::Mini::Inject::REST::Client::API; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
938
|
use Moose; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
use Carp qw/confess/; |
5
|
|
|
|
|
|
|
use HTTP::Request::Common; |
6
|
|
|
|
|
|
|
use JSON; |
7
|
|
|
|
|
|
|
use MIME::Base64; |
8
|
|
|
|
|
|
|
use REST::Client; |
9
|
|
|
|
|
|
|
use Try::Tiny; |
10
|
|
|
|
|
|
|
use URI; |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
has 'host' => (isa => 'Str', is => 'ro', required => 1); |
13
|
|
|
|
|
|
|
has 'protocol' => (isa => 'Str', is => 'ro', required => 1); |
14
|
|
|
|
|
|
|
has 'port' => (isa => 'Int', is => 'ro', required => 1); |
15
|
|
|
|
|
|
|
has 'username' => (isa => 'Str', is => 'ro'); |
16
|
|
|
|
|
|
|
has 'password' => (isa => 'Str', is => 'ro'); |
17
|
|
|
|
|
|
|
has 'client' => (isa => 'REST::Client', is => 'ro', lazy => 1, builder => '_build_client'); |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
#--Initiate a REST::Client object with optional HTTP authorisation-------------- |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub _build_client { |
23
|
|
|
|
|
|
|
my $self = shift; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
my $client = REST::Client->new; |
26
|
|
|
|
|
|
|
if ($self->username && $self->password) { |
27
|
|
|
|
|
|
|
$client->addHeader('Authorization', 'Basic ' . encode_base64($self->username . ":" . $self->password)); |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
return $client; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
#--Define the API version to use------------------------------------------------ |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub base_uri { |
37
|
|
|
|
|
|
|
return '/api/1.0'; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
#--Send an HTTP POST request to the server-------------------------------------- |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub post { |
44
|
|
|
|
|
|
|
my ($self, $path, $params) = @_; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
my $request = POST( |
47
|
|
|
|
|
|
|
$self->uri($path), |
48
|
|
|
|
|
|
|
Content_Type => 'form-data', |
49
|
|
|
|
|
|
|
Content => $params, |
50
|
|
|
|
|
|
|
); |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
my $response = $self->client->POST( |
53
|
|
|
|
|
|
|
$self->uri($path), |
54
|
|
|
|
|
|
|
$request->content, |
55
|
|
|
|
|
|
|
{'Content-Type' => $request->header('Content-Type')}, |
56
|
|
|
|
|
|
|
); |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
return $self->process($response); |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
#--Send an HTTP GET request to the server--------------------------------------- |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub get { |
65
|
|
|
|
|
|
|
my ($self, $path, $params) = @_; |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
my $uri = $self->uri($path); |
68
|
|
|
|
|
|
|
$uri .= '?' . $self->query_string($params) if $params && %$params; |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
my $response = $self->client->GET($uri); |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
return $self->process($response); |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
#--Construct a complete URI from a path string---------------------------------- |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub uri { |
79
|
|
|
|
|
|
|
my ($self, $path) = @_; |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
return join '', |
82
|
|
|
|
|
|
|
$self->protocol, '://', |
83
|
|
|
|
|
|
|
$self->host, ':', $self->port, |
84
|
|
|
|
|
|
|
$self->base_uri, '/', $path; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
#--Convert a hashref of parameters into a query string-------------------------- |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub query_string { |
91
|
|
|
|
|
|
|
my ($self, $query_params) = @_; |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
my $url = URI->new('http:'); |
94
|
|
|
|
|
|
|
$url->query_form(%$query_params); |
95
|
|
|
|
|
|
|
my $query_string = $url->query; |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
return $query_string; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
#--Decode the result of an API request------------------------------------------ |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
sub process { |
104
|
|
|
|
|
|
|
my ($self, $response) = @_; |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
my $content = try { |
107
|
|
|
|
|
|
|
decode_json($response->responseContent); |
108
|
|
|
|
|
|
|
} catch { |
109
|
|
|
|
|
|
|
$response->responseContent; |
110
|
|
|
|
|
|
|
}; |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
return ($response->responseCode, $content); |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
#------------------------------------------------------------------------------- |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
1; |