root/lang/perl/Punc/trunk/lib/Punc/Client/Request.pm @ 11749

Revision 11749, 1.5 kB (checked in by mizzy, 5 years ago)

use strict;use warnings;を全部use Moose;に置き換えた

Line 
1package Punc::Client::Request;
2
3use Moose;
4use JSON;
5use JSON::RPC::Client;
6use Punc::Client::Response;
7use File::Spec;
8
9our $AUTOLOAD;
10
11sub 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
29sub 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
54sub 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
721;
Note: See TracBrowser for help on using the browser.