This commit was generated by cvs2svn to compensate for changes in r2523,
[freeside.git] / FS / FS / part_export / http.pm
1 package FS::part_export::http;
2
3 use vars qw(@ISA);
4 use FS::part_export;
5
6 @ISA = qw(FS::part_export);
7
8 sub rebless { shift; }
9
10 sub _export_insert {
11   my $self = shift;
12   $self->_export_command('insert', @_);
13 }
14
15 sub _export_delete {
16   my $self = shift;
17   $self->_export_command('delete', @_);
18 }
19
20 sub _export_command {
21   my( $self, $action, $svc_x ) = ( shift, shift, shift );
22
23   return unless $self->option("${action}_data");
24
25   $self->http_queue( $svc_x->svcnum,
26     $self->option('method'),
27     $self->option('url'),
28     map {
29       /^\s*(\S+)\s+(.*)$/ or /()()/;
30       my( $field, $value_expression ) = ( $1, $2 );
31       my $value = eval $value_expression;
32       die $@ if $@;
33       ( $field, $value );
34     } split(/\n/, $self->option("${action}_data") )
35   );
36
37 }
38
39 sub _export_replace {
40   my( $self, $new, $old ) = ( shift, shift, shift );
41
42   return unless $self->option('replace_data');
43
44   $self->http_queue( $svc_x->svcnum,
45     $self->option('method'),
46     $self->option('url'),
47     map {
48       /^\s*(\S+)\s+(.*)$/ or /()()/;
49       my( $field, $value_expression ) = ( $1, $2 );
50       die $@ if $@;
51       ( $field, $value );
52     } split(/\n/, $self->option('replace_data') )
53   );
54
55 }
56
57 sub http_queue {
58   my($self, $svcnum) = (shift, shift);
59   my $queue = new FS::queue {
60     'svcnum' => $svcnum,
61     'job'    => "FS::part_export::http::http",
62   };
63   $queue->insert( @_ );
64 }
65
66 sub http {
67   my($method, $url, @data) = @_;
68
69   $method = lc($method);
70
71   eval "use LWP::UserAgent;";
72   die "using LWP::UserAgent: $@" if $@;
73   eval "use HTTP::Request::Common;";
74   die "using HTTP::Request::Common: $@" if $@;
75
76   my $ua = LWP::UserAgent->new;
77
78   #my $response = $ua->$method(
79   #  $url, \%data,
80   #  'Content-Type'=>'application/x-www-form-urlencoded'
81   #);
82   my $req = HTTP::Request::Common::POST( $url, \@data );
83   my $response = $ua->request($req);
84
85   die $response->error_as_HTML if $response->is_error;
86
87 }
88