unused module
[freeside.git] / FS / FS / part_event / Action / http.pm
1 package FS::part_event::Action::http;
2 use base qw( FS::part_event::Action );
3
4 use strict;
5 use vars qw( $me );
6 use IO::Socket::SSL;
7 use LWP::UserAgent;
8 use HTTP::Request::Common;
9 use Cpanel::JSON::XS;
10 use FS::Misc::DateTime qw( iso8601 );
11
12 $me = '[FS::part_event::Action::http]';
13
14 #sub description { 'Send an HTTP or HTTPS GET or POST request'; }
15 sub description { 'Send an HTTP or HTTPS POST request'; }
16
17 sub eventtable_hashref {
18   { 'cust_bill' => 1,
19     'cust_pay'  => 1,
20   },
21 }
22
23 sub option_fields {
24   (
25     'method'        => { label => 'Method',
26                          type  => 'select',
27                          options => [qw( POST )], #GET )],
28                        },
29     'url'           => { label => 'URL',
30                          type  => 'text',
31                          size  => 120,
32                        },
33     'ssl_no_verify' => { label => 'Skip SSL certificate validation',
34                          type  => 'checkbox',
35                        },
36     'encoding'      => { label => 'Encoding',
37                          type  => 'select',
38                          options => [qw( JSON )], #XML, Form, etc.
39                        },
40     'content'       => { label => 'Content', #nneed better inline docs on format
41                          type  => 'textarea',
42                        },
43     #'response_error_param' => 'Response error parameter',
44     'debug'         => { label => 'Enable debugging',
45                          type  => 'checkbox',
46                          value => 1,
47                        },
48   );
49 }
50
51 sub default_weight { 57; }
52
53 our %content_type = (
54   'JSON' => 'application/json',
55 );
56
57 sub do_action {
58   my( $self, $object ) = @_;
59
60   my $cust_main = $self->cust_main($object);
61
62   my %content =
63     map {
64       /^\s*(\S+)\s+(.*)$/ or /()()/;
65       my( $field, $value_expression ) = ( $1, $2 );
66       my $value = eval $value_expression;
67       die $@ if $@;
68       ( $field, $value );
69     } split(/\n/, $self->option('content') );
70
71   if ( $self->option('debug') ) {
72     warn "[$me] $_: ". $content{$_}. "\n" foreach keys %content;
73   }
74
75   my $content = encode_json( \%content );
76
77   my @lwp_opts = ();
78   push @lwp_opts, 'ssl_opts' => {
79                     verify_hostname => 0,
80                     SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE,
81                   }
82     if $self->option('ssl_no_verify');
83   my $ua = LWP::UserAgent->new(@lwp_opts);
84
85   my $req = HTTP::Request::Common::POST(
86     $self->option('url'),
87     Content_Type => $content_type{ $self->option('encoding') },
88     Content      => $content,
89   );
90
91   if ( $self->option('debug') ) {
92     #XXX dump raw request for debugging
93   }
94   my $response = $ua->request($req);
95
96   die $response->status_line if $response->is_error;
97
98   my $response_json = decode_json( $response->content );
99   die $response_json->{error} if $response_json->{error}; #XXX response_error_param
100
101 }
102
103 1;