| 1 | package Punc::Client::Request; |
|---|
| 2 | |
|---|
| 3 | use Moose; |
|---|
| 4 | use JSON; |
|---|
| 5 | use JSON::RPC::Client; |
|---|
| 6 | use Punc::Client::Response; |
|---|
| 7 | use File::Spec; |
|---|
| 8 | |
|---|
| 9 | our $AUTOLOAD; |
|---|
| 10 | |
|---|
| 11 | sub new { |
|---|
| 12 | my ( $class, $args ) = @_; |
|---|
| 13 | |
|---|
| 14 | my $confdir = $args->{conf}->{confdir}; |
|---|
| 15 | |
|---|
| 16 | $ENV{HTTPS_VERSION} = 3; |
|---|
| 17 | $ENV{HTTPS_CERT_FILE} = File::Spec->catfile( |
|---|
| 18 | $confdir, 'ssl', 'ca', 'ca.cert' |
|---|
| 19 | ); |
|---|
| 20 | $ENV{HTTPS_KEY_FILE} = File::Spec->catfile( |
|---|
| 21 | $confdir, 'ssl', 'ca', 'ca.key' |
|---|
| 22 | ); |
|---|
| 23 | |
|---|
| 24 | $args->{client} = JSON::RPC::Client->new; |
|---|
| 25 | |
|---|
| 26 | bless $args, $class; |
|---|
| 27 | } |
|---|
| 28 | |
|---|
| 29 | sub request { |
|---|
| 30 | my $self = shift; |
|---|
| 31 | |
|---|
| 32 | my $response = Punc::Client::Response->new; |
|---|
| 33 | for my $host ( @{ $self->{hosts} } ) { |
|---|
| 34 | |
|---|
| 35 | my $url = "https://$host:7080/$self->{module}"; |
|---|
| 36 | my $callobj = { |
|---|
| 37 | method => $self->{method}, |
|---|
| 38 | params => $self->{args}, |
|---|
| 39 | }; |
|---|
| 40 | |
|---|
| 41 | my $res = $self->{client}->call($url, $callobj) or warn @?; |
|---|
| 42 | |
|---|
| 43 | if( $res ) { |
|---|
| 44 | $response->add({ |
|---|
| 45 | host => $host, |
|---|
| 46 | response => $res->content, |
|---|
| 47 | }); |
|---|
| 48 | } |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | return $response; |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | sub AUTOLOAD { |
|---|
| 55 | no strict 'refs'; |
|---|
| 56 | my ( $self, $args ) = @_; |
|---|
| 57 | (my $method = $AUTOLOAD) =~ s/^.*:://; |
|---|
| 58 | return if $method eq 'DESTROY'; |
|---|
| 59 | |
|---|
| 60 | $self->{method} = $method; |
|---|
| 61 | $self->{args} = $args; |
|---|
| 62 | |
|---|
| 63 | if ( $self->{module} eq 'file' and $self->{method} eq 'copy' ) { |
|---|
| 64 | open my $fh, '<', $args->{src} or die $!; |
|---|
| 65 | $args->{content} = do { local $/; <$fh> }; |
|---|
| 66 | close $fh; |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | return $self->request; |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | 1; |
|---|