1
# This program is copyright 2012 codenode LLC, 2012-2013 Percona Ireland Ltd.
3
# THIS PROGRAM IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
4
# WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
5
# MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
7
# This program is free software; you can redistribute it and/or modify it under
8
# the terms of the GNU General Public License as published by the Free Software
9
# Foundation, version 2; OR the Perl Artistic License. On UNIX and similar
10
# systems, you can issue `man perlgpl' or `man perlartistic' to read these
13
# You should have received a copy of the GNU General Public License along with
14
# this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15
# Place, Suite 330, Boston, MA 02111-1307 USA.
16
# ###########################################################################
17
# Percona::WebAPI::Client package
18
# ###########################################################################
20
package Percona::WebAPI::Client;
22
our $VERSION = '0.01';
25
use warnings FATAL => 'all';
26
use English qw(-no_match_vars);
27
use constant PTDEBUG => $ENV{PTDEBUG} || 0;
34
use Scalar::Util qw(blessed);
38
use Percona::WebAPI::Representation;
39
use Percona::WebAPI::Exception::Request;
40
use Percona::WebAPI::Exception::Resource;
42
Percona::WebAPI::Representation->import(qw(as_json));
43
Percona::Toolkit->import(qw(_d Dumper have_required_args));
55
default => sub { return 'https://cloud-api.percona.com' },
63
builder => '_build_ua',
75
my $ua = LWP::UserAgent->new;
76
$ua->agent("Percona::WebAPI::Client/$Percona::WebAPI::Client::VERSION");
77
$ua->default_header('Content-Type', 'application/json');
78
$ua->default_header('X-Percona-API-Key', $self->api_key);
83
my ($self, %args) = @_;
85
have_required_args(\%args, qw(
88
my ($link) = $args{link};
90
# Get the resources at the link.
97
if ( my $e = $EVAL_ERROR ) {
98
if (blessed($e) && $e->isa('Percona::WebAPI::Exception::Request')) {
102
die "Unknown error: $e";
106
# The resource should be represented as JSON, decode it.
107
my $resource = eval {
108
JSON::decode_json($self->response->content);
111
warn sprintf "Error decoding resource: %s: %s",
112
$self->response->content,
117
# If the server tells us the resource's type, create a new object
118
# of that type. Else, if there's no type, there's no resource, so
119
# we should have received links. This usually only happens for the
120
# entry link. The returned resource objects ref may be scalar or
121
# an arrayref; the caller should know.
122
my $resource_objects;
123
if ( my $type = $self->response->headers->{'x-percona-resource-type'} ) {
125
$type = "Percona::WebAPI::Resource::$type";
126
if ( ref $resource eq 'ARRAY' ) {
127
PTDEBUG && _d('Got a list of', $type, 'resources');
128
$resource_objects = [];
129
foreach my $attribs ( @$resource ) {
130
my $obj = $type->new(%$attribs);
131
push @$resource_objects, $obj;
135
PTDEBUG && _d('Got a', $type, 'resource', Dumper($resource));
136
$resource_objects = $type->new(%$resource);
139
if ( my $e = $EVAL_ERROR ) {
140
die Percona::WebAPI::Exception::Resource->new(
143
data => (ref $resource eq 'ARRAY' ? $resource : [ $resource ]),
148
elsif ( exists $resource->{links} ) {
149
# Lie to the caller: this isn't an object, but the caller can
150
# treat it like one, e.g. my $links = $api->get(<entry links>);
151
# then access $links->{self}. A Links object couldn't have
152
# dynamic attribs anyway, so no use having a real Links obj.
153
$resource_objects = $resource->{links};
156
warn "Did not get X-Percona-Resource-Type or links from $link\n";
159
return $resource_objects;
162
# For a successful POST, the server sets the Location header with
163
# the URI of the newly created resource.
170
return $self->response->header('Location');
179
return $self->response->header('Location');
183
my ($self, %args) = @_;
184
have_required_args(\%args, qw(
187
my ($link) = $args{link};
193
headers => { 'Content-Length' => 0 },
196
if ( my $e = $EVAL_ERROR ) {
197
if (blessed($e) && $e->isa('Percona::WebAPI::Exception::Request')) {
201
die "Unknown error: $e";
208
# Low-level POST and PUT handler.
210
my ($self, %args) = @_;
211
have_required_args(\%args, qw(
216
my $method = $args{method};
217
my $res = $args{resources};
218
my $link = $args{link};
221
my $headers = $args{headers};
224
if ( ref($res) eq 'ARRAY' ) {
225
PTDEBUG && _d('List of resources');
226
$content = '[' . join(",\n", map { as_json($_) } @$res) . ']';
228
elsif ( ref($res) ) {
229
PTDEBUG && _d('Resource object');
230
$content = as_json($res);
232
elsif ( $res !~ m/\n/ && -f $res ) {
233
PTDEBUG && _d('List of resources in file', $res);
236
local $INPUT_RECORD_SEPARATOR = undef;
237
open my $fh, '<', $res
238
or die "Error opening $res: $OS_ERROR";
241
$data =~ s/,?\s*$/]/;
245
PTDEBUG && _d('Resource text');
257
if ( my $e = $EVAL_ERROR ) {
258
if (blessed($e) && $e->isa('Percona::WebAPI::Exception::Request')) {
262
die "Unknown error: $e";
269
# Low-level HTTP request handler for all methods. Sets $self->response
270
# from the request. Returns nothing on success (HTTP status 2xx-3xx),
271
# else throws an Percona::WebAPI::Exception::Request.
273
my ($self, %args) = @_;
275
have_required_args(\%args, qw(
279
my $method = $args{method};
280
my $link = $args{link};
283
my $content = $args{content};
284
my $headers = $args{headers};
286
my $req = HTTP::Request->new($method => $link);
288
$req->content($content);
291
map { $req->header($_ => $headers->{$_}) } keys %$headers;
293
PTDEBUG && _d('Request', $method, $link, Dumper($req));
295
my $response = $self->ua->request($req);
296
PTDEBUG && _d('Response', Dumper($response));
298
$self->response($response);
300
if ( !($response->code >= 200 && $response->code < 400) ) {
301
die Percona::WebAPI::Exception::Request->new(
305
status => $response->code,
306
error => "Failed to $method $link",
316
# ###########################################################################
317
# End Percona::WebAPI::Client package
318
# ###########################################################################