Business-cXML

 view release on metacpan or  search on metacpan

lib/Business/cXML.pm  view on Meta::CPAN


	$self->_debug("process(60) returning", $str);
	return $str;
}

=item C<B<new_request>( [I<$type>] )>

Returns a fresh L<Business::cXML::Transmission> ready to be used as a request.
Optional I<C<$type>> is a convenience shortcut to
L<Business::cXML::Transmission::type()|cXML::Transmission/type>.  The
request's sender secret will be pre-filled.

=cut

sub new_request {
	my ($self, $type) = @_;
	my $req = new Business::cXML::Transmission;
	$req->is_request(1);
	$req->type($type) if defined $type;
	$req->sender->secret($self->{secret}) if defined $self->{secret};
	return $req;
}

=item C<B<send>( I<$request> )>

Freeze I<C<$request>>, a L<Business::cXML::Transmission>, and attempt sending
it to the configured remote server.  Returns the received response
L<Business::cXML::Transmission> on success, C<undef> on failure.  Note that as
per L<Business::cXML::Transmission::new()|Business::cXML::Transmission/new>,
it is also possible that an error arrayref be returned instead of a
transmission if parsing failed.

In case of failure, you may want to wait a certain amount of time and try
again.  To give you more options to that effect, I<C<$request>> can be either
a L<Business::cXML::Transmission> or a string.

=cut

use LWP::UserAgent;
sub send {
	my ($self, $req) = @_;
	my $err;
	my $obj;

	if (ref($req)) {
		$obj = $req;
		($err, $req) = $req->freeze();
		return $self->_error("send(11): $err", $req, $obj) if defined $err;
	};
	$self->_notice("send() making HTTP request", $req, $obj);

	my $ua = new LWP::UserAgent;
	$ua->agent($Business::cXML::USERAGENT);
	$ua->timeout(30);
	my $res = $ua->post(
		$self->{remote},
		'Content-Type' => 'text/xml; charset="UTF-8"',
		'Content' => $req,
	);
	if ($res->is_success) {
		$res = $res->decoded_content;
		my $msg = new Business::cXML::Transmission $res;
		unless (defined blessed($msg)) {
			# We have an error status code
			return $self->_warning('send(21) ' . ($msg->[0] == 406 ? 'response XML validation' : 'response cXML traversal') . ' failure', $res);
		};
		$self->_notice("send() received HTTP response", $res, $msg);
		return $msg;
	} else {
		return $self->_warning("send(22) had network failure", $req, $obj);
	};
}

=item C<B<new_message>( [I<$type>] )>

Returns a fresh L<Business::cXML::Transmission> ready to be used as a
stand-alone message.  Optional I<C<$type>> is passed on to
L<Business::cXML::Transmission::type()|Business::cXML::Transmission/type>.

=cut

sub new_message {
	my ($self, $type) = @_;
	my $msg = new Business::cXML::Transmission;
	$msg->is_message(1);
	$msg->type($type) if defined $type;
	return $msg;
}

=item C<B<stringify>( I<$message>, I<%args> )>

Convenience wrapper around
L<Business::cXML::Transmission::toForm()|Business::cXML::Transmission/toForm>
which allows you to trap logging events.

=cut

sub stringify {
	my ($self, $msg, %args) = @_;
	my ($err, $str) = $msg->toForm(%args);
	$self->_error("stringify(): $err", $str, $msg) if defined $err;
	return $str;
}

=back

=head1 VERSION

0.6.12 based on cXML DTD 1.2.036

=head1 AUTHOR

Stéphane Lavergne L<https://github.com/vphantom>

=head1 ACKNOWLEDGEMENTS

Graph X Design Inc. L<https://www.gxd.ca/> sponsored this project.

=head1 COPYRIGHT & LICENSE

Copyright (c) 2017-2018 Stéphane Lavergne L<https://github.com/vphantom>



( run in 1.224 second using v1.01-cache-2.11-cpan-600a1bdf6e4 )