Fix WebAPI::Client to always expect links: in reponse.

This commit is contained in:
Daniel Nichter
2012-12-26 09:16:53 -07:00
parent 5051abc7ec
commit 87080d44b2
3 changed files with 43 additions and 53 deletions

View File

@@ -782,8 +782,10 @@ has 'ua' => (
); );
has 'response' => ( has 'response' => (
is => 'rw', is => 'rw',
isa => 'Object', isa => 'Object',
required => 0,
default => undef,
); );
sub _build_ua { sub _build_ua {
@@ -799,9 +801,8 @@ sub BUILD {
my ($self) = @_; my ($self) = @_;
eval { eval {
$self->_request( $self->get(
method => 'GET', url => $self->base_url,
url => $self->base_url,
); );
}; };
if ( my $e = $EVAL_ERROR ) { if ( my $e = $EVAL_ERROR ) {
@@ -813,11 +814,6 @@ sub BUILD {
} }
} }
my $entry_links = decode_json($self->response->content);
PTDEBUG && _d('Entry links', Dumper($entry_links));
$self->links($entry_links);
return; return;
} }
@@ -977,18 +973,17 @@ sub _set {
} }
} }
my $links; my $response = eval {
eval { decode_json($self->response->content);
$links = decode_json($self->response->content);
}; };
if ( $EVAL_ERROR ) { if ( $EVAL_ERROR ) {
warn sprintf "Error decoding resource: %s: %s", warn sprintf "Error decoding response to $method $url: %s: %s",
$self->response->content, $self->response->content,
$EVAL_ERROR; $EVAL_ERROR;
return; return;
} }
$self->update_links($links); $self->update_links($response->{links});
return; return;
} }
@@ -1016,34 +1011,33 @@ sub _request {
} }
PTDEBUG && _d('Request', $method, $url, Dumper($req)); PTDEBUG && _d('Request', $method, $url, Dumper($req));
my $res = $self->ua->request($req); my $response = $self->ua->request($req);
PTDEBUG && _d('Response', Dumper($res)); PTDEBUG && _d('Response', Dumper($response));
if ( uc($method) eq 'DELETE' ) { if ( uc($method) eq 'DELETE' ) {
$self->ua->default_header('Content-Length' => undef); $self->ua->default_header('Content-Length' => undef);
} }
if ( !($res->code >= 200 && $res->code < 400) ) { if ( !($response->code >= 200 && $response->code < 400) ) {
die Percona::WebAPI::Exception::Request->new( die Percona::WebAPI::Exception::Request->new(
method => $method, method => $method,
url => $url, url => $url,
content => $content, content => $content,
status => $res->code, status => $response->code,
error => $res->content, error => $response->content,
); );
} }
$self->response($res); $self->response($response);
return; return;
} }
sub update_links { sub update_links {
my ($self, $new_links) = @_; my ($self, $links) = @_;
while (my ($svc, $links) = each %$new_links) { return unless $links && ref $links && scalar keys %$links;
while (my ($rel, $link) = each %$links) { while (my ($rel, $link) = each %$links) {
$self->links->{$svc}->{$rel} = $link; $self->links->{$rel} = $link;
}
} }
PTDEBUG && _d('Updated links', Dumper($self->links)); PTDEBUG && _d('Updated links', Dumper($self->links));
return; return;

View File

@@ -69,8 +69,10 @@ has 'ua' => (
); );
has 'response' => ( has 'response' => (
is => 'rw', is => 'rw',
isa => 'Object', isa => 'Object',
required => 0,
default => undef,
); );
sub _build_ua { sub _build_ua {
@@ -86,9 +88,8 @@ sub BUILD {
my ($self) = @_; my ($self) = @_;
eval { eval {
$self->_request( $self->get(
method => 'GET', url => $self->base_url,
url => $self->base_url,
); );
}; };
if ( my $e = $EVAL_ERROR ) { if ( my $e = $EVAL_ERROR ) {
@@ -100,11 +101,6 @@ sub BUILD {
} }
} }
my $entry_links = decode_json($self->response->content);
PTDEBUG && _d('Entry links', Dumper($entry_links));
$self->links($entry_links);
return; return;
} }
@@ -271,18 +267,17 @@ sub _set {
} }
} }
my $links; my $response = eval {
eval { decode_json($self->response->content);
$links = decode_json($self->response->content);
}; };
if ( $EVAL_ERROR ) { if ( $EVAL_ERROR ) {
warn sprintf "Error decoding resource: %s: %s", warn sprintf "Error decoding response to $method $url: %s: %s",
$self->response->content, $self->response->content,
$EVAL_ERROR; $EVAL_ERROR;
return; return;
} }
$self->update_links($links); $self->update_links($response->{links});
return; return;
} }
@@ -310,34 +305,33 @@ sub _request {
} }
PTDEBUG && _d('Request', $method, $url, Dumper($req)); PTDEBUG && _d('Request', $method, $url, Dumper($req));
my $res = $self->ua->request($req); my $response = $self->ua->request($req);
PTDEBUG && _d('Response', Dumper($res)); PTDEBUG && _d('Response', Dumper($response));
if ( uc($method) eq 'DELETE' ) { if ( uc($method) eq 'DELETE' ) {
$self->ua->default_header('Content-Length' => undef); $self->ua->default_header('Content-Length' => undef);
} }
if ( !($res->code >= 200 && $res->code < 400) ) { if ( !($response->code >= 200 && $response->code < 400) ) {
die Percona::WebAPI::Exception::Request->new( die Percona::WebAPI::Exception::Request->new(
method => $method, method => $method,
url => $url, url => $url,
content => $content, content => $content,
status => $res->code, status => $response->code,
error => $res->content, error => $response->content,
); );
} }
$self->response($res); $self->response($response);
return; return;
} }
sub update_links { sub update_links {
my ($self, $new_links) = @_; my ($self, $links) = @_;
while (my ($svc, $links) = each %$new_links) { return unless $links && ref $links && scalar keys %$links;
while (my ($rel, $link) = each %$links) { while (my ($rel, $link) = each %$links) {
$self->links->{$svc}->{$rel} = $link; $self->links->{$rel} = $link;
}
} }
PTDEBUG && _d('Updated links', Dumper($self->links)); PTDEBUG && _d('Updated links', Dumper($self->links));
return; return;

View File

@@ -28,7 +28,9 @@ my $ua = Percona::Test::Mock::UserAgent->new(
$ua->{responses}->{get} = [ $ua->{responses}->{get} = [
{ {
content => { content => {
agents => '/agents', links => {
agents => '/agents',
},
}, },
}, },
]; ];