summaryrefslogtreecommitdiff
path: root/BatchPayment/Transport
diff options
context:
space:
mode:
Diffstat (limited to 'BatchPayment/Transport')
-rw-r--r--BatchPayment/Transport/File.pm63
-rw-r--r--BatchPayment/Transport/HTTPS.pm58
-rw-r--r--BatchPayment/Transport/SFTP.pm88
3 files changed, 209 insertions, 0 deletions
diff --git a/BatchPayment/Transport/File.pm b/BatchPayment/Transport/File.pm
new file mode 100644
index 0000000..eaaba49
--- /dev/null
+++ b/BatchPayment/Transport/File.pm
@@ -0,0 +1,63 @@
+package Business::BatchPayment::Transport::File;
+
+=head2 File transport
+
+The simplest case. Takes two arguments, 'input' and 'output'. These can
+be open filehandles or strings naming files. If unspecified, they default
+to /dev/null.
+
+=cut
+
+use IO::File;
+use Moose;
+with 'Business::BatchPayment::Transport';
+
+has 'input' => (
+ is => 'rw',
+ isa => 'Maybe[FileHandle|Str]',
+ default => sub {
+ warn "no input passed to file transport; using /dev/null";
+ '/dev/null'
+ },
+ #lazy => 1,
+);
+
+has 'output' => (
+ is => 'rw',
+ isa => 'Maybe[FileHandle|Str]',
+ default => sub {
+ warn "no output passed to file transport; using /dev/null";
+ '/dev/null'
+ },
+ #lazy => 1,
+);
+
+sub upload {
+ my $self = shift;
+ my $text = shift;
+ my $fh;
+ if ( ref $self->output ) {
+ $fh = $self->output;
+ } else {
+ $fh = IO::File->new();
+ $fh->open($self->output,'>')
+ or die "couldn't write to ".$self->output;
+ }
+ print $fh $text;
+}
+
+sub download {
+ my $self = shift;
+ my $fh;
+ if ( ref $self->input ) {
+ $fh = $self->input;
+ } else {
+ $fh = IO::File->new();
+ $fh->open($self->input,'<')
+ or die "couldn't read from ".$self->input;
+ }
+ local $/;
+ my $text = <$fh>;
+}
+
+1;
diff --git a/BatchPayment/Transport/HTTPS.pm b/BatchPayment/Transport/HTTPS.pm
new file mode 100644
index 0000000..c40a78f
--- /dev/null
+++ b/BatchPayment/Transport/HTTPS.pm
@@ -0,0 +1,58 @@
+package Business::BatchPayment::Transport::HTTPS;
+
+=head2 HTTPS transport
+
+Sends a request by HTTPS POST, and downloads the response the same way.
+Options are 'server', 'port', 'get_path', 'put_path', optionally
+'content_type'.
+
+=cut
+
+use Net::HTTPS::Any 0.10;
+use Moose;
+with 'Business::BatchPayment::Transport';
+
+has [ qw( host port get_path put_path ) ] => (
+ is => 'ro',
+ isa => 'Str'
+);
+
+has 'content_type' => (
+ is => 'rw',
+ isa => 'Str',
+ default => 'text/plain'
+);
+
+sub https_post {
+ my $self = shift;
+ my $path = shift;
+ my $content = shift;
+
+ warn "starting https_post...\n" if $self->debug;
+ my ( $page, $response, %reply_headers ) = Net::HTTPS::Any::https_post(
+ host => $self->host,
+ port => $self->port,
+ path => $path,
+ content => $content,
+ debug => ($self->debug >= 3),
+ );
+ warn "PAGE:\n$page\n\nRESPONSE:\n$response\n\n" if $self->debug >= 2;
+ return ($page, $response, %reply_headers);
+}
+
+sub upload {
+ my $self = shift;
+ my $content = shift;
+ $self->https_post($self->put_path, $content);
+}
+
+sub download {
+ # will probably need to be overridden in most cases
+ my $self = shift;
+ my ($page, $response, %reply_headers) = $self->https_post($self->get_path);
+ $page;
+}
+
+__PACKAGE__->meta->make_immutable;
+
+1;
diff --git a/BatchPayment/Transport/SFTP.pm b/BatchPayment/Transport/SFTP.pm
new file mode 100644
index 0000000..08d2fb7
--- /dev/null
+++ b/BatchPayment/Transport/SFTP.pm
@@ -0,0 +1,88 @@
+package Business::BatchPayment::Transport::SFTP;
+
+=head2 Business::BatchPayment::Transport::SFTP
+
+Import this role to use SFTP. Requires "host", "port', "user", and
+"password" to be set. The 'sftp' method returns a Net::SFTP::Foreign
+object providing 'get' and 'put' methods (among others). All of these
+methods die on error.
+
+Also provides "get_path" and "put_path" attributes. The default 'download'
+and 'upload' methods get and put to those paths on the SFTP host. You
+may find it useful to modify or override that behavior.
+
+=cut
+
+use Net::SFTP::Foreign;
+use File::Slurp qw(read_file);
+use Moose;
+with 'Business::BatchPayment::Transport';
+
+has [ qw( host login password ) ] => (
+ is => 'rw',
+ isa => 'Str',
+);
+
+has 'port' => (
+ is => 'rw',
+ isa => 'Int',
+ default => 22,
+);
+
+has [ 'get_path', 'put_path' ] => (
+ is => 'rw',
+ isa => 'Str',
+);
+
+has 'sftp' => (
+ is => 'ro',
+ isa => 'Net::SFTP::Foreign',
+ handles => [ 'get', 'put', 'ls', 'setcwd' ],
+ builder => '_sftp',
+ lazy => 1,
+);
+
+has 'timeout' => (
+ is => 'rw',
+ isa => 'Int',
+ default => 60,
+);
+
+sub _sftp {
+ my $self = shift;
+ my %args = (
+ host => $self->host,
+ port => $self->port,
+ user => $self->login,
+ password => $self->password,
+ timeout => $self->timeout,
+ autodie => 1,
+ );
+ $args{'more'} = '-v' if $self->debug >= 2;
+ Net::SFTP::Foreign->new(%args);
+}
+
+sub upload {
+ my $self = shift;
+ die "default SFTP upload requires 'put_path'\n" unless $self->put_path;
+ my $content = shift;
+ my ($out, $tmpfile) = tempfile();
+ die "failed to open temporary file $tmpfile\n" unless $out;
+ print $out $content;
+ close $out;
+ $self->sftp->put($tmpfile, $self->put_path);
+ unlink $tmpfile;
+}
+
+sub download { # mostly illustrative rather than directly useful
+ my $self = shift;
+ die "default SFTP download requires 'get_path'\n" unless $self->get_path;
+ my ($out, $tmpfile) = tempfile();
+ $self->sftp->get($self->get_path, $out);
+ close $out;
+ return read_file($tmpfile);
+}
+
+__PACKAGE__->meta->make_immutable;
+
+1;