fdb2c359d20ce41f89d6970dec51782609ea413e
[Business-BatchPayment.git] / BatchPayment / Transport / HTTPS.pm
1 package Business::BatchPayment::Transport::HTTPS;
2
3 =head2 HTTPS transport
4
5 Sends a request by HTTPS POST, and downloads the response the same way.
6 Options are 'server', 'port', 'get_path', 'put_path', optionally 
7 'content_type'.
8
9 =cut
10
11 use strict;
12 use Moose;
13 use Net::HTTPS::Any 0.10;
14 with 'Business::BatchPayment::Transport';
15
16 has [ qw( host port get_path put_path ) ] => ( 
17   is => 'ro',
18   isa => 'Str'
19 );
20
21 has 'content_type' => (
22   is => 'rw',
23   isa => 'Str',
24   default => 'text/plain'
25 );
26
27 sub https_post {
28   my $self = shift;
29   my $path = shift;
30   my $content = shift;
31
32   warn "starting https_post...\n" if $self->debug;
33   my ( $page, $response, %reply_headers ) = Net::HTTPS::Any::https_post(
34     host => $self->host,
35     port => $self->port,
36     path => $path,
37     content => $content,
38     debug => ($self->debug >= 3),
39   );
40   warn "PAGE:\n$page\n\nRESPONSE:\n$response\n\n" if $self->debug >= 2;
41   return ($page, $response, %reply_headers);
42 }
43
44 sub upload {
45   my $self = shift;
46   my $content = shift;
47   $self->https_post($self->put_path, $content);
48 }
49
50 sub download {
51   # will probably need to be overridden in most cases
52   my $self = shift;
53   my ($page, $response, %reply_headers) = $self->https_post($self->get_path);
54   $page;
55 }
56
57 __PACKAGE__->meta->make_immutable;
58
59 1;