REST-Consumer

 view release on metacpan or  search on metacpan

lib/REST/Consumer/HandlerInvocation.pm  view on Meta::CPAN

package REST::Consumer::HandlerInvocation;

use strict;
use warnings;

use REST::Consumer::ResponseException;

# This is an object which is passed to a coderef in the handlers => {}
# hash. It represents an invocation of a particular response-code handler.
#
# Your code should never need to instantiate this class itself.
sub new {
	my ($class, %args) = @_;
	my $self = (bless {%args} => $class);
	$self->{debugger} ||= sub {};
	return $self;
}

# Here are elments of the public API:
# Magic values your code can return to change the execution flow:
sub default {
	my ($self) = @_;
	return REST::Consumer::HandlerFlow::Default->new;
}
sub retry {
	my ($self) = @_;
	return REST::Consumer::HandlerFlow::Retry->new(
		request => $self->request,
		response => $self->response,
		attempt => $self->attempt,
	);
}
sub fail {
	my ($self) = @_;
	return REST::Consumer::HandlerFlow::Fail->new(
		request => $self->request,
		response => $self->response,
		attempt => $self->attempt,
	);
}


# Accessors that can provide information to your handler:
sub request { shift->{request} }
sub response { shift->{response} }
sub attempt { shift->{attempt} } # e.g. attempt #2

# ->parsed_response throws an exception if the response is not parseable.
# You might want to ask whether ->response_parseable.
sub parsed_response {
	my ($self) = @_;
	$self->attempt_content_deserialization;
	unless ($self->{response_parseable}) {
		REST::Consumer::ResponseException->throw(
			request  => $self->{request},
			response => $self->{response},
			attempts => $self->{attempt},
		 );
	}
	return $self->{parsed_response};
}

# True iff the response was parseable.
sub response_parseable {
	my ($self) = @_;
	$self->attempt_content_deserialization;
	return $self->{response_parseable};
}

# This is never parsed, but it is decoded.
sub response_body {
	my ($self) = @_;
	$self->attempt_content_deserialization;
	return $self->{response_body};



( run in 0.557 second using v1.01-cache-2.11-cpan-0bb4e1dffa6 )