Business-US-USPS-WebTools

 view release on metacpan or  search on metacpan

lib/Business/US/USPS/WebTools.pm  view on Meta::CPAN

	$args->{Password} = $password;
	$args->{testing}  = $args->{Testing} || 0;
	$args->{live}     = ! $args->{Testing};

	bless $args, $class;
	}

sub _testing { $_[0]->{testing} }
sub _live    { $_[0]->{live}    }

=item userid

Returns the User ID for the web service. You need to get this from the
US Postal Service.

=item password

Returns the Password for the web service. You need to get this from the
US Postal Service.

=item url

Returns the URL for the request to the web service. So far, all requests
are GET request with all of the data in the query string.

=item tx

Returns the transaction from the Mojo::UserAgent request.

=item response

Returns the response from the web service. This is the slightly modified
response. So far it only fixes up line endings and normalizes some error
output for inconsistent responses from different physical servers.

=cut

sub userid   { $_[0]->{UserID} }
sub password { $_[0]->{Password} }

sub url      { $_[0]->{url} || $_[0]->_make_url( $_[1] ) }
sub tx       { $_[0]->{transaction} }
sub response { $_[0]->{response} }

sub _api_host {
	my $self = shift;

	if( $self->_testing ) { $self->test_server_host }
	elsif( $self->_live ) { $self->live_server_host }
	else                  { die "Am I testing or live?" }
	}

sub _api_path {
	$_[0]->_live ?
		"/ShippingAPI.dll"
			:
		"/ShippingAPI.dll"
		}

sub _make_url {
	state $rc = require Mojo::URL;
	my( $self, $hash ) = @_;

	$self->{url} = Mojo::URL->new
		->scheme('https')
		->host( $self->_api_host )
		->path( $self->_api_path )
		->query(
			API => $self->_api_name,
			XML => $self->_make_query_xml( $hash )
			);
	}

sub _make_request {
	my( $self, $url ) = @_;
	state $rc = require Mojo::UserAgent;
	state $ua = Mojo::UserAgent->new;

	$self->{error} = undef;

	$self->{transaction} = $ua->get( $self->url );
	$self->{response} = $self->{transaction}->result->body;

	$self->is_error;

	use Data::Dumper;
#	print STDERR "In _make_request:\n" . Dumper( $self ) . "\n";

	$self->{response};
	}

sub _clone {
	my $rc = require Storable;
	my( $self ) = @_;

	my $clone = Storable::dclone( $self );
	}

=item is_error

Returns true if the response to the last request was an error, and false
otherwise.

If the response was an error, this method sets various fields in the
object:

	$self->{error}{number}
	$self->{error}{source}
	$self->{error}{description}
	$self->{error}{help_file}
	$self->{error}{help_context}

=cut

sub is_error {
	my $self = shift;

	return 0 unless $self->response =~ "<Error>";

	$self->{error} = {};

	# Apparently not all servers return this string in the
	# same way. Some have SOL and some have SoL
	$self->{response} =~ s/SOLServer/SOLServer/ig;

	( $self->{error}{number}       ) = $self->response =~ m|<Number>(-?\d+)</Number>|g;
	( $self->{error}{source}       ) = $self->response =~ m|<Source>(.*?)</Source>|g;
	( $self->{error}{description}  ) = $self->response =~ m|<Description>(.*?)</Description>|g;
	( $self->{error}{help_file}    ) = $self->response =~ m|<HelpFile>(.*?)</HelpFile>|ig;
	( $self->{error}{help_context} ) = $self->response =~ m|<HelpContext>(.*?)</HelpContext>|ig;

	1;
	}

=item live_server_host

This is production.shippingapis.com. The modules choose this host
when you have not set C<Testing> to a true value.

=item test_server_host

For most APIs, this is testing.shippingapis.com. The modules choose this host
when you have set C<Testing> to a true value.

=cut

sub live_server_host { "production.shippingapis.com" };
sub test_server_host { "stg-production.shippingapis.com" };


=back

=head1 SEE ALSO



( run in 1.506 second using v1.01-cache-2.11-cpan-39bf76dae61 )